UPDATE: The code outlined in this post will no longer work with Office Online. See this post for more information: http://stackoverflow.com/a/32851938/678505. Specifically, this block:
Update Dec 5, 2016: We will soon be releasing an API to detect the platform info (partially in response to the fact that the
_host_info
URL paramater, which folks had unofficially relied on, needed to be recently removed for Office Online). We also have a temporary workaround in anticipation of the forthcoming official API. See “In Excel Online, OfficeJS API is not passing the host_Info_ parameter anymore to Excel Add-In” for information on the API and workaround.
Original post:
In case you missed the news, there was an update to the Office API’s. Version 1.2 was announced at //build and released 2 weeks ago. This API enhancement gets us closer to parity with the native object models used in VBA and VSTO. One interesting aspect of the new Office add-ins (formally known as Apps for Office) is how you can use the same codebase across all the applications. However, as the API becomes richer they are also becoming more product specific. Meaning, there is now the Excel.run() and Word.run() commands (PowerPoint is a little lagging in this area, but it is supposedly on the horizon). These allow you to do Excel Workbook (or Word document) context specific commands. And there is a lot of other goodies (like promises). But, I digress…
I was working on a proof of concept for my customer when I found that I needed to use the same codebase for a lot of the same work, but in some cases I needed to do something specific in Word and in another, something specific in Excel. I came up with the following function that I placed in my App.js file:
[code lang=”javascript” collapse=”true” title=”click to expand if the docs.com embedding below is not visible.”]
var current;
app.hostTypes = { Word :"Word", PowerPoint :"PowerPoint", Excel :"Excel"};
app.getHost = function () {
if (current == null) {
if (Office.context.requirements.isSetSupported(‘WordApi’)) {
current = app.hostTypes.Word;
} else if (Office.context.requirements.isSetSupported(‘ExcelApi’)) {
current = app.hostTypes.Excel;
} else {
var host = $.urlParam("_host_Info");
if (host.toLowerCase().indexOf("word",0) >= 0) {
current = app.hostTypes.Word;
} else if (host.toLowerCase().indexOf("excel",0) >= 0) {
current = app.hostTypes.Excel;
} else {
current = app.hostTypes.PowerPoint;
}
}
return current;
}
else {
return current;
}
};
[/code]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var current; | |
app.hostTypes = { Word :"Word", PowerPoint :"PowerPoint", Excel :"Excel"}; | |
app.getHost = function () { | |
if (current == null) { | |
if (Office.context.requirements.isSetSupported('WordApi')) { | |
current = app.hostTypes.Word; | |
} else if (Office.context.requirements.isSetSupported('ExcelApi')) { | |
current = app.hostTypes.Excel; | |
} else { | |
var host = $.urlParam("_host_Info"); | |
if (host.toLowerCase().indexOf("word",0) >= 0) { | |
current = app.hostTypes.Word; | |
} else if (host.toLowerCase().indexOf("excel",0) >= 0) { | |
current = app.hostTypes.Excel; | |
} else { | |
current = app.hostTypes.PowerPoint; | |
} | |
} | |
return current; | |
} | |
else { | |
return current; | |
} | |
}; |
To use this you would do this for Word:
[code language=”javascript”]
/// WORD
if (app.getHost() == app.hostTypes.Word) {
// Word specific code where
}
[/code]
Or, this for Excel:
[code language=”javascript”]
/// EXCEL
if (app.getHost() == app.hostTypes.Excel) {
// Excel specific code here
}
[/code]