Home > Mobile >  Google Sheets Script, if email null then skip to next email and make a status if email are already s
Google Sheets Script, if email null then skip to next email and make a status if email are already s

Time:07-01

I'm using google sheets script to send an email, how to skip a row when the email is null? and how to create an status when the email is sent?

thank you

function sendMails() {
  
  var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
  var wrkShtEmailIDs= wrkBk.getSheetByName("recipient");
  var wrkShtMessage= wrkBk.getSheetByName("email_body");

  var subject = wrkShtMessage.getRange('A2').getValue();
  var message = wrkShtMessage.getRange('B2').getValue();

  for (var i=5;i<=100;i  ){

      var emailAddress = wrkShtEmailIDs.getRange('C'   i).getValue();
      var finalmsg = "";
      finalmsg = "Hi "   emailAddress   "\n"   "\n"   message;
      MailApp.sendEmail(emailAddress, subject, finalmsg);

  }

}

CodePudding user response:

Try for instance, respecting your syntax

function sendMails() {

  var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
  var wrkShtEmailIDs = wrkBk.getSheetByName("recipient");
  var wrkShtMessage = wrkBk.getSheetByName("email_body");

  var subject = wrkShtMessage.getRange('A2').getValue();
  var message = wrkShtMessage.getRange('B2').getValue();

  for (var i = 5; i <= 100; i  ) {

    var emailAddress = wrkShtEmailIDs.getRange('C'   i).getValue();

    if (emailAddress != '') {
      var finalmsg = "";
      finalmsg = "Hi "   emailAddress   "\n"   "\n"   message;
      MailApp.sendEmail(emailAddress, subject, finalmsg);
      wrkShtMessage.getRange('D'   i).setValue('sent') // status in D
    }

  }

}
  • Related