Home > OS >  Send an email for every single email in a column in a google sheet
Send an email for every single email in a column in a google sheet

Time:08-10

So what I'm trying to do is send an email for every single email in a google sheet. The emails are each stored in the first column, the subject line is the second column, and the the body of the email is the third column. I can send a single email if I don't put the program through a for loop, but then I'd have to have a method that sends an email for each row. I know that getDataRange SHOULD create a 2D array of all of the cells, so why am I not able to access it by doing dataRange[integer][integer]. Below is what I currently have so far for the google script.

function sendEmail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName("Email");
  var dataRange = sh.getDataRange();
  for(var i = 0; i < sh.getLastRow(); i  ){
    var emailAddress = dataRange[i][1];
    var subject = dataRange[i][2];
    var body = dataRange[i][3];
    MailApp.sendEmail(emailAddress,subject,body);
  }
}

This is just an example of what the google sheet may look like.

The final output should be sending:

  1. Test 1 in the subject line, Body 1 in the body portion of the email, all of this sent to whoever email 1 is
  2. test 2 in the subject line, Body 2 in the body portion of the email, all of this sent to whoever email 2 is

CodePudding user response:

Try this:

function sendEmail() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Email");
  const vs = sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues();//Assume one header row
  vs.forEach(r => {
    MailApp.sendEmail(r[0],r[1],r[2])
  });
}
  • Related