如何使用office.js查找Office AddIn Host,它是Word应用程序还是Excel? [英] How to find the Office AddIn Host it is a Word application or Excel using office.js?
问题描述
我正在创建一个可在excel和word应用程序中工作的office加载项,并且基于主机(如果它是word或excel主机),我想执行不同的逻辑.我正在使用office.js创建Office Addin.
I am creating a office AddIn which works in both excel and word applications and based on host if it is a word or excel host i want to execute different logic. I am using office.js to create office Addin.
例如:-
let say type="Excel" // after some logic executed
if(type=="Excel")
{
//run code for excel applications
}
else
{
//run code for word applications
}
我尝试使用以下风箱:-
I have tried to use the bellow:-
if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
alert("yes it is excel");
}
但是当我在excel中运行它时它不起作用.
but it is not working when i run it in excel.
我也在清单文件中发送了主机
I have sent the host in manifest file also
<Hosts>
<Host Name="Document" />
<Host Name="Workbook" />
</Hosts>
我也做了一些谷歌搜索,做了一些修改,发现了下面的代码 这对我不起作用
also I got some code alter doing a lot of googling I found the bellow code which is not working 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, 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方面,我们还有一个临时的解决方法. 请参阅"
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.
请注意,对于许多点亮情况,使用API集检测仍然会更好.请参阅"获得环境的简洁方法(例如Office版本) 以获取有关需求集的更多信息.
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.
if (Office.context.requirements.isSetSupported('ExcelApi', '1.1'))
应该可以为您工作,如果您使用的是Excel2016.则在2013年将不起作用(即返回false
).
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.
如果您的目标是Office 2013,并且仅需要Word& Excel,您可以使用编写OpenXML的功能作为区别因素(Word可以,Excel不能).因此,请检查Office.context.requirements.isSetSupported('OoxmlCoercion')
.对于Word,它将返回true,对于Excel,将返回false.
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.
这篇关于如何使用office.js查找Office AddIn Host,它是Word应用程序还是Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!