首页 > 解决方案 > 语法错误 HtmlService.createTemplateFromFile().evaluate().getContent();

问题描述

我有点卡住了,不知道为什么会出现这个语法错误。

我创建了一个脚本,该脚本创建了一封电子邮件,其中包含我输入到谷歌工作表中的内容,该工作表从我制作的 HTML 文件中获取模板,并且效果很好。

但是,现在我想将工作表移动到更公共的工作表以供其他用户操作,我只是得到一个语法错误。我已将工作表复制到新工作表中的准确单元格中,并且逐行复制了 .gs 和 .html 代码行并更新了工作表 ID 和工作表名称,但我不知道如何修复它。

我怀疑这与 my.html 不匹配有关,但我已经仔细检查了这一点,甚至更改了文件名只是为了确定,但似乎无法解决问题。

希望任何人的一些意见可以阐明我的困境。

这是完整的代码:

function EODemail2() {

  const ss=SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxx');
  const sh=ss.getSheetByName('Sheet 1'); 
  const lr= sh.getLastRow()  
  const emailAddress = sh.getRange(2,4).getValue();
  const dearAll = sh.getRange(2,2).getDisplayValue();
  const message = sh.getRange(3,2).getDisplayValue();
  const subscriptions= sh.getRange(4,2).getValue();
  const subscriptionsNumbers=sh.getRange(6,3,2,4).getValues();
  const subsComments=sh.getRange(6,2).getValues();
  const operations= sh.getRange(8,2).getValue();
  const weLognumbers=sh.getRange(9,2,6,4).getValues();
  const operationsreport= sh.getRange(16,2).getValue();
  const customerService=sh.getRange(18,2).getValue();
  const csdata = sh.getRange(19,2,5,3).getDisplayValues();
  const csSubs= sh.getRange(25,2).getValue();
  const replacementRequests= sh.getRange(26,2).getValue();
  const replacmentDataTable=sh.getRange(27,3,8,9).getValues(); 
  const lastComents = sh.getRange(37,2).getDisplayValue();
  const endParagraph = sh.getRange(39,2).getDisplayValue();
  const name = sh.getRange(41,2).getDisplayValue();
  const htmlTemplate = HtmlService.createTemplateFromFile('email');
  htmlTemplate.dearAll = dearAll;
  htmlTemplate.message = message;
  htmlTemplate.subscriptions = subscriptions;
  htmlTemplate.subscriptionsNumbers = subscriptionsNumbers;
  htmlTemplate.subsComments = subsComments;
  htmlTemplate.operations = operations;
  htmlTemplate.operationsreport = operationsreport;
  htmlTemplate.customerService = customerService;
  htmlTemplate.weLognumbers = weLognumbers;
  htmlTemplate.csdata = csdata;
  htmlTemplate.csSubs = csSubs;
  htmlTemplate.replacementRequests = replacementRequests;
  htmlTemplate.replacmentDataTable = replacmentDataTable;
  htmlTemplate.lastComents = lastComents;
  htmlTemplate.endParagraph = endParagraph;
  htmlTemplate.name = name;
  const htmlForEmail=htmlTemplate.evaluate().getContent();

  console.log(htmlForEmail);


  const date = Utilities.formatDate(new Date(), "GMT+0", "dd/MM/yyyy")
  const emailBody = "Hi "+date+"\n \n"+customerService;
  const subject= "EOD for "+date
 Utilities.sleep(10000)
  Logger.log(emailAddress)
  GmailApp.sendEmail(emailAddress, subject, dearAll,{ htmlBody: htmlForEmail } );
}

这是我的 .HTML 模板,我知道它很简单,但它只是一个报告,所以请原谅它的简单性并原谅我如何构建我的 HTML 代码,我对此不太有经验。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
      <div>
        <div>
        </div>
          <p><?=dearAll1?></p> 
          <p><?=message1?></p> 
          <div style= "font-weight:bold"; ><?=subscriptions1?></div> 

<br>
   <table style="border-spacing:0px; border-collapse: collapse">

     <tbody>
   <?subscriptionsNumbers1.forEach(r=>{?>
       <tr>
        <th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= r[0] ?></th><th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= r[1] ?></th>
      </tr>
      <?})?>
      </tbody>
      </table>

      <p><?=subsComments1?><p/> 
          <br>     

      <div> </div>
       <p style= "font-weight:bold";> <?=operations?> </p>


        <table style="border-spacing:0px; border-collapse: collapse">
     <tbody>
   <?weLognumbers.forEach(r=>{?>
       <tr>
        <th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= r[0] ?></th><th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= r[1] ?></th><th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= r[2] ?></th>
      </tr>
      <?})?>
      </tbody>
      </table>

      <div> </div>
       <p> <?=operationsreport?> </p>

       <div> </div>
       <p style= "font-weight:bold";> <?=customerService?></p>
       <table style="border-spacing:0px; border-collapse: collapse">
      <tbody>
        <?csdata.forEach(n=>{?>
       <tr>
        <th style= "font-weight:normal; text-align:left; padding:7px; border:1px solid black;"><?= n[0] ?></th><th style= "font-weight:normal; border:1px solid black; padding:7px;"><?= n[1] ?></th>
      </tr>
      <?})?>

      </tbody>
      </table>
      <div> </div>
     <p style= "font-weight:bold";> <?=csSubs?></p>
     <p> <?=replacementRequests?></p>

      <table style="border-spacing:0px; border-collapse: collapse">
      <tbody>
        <?replacmentDataTable.forEach(n=>{?>
       <tr>
        <th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[0] ?></th><th style= "text-align:left; font-weight:normal; border:1px solid black; padding:5px;"><?= n[1] ?></th><th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[2] ?></th><th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[3] ?></th><th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[4] ?></th><th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[5] ?></th><th style= "font-weight:normal; border:1px solid black; padding:5px;"><?= n[6] ?></th>
      </tr>
      <?})?>
      </tbody>
      </table>

      <p><?=lastComents?><p/>
<br>
      <p><?=endParagraph?></p> 

      <p><?=name?></p> 


</div>
  </body>
</html> 

标签: javascriptgoogle-apps-scriptgoogle-sheetsgoogle-sheets-api

解决方案


我已经设法弄清楚为什么我要上htmlTemplate.evaluate().getContent();线了。在评论部分向奥列格大喊,因为他为我指明了正确的方向。

问题是我在旧脚本上运行新的 V8 引擎,当我将整个代码粘贴到所需的工作表中时,我正在运行与旧引擎不兼容的旧引擎谷歌应用程序脚本正在使用。

简单的答案是我升级到 V8 并且整个脚本运行良好。


推荐阅读