HTMLbody to keep cell format
I have a script sending out HTML formatted emails to the list of recipients:
<!-- Greeting section START -->
<td width="100%" valign="top">
<h1 style="font-size: 18px; line-height: 26px; margin: 0;">Hi there,</h1>
<p style="margin: 0;"> <br />Please provide the weekly status report by Monday, CoB: <br /> </p>
<p style="margin: 0;">
<?= project ?>
<br /> </p>
<p style="margin: 0;"> Should you have any questions or experience difficulties, reach out to me.<br /> </p>
<h1 style="font-size: 18px; line-height: 28px; margin: 0;">Best regards,</h1>
<h1 style="font-size: 16px; line-height: 24px; margin: 0;">PMO Team</h1>
</td>
<!-- Greeting section END -->
The reference in the code takes data from respective column ( htmlBody.project = row[3]) :
function sendEmailForm() {
var htmlBody = HtmlService.createTemplateFromFile('mail_template');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("T_TEST");
var startRow = 2; // First row of data to process
var numRows = 70; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 11);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
{
var emailAddress = row[0]; // First column
if (emailAddress.match('@') === null){
continue; // skip this iteration of the loop and go to the next one
};
var ccEmailAddress = row[1]
// var body = row[3]; // Second column
var subject = row[2];
htmlBody.project = row[3];
//htmlBody.survey = row[10];
var message = htmlBody.evaluate().getContent();
//var body = email_html;
var aliases = GmailApp.getAliases()
Logger.log(aliases); //returns the list of aliases you own
Logger.log(aliases[1])
GmailApp.sendEmail(emailAddress, subject, message, {htmlBody : message, cc: ccEmailAddress,'from': aliases[1]});
}
}
}
However, data in those cells are organized in paragraphs, e.g.: Project 1 Project 2 Project 3
What I actually get in the emails is: Project 1 Project 2 Project 3
Is there any way to keep cell format in the emails? Thanks in advance for any help
Solution 1:
In that case, as a simple modification, how about the following modification?
Modified script:
Please modify sendEmailForm()
as follows.
From:
htmlBody.project = row[3];
To:
htmlBody.project = row[3].replace(/\n/g, "<br>");
And also, please modify the HTML as follows.
From:
<?= project ?>
To:
<?!= project ?>
- By the above modification,
Project 1\nProject 2\nProject 3
in a cell is converted toProject 1<br>Project 2<br>Project 3
and this HTML is put using<?!= project ?>
.
Reference:
- Force-printing scriptlets