【问题标题】:Syntax error HtmlService.createTemplateFromFile().evaluate().getContent();语法错误 HtmlService.createTemplateFromFile().evaluate().getContent();
【发布时间】:2020-06-05 17:47:13
【问题描述】:

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

我创建了一个脚本,该脚本创建了一封电子邮件,其中包含我输入到谷歌表格中的内容,该表格从我制作的 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> 

【问题讨论】:

  • 不太清楚,但this 会回答您的问题吗? HTML 和代码看起来不错,但是您的脚本的运行时(检查 appsscript.json)可能会混淆 - 是 V8 吗?
  • @Cooper - 我认为这 确切的错误消息 - 我开始经常处理这个问题 - 关于模板期间发生的事情的消息评估不是那么清楚(虽然我可能错了,但我清楚地记得这个确切的形式)。
  • 您可以创建所有 html 服务器端并在 dom 加载后使用 window.onload 或 $(function 进行加载,它会慢一些,但编写起来容易得多。
  • @OlegValter 很抱歉花了一些时间回复这个问题。我现在才有机会再次解决这个问题。你完全正确,我在我不知道的旧脚本上运行 V8,而我的新工作表正在运行旧引擎。感谢您通过检查appsscript.json 为我指出正确的方向。尽管一个弹出窗口一直盯着我看,告诉我我正在运行 V8。
  • 没问题(这是一个很常见的问题,但错误消息根本没有帮助) - 我将问题标记为重复:Script Error on internal function when calling another function in a library

标签: javascript google-apps-script google-sheets google-sheets-api


【解决方案1】:

我已经设法弄清楚为什么我在htmlTemplate.evaluate().getContent(); 线上得到了这个问题。在 cmets 部分向 Oleg 大喊,因为他为我指明了正确的方向。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2023-03-09
    • 1970-01-01
    • 2021-06-08
    • 2018-07-06
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多