如何你想创建一个执行 JavaScript 函数而不是显示 UI 的外接程序命令,执行 JavaScript 函数的键盘快捷方式。那么请使用FunctionFile 元素
<DesktopFormFactor>
<FunctionFile resid="residDesktopFuncUrl" />
<ExtensionPoint xsi:type="PrimaryCommandSurface">
<!-- information about this extension point -->
</ExtensionPoint>
<!-- You can define more than one ExtensionPoint element as needed -->
</DesktopFormFactor>
元素指示的 HTML 文件中的 JavaScript FunctionFile 必须调用 Office.initialize 和定义采用单个参数的命名函数: event 。 这些函数应使用 item.notificationMessages API 向用户指示进度、成功或失败。 此外,它应在完成执行时调用 event.completed。 函数的名称在无 FunctionName UI 按钮的元素中使用。
下面是定义函数的 HTML 文件的一个示例 trackMessage 。
Office.initialize = function () {
doAuth();
}
function trackMessage (event) {
var buttonId = event.source.id;
var itemId = Office.context.mailbox.item.id;
// save this message
event.completed();
}
下面的代码演示如何实现由使用的函数 FunctionName 。
// The initialize function must be run each time a new page is loaded.
(function () {
Office.initialize = function (reason) {
// If you need to initialize something you can do so here.
};
})();
// Your function must be in the global namespace.
function writeText(event) {
// Implement your custom code here. The following code is a simple example.
Office.context.document.setSelectedDataAsync("ExecuteFunction works. Button ID=" + event.source.id,
function (asyncResult) {
var error = asyncResult.error;
if (asyncResult.status === "failed") {
// Show error message.
}
else {
// Show success message.
}
});
// Calling event.completed is required. event.completed lets the platform know that processing has completed.
event.completed();
}