想要使脚本仅将电子邮件发送给Google Spreadsheets中最后添加的电子邮件

时间:2020-05-28 02:38:38

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

这是一个脚本,用于向我保存在googlespreadsheets中的联系人发送电子邮件。但是,我只希望它发送到最近添加的电子邮件(不是所有列表)。它一直将其发送给列表中包括的每个人,而我只想发送到注册电子邮件的行[2]中最后添加的电子邮件。

谢谢。

 function sendEmail(e) {
      var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
      var data = ws.getRange("A2:D" +ws.getLastRow()).getValues();
      data.forEach(function(row) {
      var html = HtmlService.createTemplateFromFile("htmlemail.html")
      var htmlText = html.evaluate().getContent();
      var subject = "Thanks for participation";
      var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
      var options = { htmlBody : htmlText };
      GmailApp.sendEmail(row[2], subject, textBody, options);
      });
    }

1 个答案:

答案 0 :(得分:1)

这只会每次通过电子邮件发送最后一行。

function sendEmail(e) {
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  var data = ws.getRange(2,1,ws.getLastRow()-1,4).getValues();
  var html = HtmlService.createTemplateFromFile("htmlemail.html")
  var htmlText = html.evaluate().getContent();
  var subject = "Thanks for participation";
  var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
  var options = { htmlBody : htmlText };
  GmailApp.sendEmail(data[data.length-1][2], subject, textBody, options);
}

如果您以这种方式进行操作,则除非您使用其他电子邮件对其进行编辑,否则它将仅使用该行一次。

function sendEmail(e) {
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  var data = ws.getRange(2,1,ws.getLastRow()-1,4).getValues();
  var html = HtmlService.createTemplateFromFile("htmlemail.html")
  var htmlText = html.evaluate().getContent();
  var subject = "Thanks for participation";
  var textBody = "This email requires HTML support. Please make sure you open with a client that supports it.";
  var options = { htmlBody : htmlText };
  if(data[data.length-1][2]) {
    GmailApp.sendEmail(data[data.length-1][2], subject, textBody, options);
    ws.getRange(data.length-1+2,3).setValue('');
  }
}