如何使用office.js找到Office AddIn Host它是Word应用程序还是Excel?

Pra*_*aba 6 excel add-in office-js

我正在创建一个办公室AddIn,它可以在excel和word应用程序中工作,如果它是一个单词或excel主机我想要执行不同的逻辑.我正在使用office.js创建办公室Addin.

例如 :-

let say type="Excel" // after some logic executed 

if(type=="Excel")
 {
//run code for excel applications 
}
else
{
//run code for word applications
}
Run Code Online (Sandbox Code Playgroud)

我试过用下面的声音: -

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

但是当我在excel中运行它时,它无法正常工作.

我也在清单文件中发送了主机

 <Hosts>
     <Host Name="Document" />
    <Host Name="Workbook" />
  </Hosts>
Run Code Online (Sandbox Code Playgroud)

还有一些代码改变做了很多谷歌搜索我找到了波纹管代码,这对我不起作用

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, i.e. "ExcelApi" or "WordApi"
            if (_requirements.isSetSupported(types[type] + 'Api', minVersions[minVersion])) {
                return {
                    type: types[type],
                    apiVersion: minVersions[minVersion]
                }
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

谢谢

Mic*_*oft 1

2016 年 12 月 5 日更新:我们很快将发布一个 API 来检测主机和平台信息(部分是为了响应_host_info人们非正式依赖的 URL 参数,最近需要从 Office Online 中删除)。我们还有一个临时解决方法,以应对即将推出的官方 API。有关详细信息,请参阅“在 Excel Online 中,OfficeJS API 不再将 host_Info_ 参数传递给 Excel 加载项”。

请注意,对于许多点亮场景,您仍然最好使用 API Set 检测。有关要求集的更多信息,请参阅“获取环境(即 Office 版本)的巧妙方法”。


如果您使用的是 Excel 2016,则if (Office.context.requirements.isSetSupported('ExcelApi', '1.1'))应该适合您。在 2013 年它将不起作用(即返回false)。

如果您的目标是 Office 2013,并且需要一个仅适用于 Word 和 Excel 的解决方案,您可以使用编写 OpenXML 的功能作为区分因素(Word 可以,Excel 不能)。所以检查一下Office.context.requirements.isSetSupported('OoxmlCoercion'). 对于 Word,它将返回 true,对于 Excel,它将返回 false。