Google Apps Scripts 使用撰写同时更新主题和抄送 修改后的脚本:注意:参考:

问题描述

我想打开撰写 UI 并能够同时更新草稿主题/收件人/和抄送,而不是多次操作。

Google 提供给您的示例代码不是开箱即用的,您需要更正错误。这是工作代码

/**
     * 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 subject')
              .setonClickAction(CardService.newAction()
                  .setFunctionName('applyUpdateSubjectAction')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update To recipients')
              .setonClickAction(CardService.newAction()
                  .setFunctionName('applyUpdatetoRecipientsAction')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update Cc recipients')
              .setonClickAction(CardService.newAction()
                  .setFunctionName('applyUpdateCcRecipientsAction')));
      cardSection.addWidget(
          CardService.newTextButton()
              .setText('Update Bcc recipients')
              .setonClickAction(CardService.newAction()
                  .setFunctionName('applyUpdateBccRecipientsAction')));
      return card.addSection(cardSection).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;
    }

当该代码打开时,它看起来像这样的组合 UI 显示

compose ui

不过

这些链接一次只能使用一个关闭屏幕。您必须在多个动作中执行所有操作。我希望它能够一次执行多个操作。

例如,如果我单击“更新主题”,操作会起作用并且撰写 UI 屏幕会关闭,但我不想再次打开加载项来添加抄送电子邮件地址。

add a subject working

解决方法

我相信你的目标如下。

  • 您想在对话框中一键运行 applyUpdateSubjectAction()applyUpdateToRecipientsAction()applyUpdateCcRecipientsAction()applyUpdateBccRecipientsAction() 的这些函数。

这样的话,下面的修改怎么样?

修改后的脚本:

请修改buildComposeCard()如下。

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();
}
  • 在此修改中,您可以在打开的对话框中看到“更新电子邮件”。单击它时,会运行 addUpdateSubjectaddUpdateToRecipientsaddUpdateCcRecipientsaddUpdateBccRecipients

注意:

  • 当您修改 Google Apps 脚本时,请将部署修改为新版本。这样,修改后的脚本就会反映到附加组件中。请注意这一点。

参考: