简体   繁体   中英

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

The problem is that my function receives an undefined event object. Can you tell me what's the problem?

Here's the function:

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

Here's the appsscript.json manifest:

{
  "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"
  }
}

The function declaration function onGmailMessageOpen(event) {/**/} tells that your function is designed to receive a properly populated event object.

Do not run the code directly in the script editor. If you do, the event object will not be properly populated, causing the error you mention.

What you should do is let Google's code call the function when the user opens a message in the UI. When called like this, the event object will be properly populated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM