简体   繁体   English

想让脚本仅将 email 发送到 Google 电子表格中最后添加的一个

[英]Want to make the script send email only to the last added one in Google Spreadsheets

This is a script to send emails to the contacts I saved in googlespreadsheets.这是一个向我保存在 googlespreadsheets 中的联系人发送电子邮件的脚本。 However, I want it only to send it to the last added email (not all the list).但是,我希望它只发送到最后添加的 email(不是所有列表)。 It keeps sending it to everyone included in the list and I want only to send to the last added email in row[2] where are the emails registered.它不断将其发送给列表中包含的每个人,我只想发送到注册电子邮件所在的行 [2] 中最后添加的 email。

Thanks.谢谢。

 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);
      });
    }

This will only email the last row every time.这只会 email 每次最后一行。

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);
}

if you do it this way then it will only use that line once unless you edit it with additional emails.如果您这样做,那么它将只使用该行一次,除非您使用其他电子邮件对其进行编辑。

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('');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM