How to find the Office AddIn Host it is a Word application or Excel using office.js?

Deadly 提交于 2019-12-04 06:52:31
Michael Zlatkovsky - Microsoft

Update Dec 5, 2016: We will soon be releasing an API to detect the host and platform information (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 more info.

Note that for many light-up scenarios, you would still be better off using API Set detection. See "Neat ways to get environment (i.e. Office version)" for more information on requirement sets.


The if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) should work for you, IF you are in Excel 2016. It will not work (i.e., return false) in 2013.

If you are targeting Office 2013, and need a solution just for Word & Excel, you can use the ability to write OpenXML as a distinguishing factor (Word can, Excel can't). So check for Office.context.requirements.isSetSupported('OoxmlCoercion'). It will return true for Word, false for Excel.

foilage

You can always inspect the location.search object - should return a string like ?_host_Info=Word|Win32|16.01|en-US.

Note the upcoming API that provides a formal API to get such information.

Specification link: https://github.com/OfficeDev/office-js-docs/tree/ContextAdditions_OpenSpec

This is a better way to get the information you are looking for rather than relying on URL query string or session storage. Such methods are bit risky as underlying behavior could change without warning. It is always a good option to use published APIs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!