[英]How to determine if an Office Add-in is running under Excel or Excel Online?
I'm writing an Office Add-in (formerly, Apps for Office).我正在编写 Office 加载项(以前称为 Office 应用程序)。 I'm using office.js
and in some point of code I want to check if the app is running in excel (desktop software) or running on the web (Excel Online)我正在使用office.js
并且在某些代码点中我想检查应用程序是在 excel(桌面软件)中运行还是在网络上运行(Excel Online)
Something like:就像是:
if (Office.IsRunningOnWeb){
// Do something.
}
@Mehrandvd, if I may ask, what do you need this distinction for (eg,. what would you do different based on knowing that you're in Excel Online vs. the Desktop)? @Mehrandvd,如果我可以问,你需要这种区别做什么(例如,根据知道你在 Excel Online 和桌面上,你会做什么不同的)? I work on the Office.js APIs, so I'm happy to channel your feedback to my team, if you can provide some specifics.我在 Office.js API 上工作,所以如果你能提供一些细节,我很高兴将你的反馈传递给我的团队。
If you need this distinction for feature detection, I would recommend checking the API Requirement sets instead, via a new (but back-ported to all endpoints) API, Office.context.requirements.isSetSupported(name, version)
.如果您需要这种区别来进行功能检测,我建议您通过新的(但向后移植到所有端点)API Office.context.requirements.isSetSupported(name, version)
来检查 API 要求集。 Please see my answer at Neat ways to get environment (ie Office version) .请参阅我在Neat 获取环境的方式(即 Office 版本)中的回答。
If it's due to some API differences you're seeing between the Excel desktop vs Online versions, the goal is for the APIs to behave the same across endpoints, so it may be a bug.如果这是由于您在 Excel 桌面版与在线版之间看到的某些 API 差异,目标是让 API 在端点之间表现相同,因此这可能是一个错误。 If you let me know the specifics, I can follow up.如果你告诉我具体情况,我可以跟进。
Re.关于。 the answer mentioned by @Afshin -- it may work, but just be aware that it's not public API but rather the internal workings that you're testing against, so there's a chance that this approach would stop working in the future... The only publically-exposed namespace is Office
(and, with the new Excel and Word APIs released in Sept. 2015 , also Excel
and Word
and OfficeExtension
). @Afshin 提到的答案 - 它可能有效,但请注意它不是公共 API,而是您正在测试的内部工作,因此这种方法有可能在未来停止工作......唯一公开的命名空间是Office
(并且,随着2015 年 9 月发布的新 Excel 和 Word API ,还有Excel
和Word
以及OfficeExtension
)。
Hope this helps!希望这可以帮助!
~ Michael Zlatkovsky ~迈克尔·兹拉特科夫斯基
Developer on Office Extensibility team, MSFT MSFT Office 扩展性团队的开发人员
PS: Please use the office-js
tag for tagging these sorts of questions in the future; PS:以后此类问题请使用office-js
标签; this is the stackoverflow tag that the Office Extensibility team at Microsoft actively looks at.这是 Microsoft 的 Office 扩展性团队积极关注的 stackoverflow 标记。
You can use document type:您可以使用文档类型:
if (Microsoft.Office.WebExtension.context.document instanceof OSF.DDA.ExcelWebAppDocument) {
//Your app running on the web
}
if (Microsoft.Office.WebExtension.context.document instanceof OSF.DDA.ExcelDocument) {
//Your app running in excel
}
The question is how do you do it, not why would you want to.问题是你怎么做,而不是你为什么要这么做。 There are numerous reasons why you might want to differentiate.您可能想要区分的原因有很多。 Put in this check as follows:把这个检查如下:
if (window.top == window) {
//the add-in is not running in Excel Online
}
else
{
//the add-in is running in Excel online
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.