使用Google脚本从工作表发送电子邮件

时间:2020-03-03 21:40:37

标签: google-apps-script

我正在尝试使用gs从工作表中提取信息来向员工发送电子邮件。我想出了如何提取电子邮件地址,消息和主题,但是,我想在消息中包含多个列中的信息。以下是到目前为止我所拥有的...。但是,我也想在电子邮件中包括其他列中的信息。例如,行[1]包含一条消息,行[3],行[4],行[5]包含我要包含在消息正文中的旅馆1,旅馆2,旅馆3的值。

/**
 * 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 个答案:

答案 0 :(得分:1)

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);
  }
}