How to determine if an Office Add-in is running un

2019-02-10 20:41发布

问题:

I'm writing an Office Add-in (formerly, Apps for Office). I'm using office.js and in some point of code I want to check if the app is running in excel (desktop software) or running on the web (Excel Online)

Something like:

if (Office.IsRunningOnWeb){
    // Do something.
}

回答1:

You can use document type:

if (Microsoft.Office.WebExtension.context.document instanceof OSF.DDA.ExcelWebAppDocument) {
                                    //Your app running on the web
                                }

if (Microsoft.Office.WebExtension.context.document instanceof OSF.DDA.ExcelDocument) {
                                    //Your app running in excel
                                }


回答2:

@Mehrandvd, if I may ask, what do you need this distinction for (e.g,. what would you do different based on knowing that you're in Excel Online vs. the Desktop)? I work on the Office.js APIs, so I'm happy to channel your feedback to my team, if you can provide some specifics.

If you need this distinction for feature detection, I would recommend checking the API Requirement sets instead, via a new (but back-ported to all endpoints) API, Office.context.requirements.isSetSupported(name, version). Please see my answer at Neat ways to get environment (i.e. Office version).

If it's due to some API differences you're seeing between the Excel desktop vs Online versions, the goal is for the APIs to behave the same across endpoints, so it may be a bug. If you let me know the specifics, I can follow up.

Re. the answer mentioned by @Afshin -- it may work, but just be aware that it's not public API but rather the internal workings that you're testing against, so there's a chance that this approach would stop working in the future... The only publically-exposed namespace is Office (and, with the new Excel and Word APIs released in Sept. 2015, also Excel and Word and OfficeExtension).

Hope this helps!

~ Michael Zlatkovsky

   Developer on Office Extensibility team, MSFT

PS: Please use the office-js tag for tagging these sorts of questions in the future; this is the stackoverflow tag that the Office Extensibility team at Microsoft actively looks at.



回答3:

The question is how do you do it, not why would you want to. There are numerous reasons why you might want to differentiate. Put in this check as follows:

if (window.top == window) {
//the add-in is not running in Excel Online
}
else
{
//the add-in is running in Excel online
}