简体   繁体   English

如何确定 Office 加载项是在 Excel 还是 Excel Online 下运行?

[英]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 ,还有ExcelWord以及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
}

Inspired by Sturb 's answer, the following works with ExcelApi 1.10 & 1.12Sturb回答的启发,以下适用于 ExcelApi 1.10 & 1.12

if (window.top.window == window) {
  // Add-in is running in Excel desktop
} else {
  // Add-in is running in Excel online
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用office.js加载项在在线Excel右任务窗格上显示/隐藏关闭按钮 - How to show/hide the close button on online excel right task pane using office.js add-in 名称 scope 在 Office Web 上运行 Excel 加载项时出现问题 - Name scope issue when running Excel add-in on Office Web Excel Office加载项Office.context.auth未定义 - Excel Office Add-In Office.context.auth is undefined Office.JS:如何使用Office 2016 OnPrem加载Word / Excel任务窗格加载项? - Office.JS: How to sideload Word/Excel Taskpane add-in with Office 2016 OnPrem? 是否可以使用办公室加载项复制excel公式? - Is it possible to copy excel formula using an office add-in? Office.js 加载项:在 Excel 2016 中插入图像/图片 - Office.js Add-in: Insert image/picture in Excel 2016 在 Excel Office 加载项中使用 takepane.js 时遇到问题 - Having trouble using takepane.js in an Excel Office Add-in Microsoft office.js Excel 插件 - 如何为折线图中的数据系列实现箭头样式? - Microsoft office.js Excel add-in - How to implement arrow style for a data series in a line chart? 如何使用office script excel online在office 365中发送电子邮件 - How to send email in office 365 using office script excel online 在SharePoint Online上自动打开Office加载项 - Automatically open Office add-in on SharePoint Online
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM