【问题标题】:google form sends duplicates emails on signal submission谷歌表单在信号提交时发送重复的电子邮件
【发布时间】:2021-10-03 14:37:21
【问题描述】:

我想做一个谷歌表单向提交者发送电子邮件,我使用此代码指令使用应用脚本方法如下...表单(提交)>工作表(响应)>应用脚本>获取文档文件作为电子邮件模板 > 发送电子邮件

我的问题是提交应用程序会重复发送多封电子邮件。不管我做什么 这是脚本的应用程序是从这里获取的

enter link description here

  var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1M2n7iOkl4IeKmuTjU95ibQN6W6UqYtkA9d99fccPd_Y/edit?usp=sharing';
  var EMAIL_SUBJECT = 'title';

  /**
   * Installs a trigger on the Spreadsheet for when a Form response is submitted.
   */
  function installTrigger() {
    ScriptApp.newTrigger('onFormSubmit')
        .forSpreadsheet(SpreadsheetApp.getActive())
        .onFormSubmit()
        .create();
  }

  /**
   * Sends a customized email for every response on a form.
   *
   * @param {Object} e - Form submit event
   */
  function onFormSubmit(e) {
    var responses = e.namedValues;

    // If the question title is a label, it can be accessed as an object field.
    // If it has spaces or other characters, it can be accessed as a dictionary.
    var timestamp = responses.Timestamp[0];
    var email = responses['Email Address'][0].trim();
    var name = responses.Name[0].trim();

    // If there is at least one topic selected, send an email to the recipient.
    var status = 'Email Sent';

    // Append the status on the spreadsheet to the responses' row.
    var sheet = SpreadsheetApp.getActiveSheet();
    var row = sheet.getActiveRange().getRow();
    var column = e.values.length + 1;

    sheet.getRange(row, column).setValue(status);

    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name),
    });
    
    
    Logger.log('status=' + status + '; responses=' + JSON.stringify(responses));
  }

  /**
   * Creates email body and includes the links based on topic.
   *
   * @param {string} name - The recipient's name.
   * @param {string[]} topics - List of topics to include in the email body.
   * @return {string} - The email body as an HTML string.
   */
  function createEmailBody(name) {

    // Make sure to update the emailTemplateDocId at the top.
    var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
    var emailBody = docToHtml(docId);
    emailBody = emailBody.replace(/{{NAME}}/g, name);
    return emailBody;
  }

  /**
   * Downloads a Google Doc as an HTML string.
   *
   * @param {string} docId - The ID of a Google Doc to fetch content from.
   * @return {string} The Google Doc rendered as an HTML string.
   */
  function docToHtml(docId) {
    // Downloads a Google Doc as an HTML string.
    var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' +
              docId + '&exportFormat=html';
    var param = {
      method: 'get',
      headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
      muteHttpExceptions: true,
    };
    return UrlFetchApp.fetch(url, param).getContentText();
  }

我试图这样做。在发送电子邮件之前检查最后一列是否为空。发送后,Email Sent 将失败,我的事情是限制电子邮件重复。但没有。

if(sheet.getRange(row, column).getValue() == "")
{
    sheet.getRange(row, column).setValue(status);

    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name),
    });
}

我试着统计一下它发送了多少封电子邮件。最后一个单元格显示 0,它发送了重复的电子邮件。

if(sheet.getRange(row, column).getValue() == "")
{
    sheet.getRange(row, column).setValue(0);
}
else
{
    sheet.getRange(row, column).setValue(sheet.getRange(row, column).getValue + 1);

    MailApp.sendEmail({
      to: email,
      subject: EMAIL_SUBJECT,
      htmlBody: createEmailBody(name),
    });
}

编辑

使用后...

 ScriptApp.getProjectTriggers().forEach(trigger => Logger.log(trigger.getHandlerFunction() + ' - ' + trigger.getEventType()))

这就是我得到的

【问题讨论】:

  • 检查您的触发器页面,确保您只分配了一个触发器。

标签: forms email google-apps-script google-sheets


【解决方案1】:

正如 Cooper 在评论中提到的,您可能安装了多个触发器。

您可以检查项目的触发器页面以查看重复项,或运行此行。

ScriptApp.getProjectTriggers().forEach(trigger => Logger.log(trigger.getHandlerFunction() + ' - ' + trigger.getEventType()))

如果您有重复的触发器,上面的行将像这样记录:

只需删除重复的邮件,只保留其中的 1 个,这样您的邮件就不会再发送重复的邮件了。

删除触发器页面中的重复项后,只应保留 1 个触发器:

参考:

【讨论】:

  • 你是对的我有重复的提交,但你说的Just remove the duplicate one and your mail should not send duplicate emails anymore.是什么意思。触发器被多次触发。
  • 删除其他重复的触发器。我已经修改了答案以显示如何删除它们。 @AbdullahSalma
  • 嗨@AbdullahSalma,如果我们回答了您的问题,请单击左侧的接受按钮(复选图标)。通过这样做,社区中可能与您有同样担忧的其他人将知道他们的问题可以得到解决。如果您无法使用接受按钮,请随时告诉我。 how to accept answer
猜你喜欢
  • 2021-05-09
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 2019-04-24
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多