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

Google Apps Script更新Gmail邮件字段失效及相关功能咨询

问题描述

我正在尝试用Google Apps Script实现一个可更新邮件主题、正文、CC、BCC等字段的功能,但点击按钮后无任何更新效果,不确定问题出在代码本身还是appscript.json配置文件中。此外,还有两个疑问:

  1. 该函数能否更新当前打开的Gmail撰写窗口,且变更无需刷新即可显示?
  2. 是否存在特定函数、操作或API,可通过Google Add-ons、Gmail Add-ons或Gmail API以编程方式打开撰写窗口?
代码实现
/**
 * Compose trigger function that fires when the compose UI is
 * requested. Builds and returns a compose UI for inserting images.
 *
 * @param {event} e The compose trigger event object. Not used in
 *         this example.
 * @return {Card[]}
 */
function startApp(e) {
  return [buildComposeCard()];
}

/**
 * Build a card to display interactive buttons to allow the user to
 * update the subject, and To, Cc, Bcc recipients.
 *
 * @return {Card}
 */
function buildComposeCard() {
  var card = CardService.newCardBuilder();
  var cardSection = CardService.newCardSection().setHeader('Update email');
  cardSection.addWidget(CardService.newTextButton().setText('Update email').setOnClickAction(CardService.newAction().setFunctionName('applyUpdateEmail')));
  return card.addSection(cardSection).build();
}

function applyUpdateEmail() {
  var subject = ['this is a subject'];
  var toRecipients = ['johhny.appleseed@gmail.com'];
  var ccRecipients = ['big.blue@montana.com'];
  var bccRecipients = ['spacer@gmail.com'];

  return CardService.newUpdateDraftActionResponseBuilder()
    .setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction().addUpdateSubject(subject))
    .setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction().addUpdateToRecipients(toRecipients))
    .setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction().addUpdateCcRecipients(ccRecipients))
    .setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction().addUpdateBccRecipients(bccRecipients))
    .build();
}

/**
 * Updates the subject field of the current email when the user clicks
 * on "Update subject" in the compose UI.
 *
 * Note: This is not the compose action that builds a compose UI, but
 * rather an action taken when the user interacts with the compose UI.
 *
 * @return {UpdateDraftActionResponse}
 */
function applyUpdateSubjectAction() {
  // Get the new subject field of the email.
  // This function is not shown in this example.
  var subject = ['this is a subject'];
  var response = CardService.newUpdateDraftActionResponseBuilder()
    .setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction()
      .addUpdateSubject(subject))
    .build();
  return response;
}

/**
 * Updates the To recipients of the current email when the user clicks
 * on "Update To recipients" in the compose UI.
 *
 * Note: This is not the compose action that builds a compose UI, but
 * rather an action taken when the user interacts with the compose UI.
 *
 * @return {UpdateDraftActionResponse}
 */
function applyUpdateToRecipientsAction() {
  // Get the new To recipients of the email.
  // This function is not shown in this example.
  var toRecipients = ['johhny.appleseed@gmail.com'];
  var response = CardService.newUpdateDraftActionResponseBuilder()
    .setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction()
      .addUpdateToRecipients(toRecipients))
    .build();
  return response;
}

/**
 * Updates the Cc recipients  of the current email when the user clicks
 * on "Update Cc recipients" in the compose UI.
 *
 * Note: This is not the compose action that builds a compose UI, but
 * rather an action taken when the user interacts with the compose UI.
 *
 * @return {UpdateDraftActionResponse}
 */
function applyUpdateCcRecipientsAction() {
  // Get the new Cc recipients of the email.
  // This function is not shown in this example.
  var ccRecipients = ['big.blue@montana.com'];
  var response = CardService.newUpdateDraftActionResponseBuilder()
    .setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction()
      .addUpdateCcRecipients(ccRecipients))
    .build();
  return response;
}

/**
 * Updates the Bcc recipients  of the current email when the user clicks
 * on "Update Bcc recipients" in the compose UI.
 *
 * Note: This is not the compose action that builds a compose UI, but
 * rather an action taken when the user interacts with the compose UI.
 *
 * @return {UpdateDraftActionResponse}
 */
