I have a script where I get the multiple rows from a spreadsheet and I want to send an email per each row.
The problem is that I have an HTML template where I have some data dynamically, and when I run the data.forEach()
function, it won't recognize the values I have.
My data looks like
Student | Score |
---|---|
Robert | 8 |
David | 6 |
Frank | 9 |
My script
function sendMultipleEmails() {
const rawData = ss.getRange("A1:B3").getValues();
let data = [];
// I prefer working with Object Literals rather than Spreadsheets array of arrays way
rawData.forEach(value => {
data.push({
name: value[0],
score: value[1],
})
})
data.forEach(value => {
let emailTo = "[email protected]";
// In subject, value.name works, because it's set in the code
let subject = `Scores for ${value.name}`;
let body = HtmlService.createTemplateFromFile("scores");
et message = body.evaluate().getContent();
let options = {
htmlBody: message,
}
GmailApp.sendEmail(emailTo, subject, body, options)
})
}
My template, where value.name and value.score don't work, because the parameter value in forEach is not recognized by HTML
<h2> Hey <?= value.name ?> </h2>
<p>This is your score <?= value.score?></p>
Is there a better way to do that?
CodePudding user response:
In your script, how about the following modification?
From:
let body = HtmlService.createTemplateFromFile("scores");
et message = body.evaluate().getContent();
To:
let body = HtmlService.createTemplateFromFile("scores");
body.value = value; // Added
et message = body.evaluate().getContent();