Google Apps 脚本事件对象在我的 Gmail 插件中未定义

Google Apps Script event object is undefined in my Gmail add-on

问题是我的函数接收到一个未定义的事件对象。你能告诉我有什么问题吗?

函数如下:

function onGmailMessageOpen(event) {
  const { accessToken, messageId } = event.commonEventObject;
  return buildCard();
}

这是 appsscript.json 清单:

{
  "timeZone": "Europe/Kiev",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Gmail",
        "version": "v1",
        "serviceId": "gmail"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://mail.google.com/",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
    "https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis.com/auth/gmail.metadata"
  ],
  "gmail": {
    "name": "Contacts",
    "logoUrl": "https://lh4.googleusercontent.com/dfdvUXkVaB8yVTsrnuFagTy9YDvEPz3jqJWhFNpxKbbJhq7kdOTkwfoSEtn4VBOb9tZ_B6DT3Q-L4wU5Cw-FCcYY4fISn29OkcBOCwp69q5GxI4AM2WXHHk6kaQtgA",
    "contextualTriggers": [
      {
        "unconditional": {},
        "onTriggerFunction": "onGmailMessageOpen"
      }
    ],
    "primaryColor": "#547dbf"
  },
  "runtimeVersion": "V8",
  "webapp": {
    "executeAs": "USER_ACCESSING",
    "access": "ANYONE"
  }
}

函数声明 function onGmailMessageOpen(event) {/**/} 表明您的函数旨在接收正确填充的 event 对象。

不要运行直接在脚本编辑器中编写代码。如果这样做,event 对象将无法正确填充,从而导致您提到的错误。

您应该做的是让 Google 的代码在用户打开 UI 中的消息时调用该函数。像这样调用时,event object 将被正确填充。