[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-06-05 (世界標準時間)。"],[[["\u003cp\u003eGoogle Workspace add-ons can extend Gmail functionality by providing a user interface within messages, enabling automation of tasks like retrieving or sending information.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons utilize contextual triggers, defined in their manifest, to execute functions that build the message UI when a user interacts with an email.\u003c/p\u003e\n"],["\u003cp\u003eThese triggers fire when a user opens a message while the add-on is active, prompting a trigger function to create and display the add-on's interface using cards.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can build message add-ons by defining triggers, implementing a contextual trigger function that builds the UI using card objects and accessing message data via the Gmail service with appropriate scopes.\u003c/p\u003e\n"]]],["Google Workspace add-ons enhance Gmail by providing a user interface that automates tasks related to message content. Accessing the add-on's UI triggers a contextual function defined in the add-on's manifest. This trigger function, when fired, constructs the UI using message details fetched via the message ID. The function must then build and return `Card` objects for display. The add-on can define `unconditional` triggers to operate on any message, requiring proper manifest setup and scope activation.\n"],null,["Google Workspace add-ons that extend Gmail can provide\na user interface when the user is reading messages. This allows\nGoogle Workspace add-ons to automate tasks that respond\nto message content, such as displaying, retrieving, or sending out\nadditional information related to the message.\n\nAccessing the add-on message UI\n\nThere are two ways to view an add-on's message UI. The first way is to open\na message while the add-on is already open (for example, when viewing\nthe add-on homepage in the Gmail inbox window). The second way is to\nstart the add-on while viewing a message.\n\nEither case causes the add-on to execute the corresponding\n[*contextual trigger function*](#contextual_trigger_function), defined in the\nadd-on [manifest](/workspace/add-ons/concepts/workspace-manifests#manifest_structure_for_g_suite_add-ons).\nThe trigger also executes if the user switches to a different message while the\nadd-on is still open. The contextual trigger function builds the message UI for\nthat message, which Gmail then displays to the user.\n\nBuilding a message add-on\n\nYou can add message functionality to an add-on by following these general steps:\n\n1. Add the appropriate fields to the add-on script project [manifest](/workspace/add-ons/concepts/workspace-manifests#manifest_structure_for_g_suite_add-ons), including the [scopes](/workspace/add-ons/concepts/workspace-scopes#gmail_add-on_scopes) required for message functionality. Be sure to add a [conditional trigger field](/apps-script/manifest/gmail-addons#contextualtrigger) to the manifest, with a [`unconditional`](/apps-script/manifest/gmail-addons#ContextualTrigger.FIELDS.unconditional) value of `{}`.\n2. Implement a contextual trigger function that builds a message UI when the user selects the add-on in a message.\n3. Implement associated functions needed to respond to the user's UI interactions.\n\nContextual triggers\n\nTo provide users assistance when reading messages,\nGoogle Workspace add-ons can define\na *contextual trigger* in their manifests. When the user\nopens a Gmail message (with the add-on open) that meets the trigger\ncriteria[\\*](#note1) the trigger fires. A fired trigger executes a\n[contextual trigger function](#contextual_trigger_function) that constructs the\nadd-on user interface and returns it for Gmail to display. At that point the\nuser can begin interacting with it.\n\nContextual triggers are defined in your add-on's project\n[manifest](/workspace/add-ons/concepts/workspace-manifests#manifest_structure_for_g_suite_add-ons).\nThe trigger definition tells Gmail which trigger function to fire under which\nconditions. For example, this manifest snippet sets an unconditional trigger\nthat calls the trigger function `onGmailMessageOpen()` when a message is opened: \n\n```text\n{\n ...\n \"addOns\": {\n\n \"common\": {\n ...\n },\n \"gmail\": {\n \"contextualTriggers\": [\n {\n \"unconditional\": {},\n \"onTriggerFunction\": \"onGmailMessageOpen\"\n }\n ],\n ...\n },\n ...\n }\n ...\n}\n```\n\n\n| Currently, the only contextual trigger type available is `unconditional`, which triggers for all emails regardless of content.\n\n\u003cbr /\u003e\n\nContextual trigger function\n\nEvery contextual trigger must have a corresponding *trigger function*\nthat constructs your add-on's user interface. You specify this function in your\nmanifest's [`onTriggerFunction`](/apps-script/manifest/gmail#ContextualTrigger.FIELDS.onTriggerFunction)\nfield. You implement this function to accept an\n[action event object](/workspace/add-ons/concepts/actions#action_event_objects)\nargument and return either a single\n[`Card`](/apps-script/reference/card-service/card) object or an array of\n[`Card`](/apps-script/reference/card-service/card) objects.\n\nWhen a contextual trigger fires for a given Gmail message, it calls this\nfunction and passes it an\n[action event object](/workspace/add-ons/concepts/actions#action_event_objects).\nOften trigger functions use the message ID provided by this event object\nto get the message text and other details using Apps Script's\n[Gmail service](/apps-script/reference/gmail). For example, your trigger\nfunction could extract message content using these functions: \n\n // Activate temporary Gmail scopes, in this case to allow\n // the add-on to read message metadata and content.\n var accessToken = e.gmail.accessToken;\n GmailApp.setCurrentMessageAccessToken(accessToken);\n\n // Read message metadata and content. This requires the Gmail scope\n // https://www.googleapis.com/auth/gmail.addons.current.message.readonly.\n var messageId = e.gmail.messageId;\n var message = GmailApp.getMessageById(messageId);\n var subject = message.getSubject();\n var sender = message.getFrom();\n var body = message.getPlainBody();\n var messageDate = message.getDate();\n\n // Setting the access token with a gmail.addons.current.message.readonly\n // scope also allows read access to the other messages in the thread.\n var thread = message.getThread();\n var threadMessages = thread.getMessages();\n\n // Using this link can avoid the need to copy message or thread content\n var threadLink = thread.getPermalink();\n\n| **Note:** In most cases you must activate [Gmail scopes](/workspace/add-ons/concepts/workspace-scopes#gmail_add-on_scopes) using the access token provided by the event object and the [`GmailApp.setCurrentMessageAccessToken(accessToken)`](/apps-script/reference/gmail/gmail-app#setcurrentmessageaccesstokenaccesstoken) function before using other [Gmail service](/apps-script/reference/gmail) functions.\n\nThe trigger function can then act on this data, extracting the information that\nit needs for the interface. For example, an add-on that summarizes sales\nnumbers can collect sales figures from the message body and organize them for\ndisplay in a [card](/workspace/add-ons/concepts/cards).\n\nThe trigger function must build and return an array of built\n[`Card`](/apps-script/reference/card-service/card)\nobjects. For example, the following builds an add-on with a single card that\njust lists the subject and sender of the message: \n\n function onGmailMessageOpen(e) {\n // Activate temporary Gmail scopes, in this case to allow\n // message metadata to be read.\n var accessToken = e.gmail.accessToken;\n GmailApp.setCurrentMessageAccessToken(accessToken);\n\n var messageId = e.gmail.messageId;\n var message = GmailApp.getMessageById(messageId);\n var subject = message.getSubject();\n var sender = message.getFrom();\n\n // Create a card with a single card section and two widgets.\n // Be sure to execute build() to finalize the card construction.\n var exampleCard = CardService.newCardBuilder()\n .setHeader(CardService.newCardHeader()\n .setTitle('Example card'))\n .addSection(CardService.newCardSection()\n .addWidget(CardService.newKeyValue()\n .setTopLabel('Subject')\n .setContent(subject))\n .addWidget(CardService.newKeyValue()\n .setTopLabel('From')\n .setContent(sender)))\n .build(); // Don't forget to build the Card!\n return [exampleCard];\n }"]]