0

I'm trying to create a loop that will inject a html component into my html email using google script editor. I keep getting errors: [ { message: 'Syntax error: Illegal character. line: 51 file: Code', domain: 'global', reason: 'badRequest' } ] }. How do you do this.

Path: Javascript.js

var data = [{name: 'Bob', link: 'www.test.com'}, {name: 'Joe', link: 'www.test.com'}];

var htmlComponent = [];
for (var = x = 0; x <= 1; x++) {
  var component = `<tr><td><a href=x.link>x.name</a></td></tr>`;
  htmlComponent.push(component);
}

Path: htmlEmail.html

<body>
  <center>
    <table
      style="width:100%;max-width:600px;margin: 0 auto;"
      cellpadding="0"
      cellspacing="0"
      border="0"
    >
      <tbody>
          <?= htmlComponent ?>
      </tbody>
    </table>
  </center>
</body>
bp123
  • 3,217
  • 8
  • 35
  • 74

2 Answers2

3
  • You want to create a HTML table from an array and put the created table the existing HTML.
  • You want to achieve this using Google Apps Script.
    • In your situation, Javascript.js and htmlEmail.html are put in each file on the script editor.
    • Javascript.js is Google Apps Script.

If my understanding is correct, how about this modification? Please think of this as just one of several answers.

Usage:

When you use this modified script, please copy and paste Javascript.js and htmlEmail.html to a script file and HTML file of the script editor, respectively. And run myFunction(). By this, you can see HTML including the table at the log. When html is used for MailApp.sendEmail({to: "###", subject: "sample subject", htmlBody: html}), an email with the HTML body is sent.

For Javascript.js

function myFunction() {
  var data = [{name: 'Bob', link: 'www.test.com'}, {name: 'Joe', link: 'www.test.com'}];

  // I modified below script
  var t = HtmlService.createTemplateFromFile("htmlEmail"); // Retrieve HTML from the file of htmlEmail.html.
  t.htmlComponent = data.reduce(function(s, x) {return s += "<tr><td><a href=" + x.link + ">" + x.name + "</a></td></tr>"}, "");
  var html = t.evaluate().getContent();

  Logger.log(html)

  // If you want to send an email including HTML, please use the following script.
  // MailApp.sendEmail({to: "###", subject: "sample subject", htmlBody: html});
}

For htmlEmail.html

Please modify as follows.

From:
<?= htmlComponent ?>
To:
<?!= htmlComponent ?>

Note:

  • In the current stage, unfortunately, the back-tick and the template literals cannot be used at Google Apps Script.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
2

If you don't mind trying an alternate method (based on the templated HTML guide)

Code.js:

function mailThis() {
  var htmlString = doGet();
  GmailApp.sendEmail('To', 'Subject', '', {
    htmlBody: htmlString.getContent()
  })
}

function doGet() {
  return HtmlService
  .createTemplateFromFile('Index')
  .evaluate();
}

Index.html:

<body>
  <center>
      <table
      style="width:100%;max-width:600px;margin: 0 auto;"
      cellpadding="0"
      cellspacing="0"
      border="0"
      >
      <tbody>
      <? var data = [{name: 'Bob', link: 'www.bobwebsite.com'}, {name: 'Joe', link: 'www.joewebsite.com'}];
      for (var x = 0; x <= 1; x++) { ?>
        <tr>
            <td> <a href=<?= data[x].link ?> > <?= data[x].name ?> </a></td>
        </tr>
        <? } ?>
      </tbody>
    </table>
  </center>
</body>
ADW
  • 4,177
  • 1
  • 14
  • 22