9

Unfortunately, some features of the office API do not behave exactly the same in all environments (example: formatting in Excel Online and Excel 2013). Additionally, some nice new features are not available in Excel 2013 but are available in Excel 2016 (Excel.js)

Of course, I could tell users they can only use my app with 2016, and simply not implement things that aren't fully supported in all environments.

I would prefer offering my app to users of Excel 2013 even if they don't have a way (or the inclination) to upgrade to 2016. And I would rather gracefully downgrade my feature list in less capable environments than limit the app's functionality as a whole)

It's easy enough to encapsulate all interaction with the document and run different code depending on the environment. That is, if I know what exact environment I'm in. does the current office.js offer a neat way to discover version and context(online/offline) of the host application? I couldn't find anything in office.context... etc.

There are some suggestions online about hacking into the whole .getContext chain, but these seem to be "undocumented", so I'm not quite happy with that.

Any suggestions?

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Ar Es
  • 389
  • 3
  • 12
  • 1
    Aremes, regarding the first difference that you point out (formatting in Excel Online and Excel 2013): if you can provide more info, I'd be happy to take a look at it for you. It's one thing to have some features only supported on some platforms/versions (for which you can use the requirement detection as per my answer below), but it's quite another to have inconsistent behavior. So if you believe an example of this, please let me know, and I'll file a bug (I work on the Office Extensibility team). – Michael Zlatkovsky - Microsoft Sep 29 '15 at 20:36
  • thanks, i have some, i'll write it up and get it to you by the end of the week :) – Ar Es Sep 30 '15 at 10:15

1 Answers1

14

Update Dec 5, 2016: We will soon be releasing an API to detect the platform info (partially in response to the fact that the _host_info URL paramater, which folks had unofficially relied on, needed to be recently removed for Office Online). We also have a temporary workaround in anticipation of the forthcoming official API. See "In Excel Online, OfficeJS API is not passing the host_Info_ parameter anymore to Excel Add-In" for information on the API and workaround.

I am keeping the old answer below, as it is still relevant for most light-up scenarios. Platform-detection should still be used sparingly, as querying API sets give you more fine-grained control, and ensures that your add-in "lights up" new features when they are added to a particular platform].


It sounds like you're describing a "light-up" scenario. For these sort of use cases, it isn't so much the actual version that you care about (do you really want to keep an internal list of all the minimum versions -- of Excel desktop, and soon Excel Online and iOS, and keep that updated?), but rather, you want to check the capability that something is present. And then offer a differentiated experience depending on whether the capability is there or not.

To that end, I would recommend a brand-new API that we just released alongside these APIs (and that is back-ported to all previous versions -- so as long as you're using the latest Office.js from the CDN, you should be good to go). That API offers you the ability to check, at runtime, whether a particular API set is supported. It looks like:

if (Office.context.requirements.isSetSupported('ExcelApi', 1.1)) {
    // Do something that is only available via the new APIs
}

Official documentation for it is forthcoming shortly, and our sample will soon start using it as well. Stay tuned...

The current set of newly-released Excel APIs is all under "ExcelApi" version 1.1. As we add new APIs, we'll add them to a 1.2 set, 1.3 set, and so on (and mark in the documentation and IntelliSense what set version each API is available in). Does that make sense?


For completeness sake, a few other things to note about Office.js versioning:

1) To ensure that you have the latest APIs, bug fixes, etc., you should always use the CDN location, which gets updated in-place as we roll out new features. That location is https://appsforoffice.microsoft.com/lib/1/hosted/office.js. https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js also works, with the "1" version being just an alias to "1.1" at the moment... but long-term, it's probably best to switch over to the "1" URL.

2) Corollary to the above: you should always use the latest CDN location even for older hosts. That way, you both get "light-up" for new features, and bug fixes (including for older host versions). Basically, you can always use the latest CDN, and rely on the dynamic loading of Office.js script to load the actual host-specific files you need.

3) You can use the new isSetSupported API for both the new API sets "ExcelApi" and "WordApi", and for existing sets (e.g., "MatrixBinding").

4) For APIs that are part of the "Office." namespace, you can also use "defensive programming" to do runtime checks for individual functions (e.g., check that Office.context.document.bindings && Office.context.document.bindings.addFromSelectionAsync) are supported. However, you can see that this can get quite verbose, so checking for a set should be much easier. Also, this will NOT work for APIs under the "Excel" or "Word" namespaces, so all the more reason to use the Office.context.requirements.isSetSupported API.

