You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GAS使用HtmlService.createTemplateFromFile执行evaluate方法报错求助

问题排查及解决方案

你的报错和V8运行时、json配置文件没有关联,核心原因是HTML模板语法错误和JS代码的逻辑疏漏,按以下步骤修改即可:

1. 错误点汇总

  • HTML模板语法错误,导致evaluate解析失败:
    • 第一个messageList循环的输出标签少了?结束符
    • 两处forEach循环的脚本块结束位置漏了?>闭合标记
    • 开头问候语多了多余的双引号
  • JS代码中模板实例仅初始化一次,多次循环调用evaluate会触发异常

2. 修正后的HTML模板(email.html)

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

3. 修正后的JS代码

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
  if(fvs.length === 0) return // 无符合条件数据直接结束,避免报错

  const uniqueClient = [];
  const map = new Map();
  var 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
    // 每次循环新建模板实例,避免复用冲突
    const temp = HtmlService.createTemplateFromFile("email");
    temp.client = client;
    temp.messageList = messageList;
    // 加邮箱有效性判断,避免空值报错
    if(mailto && mailto.includes("@")){
      MailApp.sendEmail({
        to: mailto,          // email address
        subject: client + "- House lease contract expire", // Subject line
        htmlBody: temp.evaluate().getContent()
      });
    }
  })// end unique client loop
}

修改完成后直接运行测试即可,不需要调整任何配置文件。

内容的提问来源于stack exchange,提问作者handoi3648

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 04:06:03