function applyUpdateBccRecipientsAction() {
  // Get the new Bcc recipients of the email.
  // This function is not shown in this example.
  var bccRecipients = ['spacer@gmail.com'];
  var response = CardService.newUpdateDraftActionResponseBuilder()
    .setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction()
      .addUpdateBccRecipients(bccRecipients))
    .build();
  return response;
}
appscript.json配置
{
  "timeZone": "Asia/Kolkata",
  "dependencies": {},
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
     "https://www.googleapis.com/auth/gmail.addons.current.action.compose",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/script.locale",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/gmail.compose",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://mail.google.com/", 
    "https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
  "addOns": {
    "common": {
      "name": "Example 1",
      "logoUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
      "useLocaleFromApp": true,
      "homepageTrigger": {
        "runFunction": "startApp",
        "enabled": true
      },
      "universalActions": [
        {
          "label": "Google",
          "openLink": "www.google.com"
        }
      ]
    },
    "gmail": {
      "contextualTriggers": [
        {
          "unconditional": {},
          "onTriggerFunction": "startApp"
        }
      ],
      "composeTrigger": {
        "selectActions": [
          {
            "text": "Login",
            "runFunction": "startApp"
          }
        ],
        "draftAccess": "METADATA"
      }
    }
  }
}
界面截图

Compose UI
Email Draft

问题排查与解答

1. 点击按钮无更新的原因及修复

配置文件问题

appscript.json中composeTrigger.draftAccess设置为METADATA,该权限仅允许读取草稿元数据,无法修改草稿内容。需改为WRITE权限:

"composeTrigger": {
  "selectActions": [
    {
      "text": "Login",
      "runFunction": "startApp"
    }
  ],
  "draftAccess": "WRITE"
}

代码逻辑问题

  • addUpdateSubject方法接收的是单个字符串,而非数组。当前传的是['this is a subject'],需改为直接传字符串:
    .setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction().addUpdateSubject('this is a subject'))
    
  • 收件人更新方法:addUpdateToRecipients/addUpdateCcRecipients/addUpdateBccRecipients每次只能添加单个收件人,若要设置多个收件人,应使用setUpdateToRecipients/setUpdateCcRecipients/setUpdateBccRecipients并传入数组:
    function applyUpdateEmail() {
      var subject = 'this is a subject';
      var toRecipients = ['johhny.appleseed@gmail.com'];
      var ccRecipients = ['big.blue@montana.com'];
      var bccRecipients = ['spacer@gmail.com'];
    
      return CardService.newUpdateDraftActionResponseBuilder()
        .setUpdateDraftSubjectAction(CardService.newUpdateDraftSubjectAction().addUpdateSubject(subject))
        .setUpdateDraftToRecipientsAction(CardService.newUpdateDraftToRecipientsAction().setUpdateToRecipients(toRecipients))
        .setUpdateDraftCcRecipientsAction(CardService.newUpdateDraftCcRecipientsAction().setUpdateCcRecipients(ccRecipients))
        .setUpdateDraftBccRecipientsAction(CardService.newUpdateDraftBccRecipientsAction().setUpdateBccRecipients(bccRecipients))
        .build();
    }
    

2. 疑问解答

疑问1:能否更新当前打开的撰写窗口且无需刷新?

可以。使用CardService.newUpdateDraftActionResponseBuilder()构建的响应,专门用于实时更新当前打开的Gmail撰写窗口,修改内容会立即显示,无需刷新页面。修正上述代码和配置后即可实现该效果。

疑问2:如何编程打开撰写窗口?

有两种常用方式:

  • Gmail Add-ons方式:使用CardService.newComposeActionResponseBuilder()构建响应,可直接打开新的撰写窗口,并预设主题、收件人、正文等内容:
    function openComposeWindow() {
      return CardService.newComposeActionResponseBuilder()
        .setToRecipients(['example@test.com'])
        .setSubject('预设主题')
        .setBody('预设正文内容')
        .build();
    }
    
  • Gmail API方式:调用users.drafts.create接口创建草稿,再通过草稿的id生成打开链接;或者直接构造Gmail撰写URL,通过链接打开撰写窗口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 04:57:02