Home > Net >  How can i do line break in Html Email using app script while the data is only from one cell of Sprea
How can i do line break in Html Email using app script while the data is only from one cell of Sprea

Time:06-23

I tried to import the data from a spread sheet and send it with GmailApp. In the "B4" of my spreadsheet, there is a text with line breaks. If i tried to transfer it to Html-Email. It didn't read the "
" which i converted from "\n". It always showed like " Hello
i'm here! " in the email and didn't do the line breaks in email. Should i do something more for it? Thanks!

The code:

function splitTest() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
 var str = ss.getRange("B4").getValue();
  Logger.log(str);
  var array1 = [{}];
  array1 = str.split("\n");
  return array1
};

function sendMail() {
  var ename = 3; //give every variable the right column number. 1st column is 0 and so on.
  var cname = 2;
  var email = 5;
  var emailTemp = HtmlService.createTemplateFromFile("email");
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Recipients");
  var wsSettings = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var name = wsSettings.getRange("B2").getValue();
  var subject = wsSettings.getRange("B1").getValue();
  var array1 = splitTest();
  var cMessage = array1.join("<br />");
  var data = ws.getRange("A2:G"   ws.getLastRow()).getValues();
  data = data.filter(function(r){ return r[6] == true}); //return r[a], a is for where the checkbox is. for us to choose who we send the mails.The column starts from 0!
  data.forEach(function(row){
   emailTemp.cmess = cMessage;
   var htmlMessage = emailTemp.evaluate().getContent();
    GmailApp.sendEmail(
      row[email],
      subject,
      "Bitte öffnen Sie das Html-Format,um die Email zu lesen, Danke!",
      {name: name,
      htmlBody: htmlMessage, 
      });
  }); 
};

The html code

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    
  </head>
  <body>
   
  <div align="left">
<table style="border: none;"><colgroup><col style="width: 487px;" /><col style="width: 115px;" /></colgroup>
<tbody>
<tr style="height: 99pt;">
<td style="vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; text-align: justify; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><span style="border: none;"><img src="https://drive.google.com/uc?id=1rRVBhFpEbp8bjGI9jqWlbn_6K8H4djAv" width="438" height="158" /></span></span></p>
</td>
<td style="border-right: 1pt solid #ffffff; border-bottom: 1pt solid #ffffff; border-top: 1pt solid #ffffff; vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; text-align: right; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><span style="border: none;"><img src="https://drive.google.com/uc?id=1vwNZtLRAnoRX5WuEWeojEKMf6lOOYbre" width="67" height="137" /></span></span></p>
</td>
</tr>
<tr style="height: 470.66pt;">
<td style="border-left: 1pt solid #ffffff; border-right: 1pt solid #ffffff; vertical-align: top;" colspan="2">
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><?= cbegin ?></span></p>
<br />
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt; white-space:pre-line;"><span style="font-size: 11pt; font-family: Arial;">
  <?= cmess ?>
  </span>

</p>
<br />
<br />
<br />
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 11pt; font-family: Arial;"><?= cend ?></span></p>
</td>
</tr>
<tr style="height: 60.1465pt;">
<td style="vertical-align: top; background-color: #f4cccc;">
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><strong><span style="font-size: 9pt; font-family: Arial;">Formosa Chinesische Sprachschule e.V.</span></strong></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 9pt; font-family: Arial;">Schulort : Geschwister-Scholl-Gymnasium D&uuml;sseldorf</span></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><span style="font-size: 9pt; font-family: Arial;">Redinghovenstra&szlig;e 41, 40225 D&uuml;sseldorf</span></p>
<p style="line-height: 1.2; margin-top: 0pt; margin-bottom: 0pt;"><strong><span style="font-size: 9pt; font-family: Arial;">Email: </span></strong><a style="text-decoration: none;" href="mailto:[email protected]"><strong><span style="font-size: 9pt; font-family: Arial; color: #1155cc; text-decoration: underline; text-decoration-skip-ink: none;">[email protected]</span></strong></a></p>
</td>
<td style="border-right: 1pt solid #ffffff; border-bottom: 1pt solid #ffffff; border-top: 1pt solid #ffffff; vertical-align: top; background-color: #f4cccc;">&nbsp;</td>
</tr>
</tbody>
</table>
</div>

  </body>
</html>

CodePudding user response:

I find the answer!! It works if i do .replace() to change the code back to "<" and ">", Thank you Cooper anyway!! Yes!!

var str = emailTemp.evaluate().getContent();
  str1 = str.replace(/&lt;/g,"<");   
  str2 = str1.replace(/&gt;/g,">"); 
....
htmlbody: str2...

CodePudding user response:

Try this:

function splitTest() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var str = ss.getRange("B4").getValue();
  return str.split("\n");
}
  • Related