Office-js 从 Ribbon/FunctionFile.js 打开一个新的 Window

Office-js Open a New Window from the Ribbon/FunctionFile.js

我无法从功能区打开 window。我声明了我的按钮,它的 onclick 触发了在函数-file.js 中声明的函数。一切正常。但是,它不会执行 window.open。我可以理解可能存在一些 CORS 问题。所以我测试了几件事。首先,我在

中声明了网站 URL
<AppDomains><AppDomain>Website-URL</AppDomain></AppDomains>

没有任何改变。所以我试着在本地调试。当我 运行 本地主机上的加载项并调用托管在网络服务器上的网站(可能会出现 CORS 问题,因此这似乎不是问题所在)时,它似乎可以正常工作。所以我做了以下事情:

var winHandle = window.open("https://website");
if (winHandle == null) {
    console.log('Window was blocked');
    Office.context.ui.displayDialogAsync('https://website/openWebsite.html');
}

上面的代码使用 displayDialog-api 以防无法打开 window,它会打开一个站点,然后提供一个按钮来触发 window.open。因为如果我只是设置新位置,它会给我一个弹出警告。并尝试使用 VS-Debug 功能进行此操作。它没有打开 window,但它打开了对话框,所以我终于可以从对话框中打开 window。但是,当 Manifest.xml 被旁加载时,它不起作用。

有任何建议可以让 window.open 从函数文件或至少显示对话框中工作吗?


已编辑: 正如第二个答案所说:它是正确的,first 页面确实必须来自同一来源。然而,我遇到的是一个子域调用,侧载加载项无法执行。在 Visual Studios 调试模式下,他只给了我一个 CORS 警告并加载了它。

原创 哦,顺便说一句,dialog-api 文档说 displayDialog 不应该在第一次调用时离开域。但是我直接调用

好像没问题
Office.context.ui.displayDialogAsync('https://someOtherDomain/website');

我想您只需要在 AppDomains 中指定域即可。

感谢您的帮助。

函数旨在在没有 UI 的情况下运行。如果您需要显示信息或与用户交互,您需要使用 ShowTaskpane action。您可以将此操作指向应用中的任何页面。通常对于这种情况,我们会创建一个页面来执行加载,然后显示结果。

根据 dialog API docs,您在对话框中打开的 第一个 页面必须在同一域中。同一段文档还明确提到从函数文件调用。

  • The URL uses the HTTPS protocol. This is mandatory for all pages loaded in a dialog box, not just the first page loaded.
  • The domain is the same as the domain of the host page, which can be the page in a task pane or the function file of an add-in command. This is required: the page, controller method, or other resource that is passed to the displayDialogAsync method must be in the same domain as the host page.

After the first page (or other resource) is loaded, a user can go to any website (or other resource) that uses HTTPS. You can also design the first page to immediately redirect to another site.

在我加载 Office.js 并在对话框页面中调用 Initialize 之前,我尝试从一个函数加载对话框时得到了非常奇怪的结果(这被记录为使您能够将消息发送回父级,但未按要求提及)。