英文:
Dialog boxes when converting JavaScript Office add-ins to installable add-ins
问题
我们有一个JavaScript Office插件。我们正在通过VSTO和WebView2在其上创建一个可安装和可执行的插件。大多数功能在没有任何问题的情况下直接工作。但似乎Office对话框API不起作用。
例如,在我们的插件中有以下代码,在JavaScript Office插件中可以正常工作。
Office.context.ui.displayDialogAsync(
`https://www.ourfrontend.com/auth/google`,
{ height: 90, width: 60 },
asyncResult => {
const dialog = asyncResult.value;
dialog.addEventHandler(
Office.EventType.DialogMessageReceived,
//tslint:disable-next-line:no-any
async (arg: any) => {
if (arg.message === 'getUser') {
const token = await getSignTokenLocal();
resolve(payloadFromToken(token));
}
if (arg.message == 'close') {
dialog.close();
}
},
);
},
);
然而,当转换为VSTO时,相同的代码会引发错误:Error: TypeError: Cannot read properties of undefined (reading 'displayDialogAsync')
。
对话框框或类似的控件是插件中的重要元素。我想要实现的是:
- 在任务窗格中点击按钮可以启动一个窗口,窗口位于任务窗格之外,我们可以导航到窗口的特定页面。
- 在窗口上进行操作后,它会触发任务窗格的回调。
有人知道如何在将JavaScript Office插件转换为可安装插件时实现这一点吗?
英文:
We have a JavaScript Office add-in. We are making an installable and executable add-in on top of it by VSTO and WebView2. Most features work directly without any problem. But it seems that Office dialog API does not work.
For instance, we have the following code in our add-in, which works well in JavaScript Office add-in.
Office.context.ui.displayDialogAsync(
`https://www.ourfrontend.com/auth/google`,
{ height: 90, width: 60 },
asyncResult => {
const dialog = asyncResult.value;
dialog.addEventHandler(
Office.EventType.DialogMessageReceived,
//tslint:disable-next-line:no-any
async (arg: any) => {
if (arg.message === 'getUser') {
const token = await getSignTokenLocal();
resolve(payloadFromToken(token));
}
if (arg.message == 'close') {
dialog.close();
}
},
);
},
);
However, when converting to VSTO, the same code raises an error: Error: TypeError: Cannot read properties of undefined (reading 'displayDialogAsync')
.
Dialog boxes or similar controls are important elements in add-ins. What I would like to realize is
- clicking on a button in the taskpane could launch a window outside the taskpane, and we could navigate to certain pages in the window.
- After manipulations on the window, it triggers a callback to the taskpane.
Does anyone know how to achieve this when converting a JavaScript Office add-in to installable add-ins?
答案1
得分: 1
displayDialogAsync
方法是 Office JavaScript API(OfficeJS)的一个特性,在处理隔离的 WebView2 实例时不存在。
对于 VSTO 插件,您需要使用 Windows Forms 或 WPF 技术创建对话框,并为其提供回调以将 Web 视图中使用的代码连接起来。
如果您仅考虑在向用户显示 Web 视图实例时显示对话框,可以考虑使用标准的 JS 技术。
在这种情况下,您需要在 VSTO 插件外部显示一个对话框,而不是在 WebView2 浏览器实例中。Winforms 或 WPF 可以帮助完成这些任务。
学习如何与 WebView2 浏览器实例进行双向通信。这将是代码插件功能 - 在 VSTO 插件中实现用于 WebView2 实例中的 Web 页面事件的事件处理程序。
英文:
The displayDialogAsync
method is a feature of the Office JavaScript API (OfficeJS) which is absent when you deal with a WebView2 instance isolated.
In case of VSTO add-ins you need to create a dialog box using Windows Forms or WPF technologies and provide callbacks to them to glue the code used in the web view.
If you consider only displaying a dialog box when a web view instance is displayed to a user you may consider using standard JS techniques.
I've noticed the following new questions after answering to the original post:
> clicking on a button in the taskpane could launch a window outside the taskpane, and we could navigate to certain pages in the window.
You need to display a dialog box from your VSTO add-in outside of the WebView2 browser instance in that case. Winforms or WPF can help with such tasks.
> After manipulations on the window, it triggers a callback to the taskpane.
Learn how to communicate with a webview2 browser instance back and forth. That will be the code add-in functionality - implement event handlers in your VSTO add-in for web page events in the WebView2 instance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论