Home > Enterprise >  GAS - Error when evaluate HtmlService.createTemplateFromFile
GAS - Error when evaluate HtmlService.createTemplateFromFile

Time:09-27

I am trying to send email using HtmlService but get error at line temp.evaluate().getContent(). I tried to search for response, there is a response related to runtime V8 but cannot get it. Do I need to change the json file?

I am very new to Google Apps Script so I really appreciate for your help. Please help check. Thank you so much for your help.

The code as follows:

js file:

function HLReminder(){
var SS = SpreadsheetApp.getActiveSpreadsheet();  //declare the spreadsheet
var Sheet = SS.getSheetByName("Tax_Master");  //declare sheet name
var Range = Sheet.getDataRange(); //to set the range as array
var Values = Range.getDisplayValues(); //to get the value in the array 
 
let fvs = Values.filter(function (item) { return item[13] == "Y" }); // filter only housecontract to remind
 
const temp = HtmlService.createTemplateFromFile("email");

const uniqueClient = [];
const map = new Map();
var messageBody,mailto;
    for (const item of fvs) { // create unique list
        if(!map.has(item[0])){
            map.set(item[0], true);    
            uniqueClient.push(item[0] );
        }
    } //end unique client create loop
    
    
    uniqueClient.forEach(function(client){ // loop through unique clients
       var messageList = [];
       fvs.forEach(function(row){ //loop through all rows to check for a match of unique client
        
          if(client == row[0]){
             messageList.push(["- Mr/Ms " row[1] ": house lease expired on "  row[7]]);   
             
             mailto =  row[12];
            }
        }) // end inside matching row loop
       Logger.log(messageList)
       temp.client = client;
       temp.messageList = messageList;

            MailApp.sendEmail({
                to: mailto,          // email address
                subject: client   "- House lease contract expire", // Subject line
                htmlBody: temp.evaluate().getContent()
                });
            
    })// end unique client loop
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div>
      <div>
        <p> "Dear team,</p>
        <p> </p>
        <p> This is to notify that the house lease contract(s) of the below expats of <?!= client ?> has expired. Please action accordingly.</p>
      </div>
      <div> 
       <? messageList.forEach(r=>{ ?> 
        
       <p> <?!= r[0]> </p>
       <? })
      </div>
      <div>
          <p> ----------------- </p>
          <p> Sample email: </p>
          <p> Dear , </p>
          <p> </p>
          <p> The house lease contract(s) of the following expatriate employee(s) of <?= client ?> has expired. Please help provide us with the updated house lease contracts. Thank you.</p>
      </div>
      <div> 
       <? messageList.forEach(r=>{ ?> 
        
       <p> <?!= r[0]?> </p>
       <? })
      </div>
      <div>
        <p> </p>
        <p> Should you have any question, please contact us. </p>
        <p> </p>
        <p> Best regards, </p>
      </div>
  </body>
</html>

CodePudding user response:

I thought that in your script, some scriptlets are not enclosed. I think that the reason for your issue of error at line temp.evaluate().getContent() is due to this. So how about the following modification?

Modified script:

Please modify your HTML as follows.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <div>
      <div>
        <p> "Dear team,</p>
        <p> </p>
        <p> This is to notify that the house lease contract(s) of the below expats of <?!= client ?> has expired. Please action accordingly.</p>
      </div>
      <div> 
       <? messageList.forEach(r=>{ ?> 
        
       <p> <?!= r[0] ?> </p> <!-- HERE -->
       <? }) ?> <!-- HERE -->
      </div>
      <div>
          <p> ----------------- </p>
          <p> Sample email: </p>
          <p> Dear , </p>
          <p> </p>
          <p> The house lease contract(s) of the following expatriate employee(s) of <?= client ?> has expired. Please help provide us with the updated house lease contracts. Thank you.</p>
      </div>
      <div> 
       <? messageList.forEach(r=>{ ?> 
        
       <p> <?!= r[0]?> </p>
       <? }) ?> <!-- HERE -->
      </div>
      <div>
        <p> </p>
        <p> Should you have any question, please contact us. </p>
        <p> </p>
        <p> Best regards, </p>
      </div>
  </body>
</html>

Note:

  • In this answer, it supposes that your other script works. Please be careful about this.

Reference:

  • Related