简体   繁体   中英

Google sheets: send email data range question

I am using this script to send reminder emails. It works when I run it manually, but when I try to run it on a trigger, it fails.

var EMAIL_SENT = 'EMAIL_SENT';

function sendEmail() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Job Checkup Tool');
  var startRow = 4; // First row of data to process
  var numRows = 13; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3);
  // 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[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[2]; // Third column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Job Checkup Reminder';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

The problem appears to be a result of the script finding no email address in column 1.

When I run it manually, though it has an error, it will successfully send the emails even if there isn't an email value in column 1.

When the trigger runs it, it shows the same error

Exception: Failed to send email: no recipient

but doesn't send any emails.

If I adjust the range to focus only on rows with an email address in column 1, it runs great manually and via the trigger.

Is there a good way to request the script to send the email IF there is an email present within column 1 but then ignore the other rows that don't have an email yet?

Replace

if (emailSent !== EMAIL_SENT) {

by

if (emailAddress !== '' && emailSent !== EMAIL_SENT) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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