Kar3n
Kar3n

Reputation: 1

google script - creating a list of values to be inserted into an email

I'm new to Google Script and Javascript in general. But I'm trying to email a list of file names pulled from a spreadsheet. The names are in a column in my sheet, and I created a variable called "newfiles", which pulls out just the values that I need. So far so good.

function sendUpdate()
{
 
  //set up variables
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("finishedfiles");
  var range = sheet.getDataRange().getValues();
 
    for (n = 0; n < range.length; n++)
      {
      var newfiles = range[n][1];
      }

The problem is that the script sends out an email for each value in the range.

var startRow = 2;
  
for (var i = 0; i < range.length; i++)
if (startRow + i , 5 != '')
      {
       MailApp.sendEmail
       {to: "[email protected]", 
       subject: "list of files", 
       htmlBody: "Here's your list:<br>" + newfiles +
       "<br>This message was sent automatically."});
       }

I would like to send only one email, so I need to combine the values in my variable into one text string that can be inserted into my message.

I feel like this is a n00b question, but I can not for the life of me figure it out.

Upvotes: 0

Views: 199

Answers (1)

Robin Gertenbach
Robin Gertenbach

Reputation: 10796

Well, if you want one big email you mustn't call MailApp for every file name in the range.

function sendUpdate() {
  var sheet = SpreadsheetApp
      .getActiveSpreadsheet()
      .getSheetByName("finishedfiles");

  var range = sheet.getDataRange().getValues();

  var filenames = range
    .map(function(row) {return row[1];}) // Extract the filenames column
    .join("<br>");                       // Combine them all into a string

  MailApp.sendEmail(
    {to: "[email protected]", 
     subject: "list of files", 
     htmlBody: "Here's your list:<br>" + filenames +
     "<br>This message was sent automatically."});
}

What does if (startRow + i , 5 != '') do? Doesn't it always evaluate to true?

Upvotes: 1

Related Questions