How to find Office AddIn Host this Word or Excel application using office.js?

I am creating an AddIn office that works in both excel and word applications and on a host basis, if it is a word or excel host, I want to execute different logic. I am using office.js to create an Office Addin.

eg: -

let say type="Excel" // after some logic executed if(type=="Excel") { //run code for excel applications } else { //run code for word applications } 

I tried using a roar: -

  if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) { alert("yes it is excel"); } 

but it does not work when I run it in excel.

I also sent the host to the manifest file

  <Hosts> <Host Name="Document" /> <Host Name="Workbook" /> </Hosts> 

Also, I have a code that changes a lot, I found the following code that does not work for me

 function getHostInfo() { var _requirements = Office.context.requirements; var types = ['Excel', 'Word']; var minVersions = ['1.1', '1.0']; // Start with the highest version // Loop through types and minVersions for (var type in types) { for (var minVersion in minVersions) { // Append "Api" to the type for set name, ie "ExcelApi" or "WordApi" if (_requirements.isSetSupported(types[type] + 'Api', minVersions[minVersion])) { return { type: types[type], apiVersion: minVersions[minVersion] } } } } }; 

thanks

+5
source share
3 answers

Update December 5, 2016. We will soon release an API to detect host and platform information (partly in response to the fact that the _host_info URL paramater parameter, which people unofficially relied on, needed to remove Office Online recently). We also have a temporary workaround pending the upcoming official API. See " In the Excel Online API, OfficeJS no longer passes the host_Info_ parameter for the Excel add-in " for more information.

Note that for many lighting scenarios, you would still be better off using the API set definition. See “ Clean ways to get your environment (for example, the Office version) ” for more information on requirement sets.


if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) should work for you if you are in Excel 2016. This will not work (ie return false ) in 2013.

If you focus on Office 2013 and need a solution only for Word and Excel, you can use the ability to write OpenXML as a distinguishing factor (Word can, Excel cannot). So check out Office.context.requirements.isSetSupported('OoxmlCoercion') . It will return true for Word, false for Excel.

+1
source

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

0
source

Check out the upcoming API, which provides a formal API to get this information.

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

This is the best way to get the information you are looking for rather than relying on the URL query string or session store. Such methods are a little risky, as the main behavior may change without warning. This is always a good use case for published APIs.

0
source

All Articles