0

My objective is similar to this can you store a line break in a variable? but I need it to be in GAS/JS/HTML.

mesej is a variable that stores a long string which needs to be displayed as 2 lines of strings in the html email. That string value is in a gsheet cell A14. The line break is displayed just fine in gsheet when I used \n in GAS to break a line after symbol : but it doesn't happen the same in html email.

I have tried with <br> and %A0 but ended up having that tags appeared in the html email and when I use mesej.split(":").join(":\n"), I would get a bounced back email from google support indicates that the email is blocked. Please help to correct my code. Thank you.

My GS function:

function breakline(){
var sheet=SpreadsheetApp.openById("THIS_SHEET_ID").getSheetByName("job");

//some operations in subfn A
var msg1="Career Status";

//some operations in subfn B
var msg2="Desperately Seeking";

var msg=msg1+":\n"+msg2;
sheet.getRange(14,1).setValue(msg);

var mesej=sheet.getRange(14,1).getValue();//"Career Status:Desperately Seeking" 2 lines in 1 cell

var sampling=HtmlService.createTemplateFromFile("sample");
  sampling.mesej=mesej;
  
var mymesej = sampling.evaluate().getContent();
  
MailApp.sendEmail("email@gmail.com","Job Application"," ",{htmlBody:mymesej});
}

My HTML code:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
   <?=mesej?> 
  </body>
</html>
Rubén
  • 34,714
  • 9
  • 70
  • 166
dell
  • 171
  • 13
  • 2
    I don't know anything about the specifics of what you're doing, but I'm pretty sure the issue is that your HTML `
    ` is being "escaped." And you need it to not be escaped. See if any reading here helps for a method to _not_ escape the HTML in your string: https://developers.google.com/apps-script/guides/html/templates
    – Marc Oct 23 '20 at 17:03
  • 1
    How about `var msg=msg1+":
    "+msg2;`
    – Cooper Oct 23 '20 at 17:29
  • 1
    After lots of trials and errors, thanks a lot to both @Marc because your link has led me to https://stackoverflow.com/questions/28459829/passing-variable-to-html-output-and-then-into-a-scriptlet and @Cooper as the `
    ` can remain as it is and it appears as how I wanted and from there I learnt I could even add in other html tags like `` altogether!
    – dell Oct 24 '20 at 18:34

1 Answers1

0

answer from Cameron Roberts in Passing variable to HTML output and then into a scriptlet : we ONLY need to add the ! in the HTML encoded equivalents to get the desired effect of html tags such as <br> tag in our html output. This specifically for scenario where a long string value stored in 1 variable.

In GAS editor:- var msg=msg1+":\n"+msg2; changed to var msg="<i><b>"+msg1+":</b></i><br>"+msg2; to get string value in msg1 in font style italic and bold and then a line break before msg2 content.

In HTML editor: <?=mesej?> changed to <?!=mesej?>

dell
  • 171
  • 13