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.
}
Paul B.
  • 2,394
  • 27
  • 47
mehrandvd
  • 8,806
  • 12
  • 64
  • 111

4 Answers4

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.

Community
  • 1
  • 1
  • 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? – mehrandvd Oct 01 '15 at 10:07
  • 1
    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. – Michael Zlatkovsky - Microsoft Oct 02 '15 at 01:16
  • 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 -> http://stackoverflow.com/questions/33321407/how-can-office-add-in-task-pane-not-display-by-default – zumalifeguard Oct 24 '15 at 17:52
  • @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?). – Michael Zlatkovsky - Microsoft Oct 26 '15 at 03:59
  • @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. – zumalifeguard Oct 27 '15 at 04:21
  • @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. – Michael Zlatkovsky - Microsoft Oct 27 '15 at 17:19
  • @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? – alek kowalczyk Nov 05 '16 at 02:52
  • @alekkowalczyk, let me bring this back to the team for discussion. – Michael Zlatkovsky - Microsoft Nov 12 '16 at 05:15
  • 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. – Space Bear Aug 21 '20 at 12:37
  • 1
    @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. – Michael Zlatkovsky - Microsoft Aug 21 '20 at 17:13
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
}
Sturb
  • 71
  • 4
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
                                }
Afshin Alizadeh
  • 218
  • 3
  • 10
  • No longer works: `OSF.DDA` has no key named `ExcelWebAppDocument`, only `ExcelDocument` (the same for Word and PowerPoint). – skozin May 01 '16 at 21:44
  • @skozin Hey, what do you know, that's what Mike'rosoft [said](https://stackoverflow.com/a/32871402/1739000) would happen! (Sorry, Mike from Microsoft, couldn't resist the pun :) – NH. Jul 10 '17 at 20:02
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
}
Alex Monkey
  • 1,427
  • 1
  • 14
  • 16