2

I have a google form which collects few details and an email-id, however if there is a wrong entry in the email, the script fails and does not execute the next row, it gets stuck there. Now i want the script to ignore the wrong entries and execute for the correct entries.I have my code below.

var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = sheet.getLastRow();   // Number of rows to process
  // Fetch the range of cells A2:B3

  var dataRange = sheet.getRange(startRow, 5, 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 = "You have been registered for follwoing events:-";
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 7).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
KRR
  • 4,647
  • 2
  • 14
  • 14

1 Answers1

0

For this you can add a if condition before this line of code

if(valid email address){     
MailApp.sendEmail(emailAddress, subject, message);
}
else{continue;}

to verify if the email id has a valid value or not.

You can also check this approach.

Hope that helps!

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14
  • it tells Missing ) after condition. (line 18, file "Code") thas the if conditon part –  Dec 13 '14 at 04:33