You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Office JS:Outlook动作能否触发任务窗格及相关问题咨询

Outlook插件Action相关问题解答

问题1:Outlook中的Action是否支持点击事件?能否执行TypeScript代码?

  • Outlook中的Action完全支持点击事件,且可以执行TypeScript代码。
  • 实现方式:在插件的manifest.xml中,将Action类型设置为ExecuteFunction并指定对应函数名;在commands.ts中定义该函数,函数内部即可编写TS逻辑。
  • 示例manifest配置片段:
<Action xsi:type="ExecuteFunction">
  <FunctionName>action</FunctionName>
</Action>
  • 你当前代码里的action函数就是对应的事件处理器,点击Action时会触发该函数执行。

问题2:如何通过Action点击打开插件任务窗格?

你调用Office.addin.showAsTaskpane()失败,需修正几个关键问题:

  1. 调用时机错误:event.completed()会结束命令函数的执行上下文,必须在它之前调用showAsTaskpane(),否则后续代码可能无法正常运行。
  2. 未处理Promise错误:showAsTaskpane()返回Promise,添加catch捕获错误可快速排查问题。
  3. API版本与配置检查:确保Office JS API版本≥1.13,Outlook客户端为365/2019及以上版本;同时manifest中需正确配置任务窗格的SourceLocation。

修改后的action函数示例:

function action(event: Office.AddinCommands.Event) {
  const successMessage: Office.NotificationMessageDetails = {
    type: Office.MailboxEnums.ItemNotificationMessageType.InformationalMessage,
    message: "Performed action. - Click",
    icon: "Icon.80x80",
    persistent: true,
  };

  // 优先尝试打开任务窗格
  Office.addin.showAsTaskpane()
    .then(() => {
      Office.context.mailbox.item.notificationMessages.replaceAsync("action", successMessage);
    })
    .catch((error) => {
      const errorMessage = {
        type: Office.MailboxEnums.ItemNotificationMessageType.ErrorMessage,
        message: `打开任务窗格失败: ${error.message}`,
        icon: "Icon.80x80",
        persistent: true,
      };
      Office.context.mailbox.item.notificationMessages.replaceAsync("error", errorMessage);
    })
    .finally(() => {
      // 无论结果如何,必须调用event.completed()结束命令
      event.completed();
    });
}

另外注意:Office.onReady()中调用showAsTaskpane()无实际作用,因为commands.ts仅在触发命令时加载,插件启动时不会执行这段代码。

内容的提问来源于stack exchange,提问作者AndyPet74

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:32:49