- 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.