0
votes

I am trying to send emails to employees by using gs to pull the info from a sheet. I figured out how to pull the email address, message and subject, however, I want to include information from multiple columns in the message. Below is what I have so far....however I also want to include information from additional columns in the email message. For example, row[1] contains a message, row[3], row[4], row [5] contain values for Hotel 1, Hotel 2, Hotel 3 which i want to include in the message body.

/**
 * Sends emails with data from the current spreadsheet.
 */
function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:D3
  var dataRange = sheet.getRange(startRow, 1, numRows, 4);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var subject = 'Your hotel reservation options';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}
1
What's your question?Cooper

1 Answers

1
votes
function sendEmails() {
  var sh=SpreadsheetApp.getActiveSheet();
  var sr=2;
  var rg=sh.getRange(sr, 1, sh.getLastRow()-sr+1,sh.getLastColumn());
  var data=rg.getValues();
  for (var i=0;i<data.length;i++) {
    var row=data[i];
    var emailAddress=row[0]; 
    var message=Utilities.formatString('Starting Message:%s\nHotel1:%s\nHotel2:%s\nHotel3:%s', row[1],row[3],row[4],row[5]); 
    //You can add whatever text that you wish between the `%s` and they get replaced the arguments in the order that they appear.
    //If you wish to use the plain text version then use \n for new lines and if you plan to use {htmlBody:} then use `<br />` for new lines.
    var subject='Your hotel reservation options';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}