0
votes

I'm trying to import a column into Google Sheets, and then send it via an email.

However, it's currently putting each cell in the column on the same line in the email, separated by a comma.

function sendEmails() {
    var emails = ['example@example','example2@example.com'];
    // Get Message
    var messageContents = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("H3:H20").getValues();//Double quotes added by editor. See edit history
    //Send Emails
    var subject = "Example";
    MailApp.sendEmail(emails, subject, messageContents);
}

Is there any way to get it to put each cell on a new line, as they are currently in the spreadsheet.

Thanks

1
Try messageContents.join("\n") - TheMaster

1 Answers

0
votes

Just add this line after you define messageContents:

messageContents = messageContents.join("\n");

The join() method concatenates all the elements in an array to a new string. You can set a separator as a parameter, in our case, \n, which creates a new line.

This method does not modify the original variable, so you have to assign the result of join() to messageContents.

So full code would be like:

function sendEmails() {
  var emails = ['example@example','example2@example.com'];
  // Get Message
  var messageContents = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("H3:H20").getValues();//Double quotes added by editor. See edit history
  messageContents = messageContents.join("\n");
  //Send Emails
  var subject = "Example";
  MailApp.sendEmail(emails, subject, messageContents);
}

I hope this is of any help.