How to determine if an Office Add-in is running under Excel or Excel Online?

前端 未结 4 566
一个人的身影
一个人的身影 2021-01-03 04:50

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 (deskto

相关标签:
4条回答
  • 2021-01-03 04:59

    @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.

    0 讨论(0)
  • 2021-01-03 05:02

    Inspired by Sturb's answer, the following works with ExcelApi 1.10 & 1.12

    if (window.top.window == window) {
      // Add-in is running in Excel desktop
    } else {
      // Add-in is running in Excel online
    }
    
    0 讨论(0)
  • 2021-01-03 05:07

    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
                                    }
    
    0 讨论(0)
  • 2021-01-03 05:15

    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
    }
    
    0 讨论(0)
提交回复
热议问题