Include a response url in an HtmlService-generated html

37 views Asked by At

I'm creating a message for an email to send using MailApp. The gscript looks like this:

function emailIT(newEmp)
{
  var templ = HtmlService.createTemplateFromFile('ITEmailHtml')
  
  templ.newEmp = newEmp;
  
  var message = templ.evaluate().getContent();

The script crashes on the last line. newEmp is a dictionary with a key:value pair responseUrl: "https://docs.google.com/forms/d/e/1FAIpQL...".

The html template file looks like:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    
    A new employee onboarding request has been submitted for <?= newEmp.empName ?>. 
Please onboard this new employee and update the request <a href="<?= newEmp.responseUrl ?>">here</a>. 
The relevant information for this employee is listed below: <br><br>
...

I've tried changing the scriptlets to force print tags, , with the same result. If I get rid of the href part, all of my other print tags work fine.

Thanks in advance for any help

2

There are 2 answers

0
Cooper On

This works for me:

GS:

function myfunk() {
  const obj = {val1:"one",val2:"two",val3:"three"};
  let t = HtmlService.createTemplateFromFile('ah3');
  t.obj = obj;
  Logger.log(t.evaluate().getContent());
}

html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <title></title>
  </head>
  <body>
   <div> Value 1 is <?= obj.val1 ?> </div>
   <div> Value 2 is <?= obj.val2 ?> </div>
   <div> Value 3 is <?= obj.val3 ?> </div>
  </body>
</html>

Logger Output:

9:26:22 PM Notice Execution started 9:26:20 PM Info

<html>

  <head>

    <base target="_top">

    <title></title>

  </head>

  <body>

   <div> Value 1 is one </div>

   <div> Value 2 is two </div>

   <div> Value 3 is three </div>

  </body>

</html>

9:26:23 PM  Notice  Execution completed
0
idfurw On

I got no error:

function total() {
  const newEmp = {
    empName: 'Name',
    responseUrl: 'url.html'
  };
  var templ = HtmlService.createTemplateFromFile('ITEmailHtml');
  templ.newEmp = newEmp;  
  var message = templ.evaluate().getContent();
  GmailApp.sendEmail('xxx', 'subject', '', {htmlBody:message});
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  A new employee onboarding request has been submitted for <?= newEmp.empName ?>. 
  Please onboard this new employee and update the request <a href="<?= newEmp.responseUrl ?>">here</a>. 
  The relevant information for this employee is listed below: <br><br>
  </body>
</html>