Debye Leon
Debye Leon

Reputation: 21

How to do a line break in Google Sheets using (GAS)

Hello, I'm new to Google Apps Script. I have a google Sheets with 6 columns and 1 row, will insert more once I can have it running

My objective is to send automatic emails from the list of emails I have from my Column 1. And using the text that will go in the email from Column 2-6, I've managed to send out email but it jumbles it all together.

Does anyone know what to code to have a normal looking email with next lines for each column and also add text like "Your Student" before a column?

This is what I have

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A1:F2
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  // 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 = "Hello your Student" + row[1] 
    **" Has done a marvelous job in Class" + row[2]
    " His new area to grow involves" + row[3]+ row [4]+ row [5] ;**
  var subject = "Report for your Student";
  MailApp.sendEmail(emailAddress, subject, message);

} }

What is starred is not seen in the email, maybe because it's on the next line and I don't know what to code to introduce it still to keep it going and use a space in the email. Thanks

Upvotes: 0

Views: 5586

Answers (2)

OblongMedulla
OblongMedulla

Reputation: 1595

Using html you can add breaks and or other formatting such as Bold.

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  // Fetch the range of cells A1:F2
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  // 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 myMessage = "<b>Hello your Student</b> " + row[1] + " <br> Has done a marvelous job in Class" + row[2] + "<br> His new area to grow involves:" + "<br>" + row[3] + "<br>" + row [4] + "<br>" + row [5] ;
  var subject = "Report for your Student";
    MailApp.sendEmail(emailAddress, subject, 'text Body',{htmlBody: myMessage});
  }}

Upvotes: 0

FunThomas
FunThomas

Reputation: 29276

var message = "Hello your Student" + row[1] 
     + " Has done a marvelous job in Class" + row[2]
     + " His new area to grow involves" + row[3]+ row [4]+ row [5] ;

or if you need line breaks

var message = "Hello your Student" + row[1] + '\n'
     + " Has done a marvelous job in Class" + row[2] + '\n'
     + " His new area to grow involves" + row[3]+ row [4]+ row [5] ;

Upvotes: 1

Related Questions