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

4

6

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.
}
Syndic answered 1/6, 2015 at 5:31 Comment(0)
A
4

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

Appal answered 30/9, 2015 at 16:38 Comment(10)
Thank you Michael. As this question is a little old, I don't remember the exact situation. But as I remember, It was because of some different behavior (or a bug as you said). I think, in the TableBinding mode the order of raising DataChanged event and SelectionChanged event was different in desktop and web. If it helps, I can go back to that project to reveal the exact but?Syndic
No problem. But if you do re-discover your repro, feel free to start a separate thread marked with office-js and excel tags, and we (the product team) can have someone investigate.Appal
Michael, I do have an example of when I want to determine whether I'm running in the desktop version of Excel. You feedback is welcome. See -> #33321907Stepfather
@zumalifeguard, I totally understand the desire to have "non-sticky" taskpanes, and will pass it to the appropriate Program Manager in charge of the add-in UX capabilities. I am less convinced that this is a true use-case for needing to distinguish desktop from online, though, since it sounds from that other thread that you're really using that only for a workaround (and could there not be times when you do want the add-in to work in the Desktop scenario, too?).Appal
@MichaelZlatkovsky, the end user sees the feature like so: they go to a sharepoint site, open a blank spreadsheet with some graphics but no data. they insert the add-in, fill out a few fields, and get a new table created with business data. They now want a copy of this report, so they save it as an excel file for future reference. When the report is later opened by other people, we don't want them to have access to the add-in. They may not even be licensed to use it. We really need a ribbon addin like tfs or the new "Office Mix" product in PowerPoint.Stepfather
@zumalifeguard, what I meant to say is not so much "there isn't a use case to distinguish", but rather that while it's true that in your scenario right now the user comes from SharePoint and hence ends up in Excel online, the distinction feels like it could be a point-in-time thing (what if the user did an "Open in Desktop" from within SharePoint?). I think the more generic ask, which I totally understand, is to have "non-sticky" taskpanes, and/or the ability to export out the report. I will pass those requests/requirements to the appropriate folks.Appal
@MichaelZlatkovsky the API under Web and Desktop doesn't work 100% the same, e.g. window.open behaves different under desktop and Web (and on Web different between IE and Chrome...) so I may need to implement different approaches based on the client type. I guess that's why Office.context.mailbox.diagnostics was added?Angulation
@alekkowalczyk, let me bring this back to the team for discussion.Appal
This answer isn't helpful. There are several functions that are not documented, such as Office.context.ui.openBrowserWindow. On desktop this and href's open in browser, but on web this doesn't work, and hrefs open in the pane instead of a new tab. Being able to explicitly check if we're on office online or not is needed to make bloody links work.Bufordbug
@SpaceBear, I am no longer on the Office Extensibility team. But you should be able to use the Office.context.platform API for what you need.Appal
C
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
}
Canaigre answered 5/5, 2016 at 13:15 Comment(0)
H
2

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
                                }
Heading answered 16/6, 2015 at 6:31 Comment(2)
No longer works: OSF.DDA has no key named ExcelWebAppDocument, only ExcelDocument (the same for Word and PowerPoint).Czerny
@Czerny Hey, what do you know, that's what Mike'rosoft said would happen! (Sorry, Mike from Microsoft, couldn't resist the pun :)Timothy
S
0

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
}
Steeplejack answered 30/10, 2020 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.