如果单元格为空,MailApp不会发送电子邮件

时间:2018-07-02 20:02:14

标签: google-apps-script

我对编码只有足够的了解,会让自己陷入困境。我有一个代码,可以让我发送包含用户对我的Google表单的回复的电子邮件。但是,如果第14行为空白,则无法发送电子邮件。而是发送空白电子邮件。我还想知道是否有可能在第14行具有值后立即自动发送电子邮件,如果是,那么如果编辑了第14行,是否还可以发送第二封电子邮件?

// This constant is written in column O for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 2000;   // Number of rows to process
  // Fetch the range of cells A2:P
  var dataRange = sheet.getRange(startRow, 1, numRows, 2000)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[1];  // Second column
    var message = row[14];       // Third column
    var emailSent = row[15];     // Third column
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = row[3];
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 16).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is 
interrupted
      SpreadsheetApp.flush();
    }
  }
}

0 个答案:

没有答案