5) Finally: for apps that ONLY make sense to run if a given requirement set is present, you can specify the API set in the app's manifest. That being said, a manifest check is about specifying the absolute minimum essentials, without which the app simply won't run (or even show up as available in the insert dialog). A runtime check, meanwhile, lets you control how you want to react if a particular API is unsupported (offering the option of "lightup" features, or a downgraded experience). So, I would generally recommend using the lowest manifest-requirement that makes sense for your app, and then doing runtime checks to light up for new features.

Hope this helps,

~ Michael Zlatkovsky
   Developer on Office Extensibility team, MSFT

Community
  • 1
  • 1
  • I am struggling with this: I want to detect whether I am running Word or Excel however the code if (Office.context.requirements.isSetSupported("ExcelApi", 1.1)) { does not get inside the if for Excel 2013. I have tried 1.0 as well. I essentially can't tell if I am in Word or Excel. – gremwell Nov 13 '15 at 10:43
  • @gremwell, Yes, for Office 2013, it would return "false" in either case. But you could choose another requirement (ideally based on WHY you care about being in Word or Excel). For example, if you're using OOXML, you could do isSetSupported("OOXMLCoercion"), which would effectively only return "true" in Word. Does that help? – Michael Zlatkovsky - Microsoft Nov 13 '15 at 17:28
  • @MichaelZlatkovsky Checking the sets, functions, etc. is nice if you have an app that support all hosts in the same way. BUT when you have different behavouir between Word and Excel (but the same core) this is very vague. Why not provide a simple host property in the API? – ndee Jun 21 '16 at 13:59
  • @ndee, for better or worse, Office 2013 had a "common API" philosophy where, by design, the host was not exposed. Instead, developers were meant to only need to worry about sets like "Bindings", so that the same code could run on all hosts that supports bindings. Clearly this was suboptimal for some scenarios (and also limited what we could do with the APIs a great deal), which is why in the 2016+ wave of APIs we have *host-specific* APIs like `Excel.`, `Word.`, etc. This also means that for 2016+, you CAN in fact always find out the host by querying for the `ExcelApi`, `WordAPI` etc sets. – Michael Zlatkovsky - Microsoft Jun 21 '16 at 14:39
  • @MichaelZlatkovsky when _host_Info will gone for Office365 Outlook? I already asked [here](http://stackoverflow.com/a/40982131/2764878) where described how it blocks our Outlook add-in. Thank you. – Peter Nedonosko Feb 15 '17 at 09:07
  • @PeterNedonosko, the official API is coming soon -- but for now, for a temporary workaround, see http://stackoverflow.com/questions/32840459/neat-ways-to-get-environment-i-e-office-version/32851938?noredirect=1#comment71648063_32851938. I haven't personally tested it in Outlook (I have for Word/Excel/PPT), but I have very high confidence that it would work there too. – Michael Zlatkovsky - Microsoft Feb 15 '17 at 22:21
  • @MichaelZlatkovsky your link for workaround leads to my above comment, I guess you wanted an another link there. Can you update with an one? We definitely need a workaround until it will be released in Office 365. – Peter Nedonosko Feb 16 '17 at 11:51
  • @MichaelZlatkovsky thank you for helping. Indeed my problem appears before loading the add-in script. The manifest contains custom pane with `SourceLocation` is https://exoplatform.com.ua/portal/intranet/outlook and when I'm in Outlook for Web and click add-in link, the Outlook app sends a request https://exoplatform.com.ua/portal/intranet/outlook?et=&_host_Info=Outlook|Web|16.01|en-US|b47fbe56-a51a-63eb-6058-591785731e5e|, which is what I have in the manifest but plus an extra query param `_host_Info` which contains illegal characters that my server (Apache Tomcat) rejects to server. – Peter Nedonosko Feb 22 '17 at 13:16
  • @PeterNedonosok, I suggest you create a separate StackOverflow question for this, and add more detail there. It seems like a separate topic (and one that I'm not sure I know the answer to), so having it in a separate thread will vastly increase the chance of someone answering it. – Michael Zlatkovsky - Microsoft Feb 22 '17 at 17:01
  • From this long post I didn't get an answer to how to get `Word version`?! – Alireza Jan 22 '19 at 08:06
  • You can get the host version using `Office.context.diagnostics.version` – Michael Zlatkovsky - Microsoft Jan 24 '19 at 23:35