从Google工作表发送电子邮件

时间:2019-08-21 05:52:35

标签: google-apps-script google-sheets

我正在尝试发送一封包含2列数据的电子邮件作为电子邮件正文,并且电子邮件的主题行由其中一列中的3个单元格组成(请参见摘录)。

enter image description here

这是我最后的尝试!

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows,2);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1,2]; // Second column
    var subject = 'Here is your BUY DRAFT from Ben's Auto';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

我只能在电子邮件正文中显示1个单元格

1 个答案:

答案 0 :(得分:0)

摘要

如果我对您的问题和评论的理解正确,

  • 您在A1单元格中有一个电子邮件地址
  • 您要将B1:C15单元格的全部内容(2列15行)附加到邮件正文中
  • 您要发送一封具有上述定义的邮件正文的电子邮件

在这种情况下,请如下修改您的代码:

 function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1; 
  var numRows = 15; // from your description it seems like you want to append to body the data of all 15 rows
  // Fetch the range of cells B1:C15
  var dataRange = sheet.getRange(startRow, 2, numRows,2);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  var message='';  
  var emailAddress = sheet.getRange('A1').getValue(); 
  for (var i=0;i<data.length;i++) {
    var row = data[i];
    message = message+row[0] + " " + row[1]+'\n';
   }    
  var subject = 'Here is your BUY DRAFT from Ben\'s Auto';
  MailApp.sendEmail(emailAddress, subject, message);
}