4.创建 FunctionFile

如何你想创建一个执行 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();
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容