How to check the host correctly: Office, Office online and web browser
I need to determine if a user hit my add-in URL from a browser, outside of Office, and Office on the web. (I've seen questions here about discovery, whether it's Excel or Excel for the web, but none of them.)
I looked at the utility's functions OfficeHelpers.Utilities.host
and OfficeHelpers.Utilities.isAddin
, but the code notes say they will only work after Office. initialization is called. If the URL was migrated from outside of the Office client, this will not be the case (?).
So I think I need to do the same check as everything is done when you get a warning: Warning in Google Chrome ... to make sure Office.initialize won't run, not that it isn't already launched.
A simple use of this would be to direct users to a page that displays explanations that they cannot access it without downloading it as an add-on, along with instructions for downloading the add-on, etc.
The actual result I'm trying to get is to handle both cases as:
Inside Office client: "GetData" will return some values ββand write them to the document. When accessed from a browser: "GetData" will return some values ββand redirect them to the view to display them.
source to share
There is no great solution at the moment, although I agree that there is an option to open "Office.ready" here to provide the final mark that Office.js is loaded anyway, and that the host and platform are now stably defined (including defined as stable "spider web" and zero).
For Script Lab, we take the approach of waiting on Office.js for three seconds and then giving up and displaying the character set to the user asking them for a host choice. We can probably safely assume that in this case "website", but in our case, we want to provide disambiguation anyway so that users can choose whether to run Web Slices or view the host nodes wherever they want. host choice anyway.
If you're interested in seeing how this is done, see the "initialize" function at https://github.com/OfficeDev/script-lab/blob/eeb4ba89a7a570cf1bb035fdd740ec7dcfb92822/src/client/app/helpers/environment.ts
source to share