简体   繁体   中英

sending an email from a google sheet

I am trying to send an email that contains 2 columns of data as the body of the email, and the subject line of the email is comprised of 3 cells in one of the columns (see snippet).

在此处输入图片说明

This was my last attempt!!

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

I have only been able to get 1 cell to show in the body of an email

Summary

If I understood it right from your question and comments:

  • You have a email address in cell A1
  • You want to append the entire content of the cells B1:C15 (2 columns, 15 rows) to the message body
  • You want to send a single email with the message body as defined above

In this case, please modify your code as following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM