英文:
Is it possible to execute an excel function from an office dialog?
问题
我已经使用yeoman生成器在React TypeScript中构建了一个Excel插件。我的对话框只能通过我创建的自定义选项卡中的按钮来打开。作为开发人员,我想允许用户从对话框中运行一个Excel函数,以更新工作表。
我已经阅读了Office文档,但只找到与身份验证相关的示例。有人可以进一步解释这个吗?
英文:
I have built an excel add-in in react typescript using the yeoman generator. My dialog is only opened through a button found within the custom tab I've created. As a developer, I want to allow a user run an excel function from a dialog that will update the worksheet.
I've read through the office documentation but can only find authentication related examples. Can anyone shed further light on this?
答案1
得分: 1
你可以从插件中调用一些内置的Excel函数。但要在对话框中执行此操作有些棘手。对话框中仅支持isSetSupported
和messageParent
这两个Office.js API。因此,对话框中的按钮不能直接调用workbook.functions
API。您需要做的是,对话框中的按钮发送一条消息回到调用displayDialogAsync
的父JavaScript运行时,该消息标识要调用的函数。父级中的代码处理MessageReceived事件,并调用workbook.functions
API,传递要调用的函数名称。对最终用户而言,看起来好像对话框中的按钮正在运行Excel函数。【参考链接】:这里。
英文:
You can call some of the built-in Excel functions from an add-in. (See here.) But to do it from a dialog is tricky. The only Office.js APIs that are supported in the dialog are isSetSupported
and messageParent
. So, your button in the dialog can't directly call the workbook.functions
API. What you need to do is have the button in the dialog send a message back to the parent JavaScript runtime where displayDialogAsync
was called. The message identifies the function to be called. Code in the parent handles the MessageReceived event and calls the workbook.functions
API, passing the name of the function to be called. To the end user it looks like the button in the dialog is running the Excel function.
答案2
得分: 0
你可以使用Office对话框 API在你的 Office 插件中打开对话框框。有关更多信息,请参阅在 Office 插件中使用 Office 对话框 API。
对话框框中的代码使用messageParent
函数向宿主页面发送字符串消息。该字符串可以是一个单词、句子、XML 数据块、字符串化的 JSON,或者任何可以序列化为字符串或强制转换为字符串的内容。要使用messageParent
方法,对话框框必须首先初始化 Office JavaScript API:
Office.onReady(function() {
// 在此处添加对话框的任何初始化代码。
});
// 用户登录时调用。
function userSignedIn() {
Office.context.ui.messageParent(true.toString());
}
messageParent
函数是仅有的两个可以在对话框框中调用的 Office JS API 之一。宿主页面必须配置以接收消息。您可以通过向displayDialogAsync
的原始调用添加回调参数来实现这一点。回调参数会将处理程序分配给DialogMessageReceived
事件。例如:
let dialog; // 声明对话框为全局变量,以在后续函数中使用。
Office.context.ui.displayDialogAsync('https://www.contoso.com/myDialog.html', {height: 30, width: 20},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
}
);
基本上,从您的消息框实例中,您可以向宿主页面发送消息,宿主页面可以调用一个函数。
英文:
You can use the Office dialog API to open dialog boxes in your Office Add-in. See Use the Office dialog API in Office Add-ins for more information.
Code in the dialog box uses the messageParent
function to send a string message to the host page. The string can be a word, sentence, XML blob, stringified JSON, or anything else that can be serialized to a string or cast to a string. To use the messageParent
method, the dialog box must first initialize the Office JavaScript API:
Office.onReady(function() {
// Add any initialization code for your dialog here.
});
// Called when dialog signs in the user.
function userSignedIn() {
Office.context.ui.messageParent(true.toString());
}
The messageParent
function is one of only two Office JS APIs that can be called in the dialog box. The host page must be configured to receive the message. You do this by adding a callback parameter to the original call of displayDialogAsync
. The callback assigns a handler to the DialogMessageReceived
event. For example:
let dialog; // Declare dialog as global for use in later functions.
Office.context.ui.displayDialogAsync('https://www.contoso.com/myDialog.html', {height: 30, width: 20},
function (asyncResult) {
dialog = asyncResult.value;
dialog.addEventHandler(Office.EventType.DialogMessageReceived, processMessage);
}
);
Basically, from your message box instance you can send a message to the host page where you can call a function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论