我正在创建一个 Office AddIn,它可以在 Excel 和 Word 应用程序中使用,并且基于主机(如果它是 Word 或 Excel 主机,我想执行不同的逻辑)。我正在使用office.js 创建office Addin。
例如 :-
let say type="Excel" // after some logic executed
if(type=="Excel")
{
//run code for excel applications
}
else
{
//run code for word applications
}
我尝试使用以下内容:-
if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
alert("yes it is excel");
}
但当我在 excel 中运行它时它不起作用。
我也已在清单文件中发送了主机
<Hosts>
<Host Name="Document" />
<Host Name="Workbook" />
</Hosts>
我还得到了一些代码更改,做了很多谷歌搜索,我发现了下面的代码
这对我不起作用
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]
}
}
}
}
};
谢谢
2016年12月5日更新:我们即将发布一个API来检测主机和平台信息(部分是为了回应以下事实:_host_info
人们非正式地依赖的 URL 参数最近需要在 Office Online 中删除)。我们还有一个临时解决方法,以应对即将推出的官方 API。See "在 Excel Online 中,OfficeJS API 不再将 host_Info_ 参数传递给 Excel 加载项 https://stackoverflow.com/questions/40688057/in-excel-online-officejs-api-is-not-passing-the-host-info-parameter-anymore-to/40963500#40963500”了解更多信息。
请注意,对于许多点亮场景,您仍然最好使用 API Set 检测。看 ”获取环境的巧妙方法(即 Office 版本) https://stackoverflow.com/questions/32840459/neat-ways-to-get-environment-i-e-office-version“了解有关要求集的更多信息。
The if (Office.context.requirements.isSetSupported('ExcelApi', '1.1'))
如果您使用的是 Excel 2016,则应该适合您。它不会工作(即返回false
)于2013年。
如果您的目标是 Office 2013,并且需要一个仅适用于 Word 和 Excel 的解决方案,您可以使用编写 OpenXML 的功能作为区分因素(Word 可以,Excel 不能)。所以检查Office.context.requirements.isSetSupported('OoxmlCoercion')
。对于 Word,它将返回 true,对于 Excel,它将返回 false。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)