0

I am sure the answer is staring at me right in the face but I'm a bit confused. I have a spreadsheet with employee information and I created a forEach loop (below) so that at the end of each row it should send an email with information from the row. However, the HTML file is not recognizing value[0], value[1], etc and I am getting the following error message: ReferenceError: value is not defined

function ncnsEmailFunction () {

const values = ncnsSheet.getRange(2, 1, 10, 6).getValues();

values.forEach(function(value, index){
if (value[0] === "") return;

const htmlBody = HtmlService.createTemplateFromFile("NCNSEmailTemplate").evaluate().getContent();
MailApp.sendEmail({
  to: Session.getActiveUser().getEmail(),
  subject: `NCNSEmailTemplate`,
  htmlBody: htmlBody,
  });

})

}

Here is what my HTML file looks like:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

Hello <?=value[0]?>

  </body>
</html>

What can I do to fix this?

1
  • 1
    ncnsSheet is undefined Commented Sep 20, 2022 at 1:19

1 Answer 1

3

Try this:

function ncnsEmailFunction() {
  const ss = SpreadsheetApp.getActive();
  const ncnsSheet = ss.getSheetByName("Your sheet name")
  const values = ncnsSheet.getRange(2, 1, 10, 6).getValues();
  values.forEach(function (value, index) {
    if (value[0] === "") return;
    let t = HtmlService.createTemplateFromFile("NCNSEmailTemplate");
    t.value = value[0];
    const htmlBody = t.evaluate().getContent();
    MailApp.sendEmail({to: Session.getActiveUser().getEmail(),subject: `NCNSEmailTemplate`,htmlBody: htmlBody});
  })
}

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
Hello <?=value?>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Ok perfect! I was just playing around with the following line and it helped fix the issue, this is good to know for future projects: t.value = value[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.