防止重复的电子邮件Google Spreadsheets脚本

时间:2020-06-17 09:15:55

标签: javascript google-apps-script google-sheets gmail

我目前能够使用Google Spreadsheets脚本发送电子邮件。但是我的触发器和条件是否不能阻止电子邮件按我的意愿发送:

这是我的代码: '''

function myFunction() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("MASTER");

  const h3 = 'SPP Proyek JIS Tanggal xx dari xxx';
  const headers = ws.getRange("A2:M2").getValues();
  const item = headers[0][4];
  const spec = headers[0][5];
  const sat = headers[0][6];
  const qty = headers[0][7];
  const price = headers[0][8];
  const total = headers[0][9];
  const tujuan = headers[0][10];

  const lr = ws.getLastRow();

  const tableRangeValues = ws.getRange(3, 5,lr-2,7).getDisplayValues();
  const trigger = ws.getRange(3, 1,lr-2).getValues();
  const statusEmail = ws.getRange(3, 13,lr-2).getValues();

  const htmlTemplate = HtmlService.createTemplateFromFile("Email");
  htmlTemplate.h3 = h3;
  htmlTemplate.headers = headers;
  htmlTemplate.item = item;
  htmlTemplate.spec = spec;
  htmlTemplate.sat = sat;
  htmlTemplate.qty = qty;
  htmlTemplate.price = price;
  htmlTemplate.total = total;
  htmlTemplate.tujuan = tujuan;
  htmlTemplate.tableRangeValues = tableRangeValues;
  htmlTemplate.trigger = trigger;
  htmlTemplate.statusEmail = statusEmail;

  const htmlForEmail = htmlTemplate.evaluate().getContent();

  if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')); {

  GmailApp.sendEmail(
    "sistem.jis@gmail.com", 
    "Approval SPP Komersial", 
    "HTML Support",
    { htmlBody: htmlForEmail }
   );

  ws.getRange(3, 13,lr-2).setValue('EMAIL_SENT');

}

'''

这是我的示例文件链接: https://docs.google.com/spreadsheets/d/13TKIhY7HmK3o-j98q45XXb2nwZzfYwyYn7EULhY_RJw/edit#gid=1216091331

似乎我对触发器以及如果我不理解的条件代码有疑问

谢谢!

1 个答案:

答案 0 :(得分:0)

定义const trigger = ws.getRange(3, 1,lr-2).getValues();将返回类型为[[FALSE], [true], [FALSE], [true]]的数组

  • 要使代码正常工作,您需要定义一个循环遍历每行(并触发)的循环
  • 另外,从;中删除if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')); {

示例:

function myFunction() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("MASTER");

  const h3 = 'SPP Proyek JIS Tanggal xx dari xxx';
  const headers = ws.getRange("A2:M2").getValues();
  const item = headers[0][4];
  const spec = headers[0][5];
  const sat = headers[0][6];
  const qty = headers[0][7];
  const price = headers[0][8];
  const total = headers[0][9];
  const tujuan = headers[0][10];
  const lr = ws.getLastRow();
  const tableRangeValues = ws.getRange(3, 5,lr-2,7).getDisplayValues();
  var data = ws.getRange(3, 1,lr-2,13).getDisplayValues();
  for (var i = 0; i < data.length; i++){
    const trigger = data[i][0];
    const statusEmail = data[i][12];

    const htmlTemplate = HtmlService.createTemplateFromFile("Email");
    htmlTemplate.h3 = h3;
    htmlTemplate.headers = headers;
    htmlTemplate.item = item;
    htmlTemplate.spec = spec;
    htmlTemplate.sat = sat;
    htmlTemplate.qty = qty;
    htmlTemplate.price = price;
    htmlTemplate.total = total;
    htmlTemplate.tujuan = tujuan;
    htmlTemplate.tableRangeValues = tableRangeValues;
    htmlTemplate.trigger = trigger;
    htmlTemplate.statusEmail = statusEmail;

    const htmlForEmail = htmlTemplate.evaluate().getContent();
    Logger.log(trigger);
    if ((trigger != 'FALSE') && (statusEmail != 'EMAIL_SENT')) {
        GmailApp.sendEmail(
      "sistem.jis@gmail.com", 
      "Approval SPP Komersial", 
      "HTML Support",
      { htmlBody: htmlForEmail }
      );

      ws.getRange(3, 13,lr-2).setValue('EMAIL_SENT');
    }
  }
}

注意: 在此示例中,我没有修改tableRangeValues,因为这些值稍后会在客户端进行处理。根据您希望他们是什么样子,您可能还需要迭代thromguh tem。