首页 > 解决方案 > Google App script SyntaxError: Unexpected token '<' when running script

问题描述

有人知道这里有什么问题吗?它已经工作了多次,即使

const htmlForEmail = htmlTemplate.evaluate().getContent(); 现在它似乎抛出了一个错误:

错误:错误

SyntaxError: Unexpected token '<'electrical @electric.gs:24 我在运行时版本-runtimeVersion-“V8”

我的谷歌应用脚​​本代码(html代码在它下面):

 function electrical() {

    const ss = SpreadsheetApp.getActiveSpreadsheet ();
    
    const ws = ss.getSheetByName("Bid Request");

    const h1 = ws.getRange("G4").getValue();
    const headers = ws.getRange("G5:H5").getValues();
    const project = headers [0][0];
    const response = headers [0][1];

    const lr =ws.getLastRow();
    const tableRangeValues = ws.getRange(6,2,lr-6,2).getDisplayValues();

    const htmlTemplate = HtmlService.createTemplateFromFile("email");
    htmlTemplate.h1 = h1;
    htmlTemplate.headers = headers;
    htmlTemplate.project = project;
    htmlTemplate.response = response;
    htmlTemplate.tableRangeValues = tableRangeValues;

    const htmlForEmail = htmlTemplate.evaluate().getContent();

    console.log(htmlForEmail);

    GmailApp.sendEmail(
    "example@gmail.com",
    "Bid Request",
    "Please",
    {htmlBody: htmlForEmail}
 
   );

  
   }

html:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
 <div>
 </div>
 <div>
    
  <h1><?= h1 ?></h1>
  <div></div>
  <table>

  <thead>
  <tr>
  <th><?= project ?></th><th><?= response></th>
  </tr>
  </thead>

  <tbody>

  <? tableRangeValues.forEach(r => { ?>
  <tr>
  <td><?= r[0] ?></td><td><?= r[1] ?></td>
  </tr>
  <?}) ?>

  </tbody>

      
  </div>
  </div>

  </body>
  </html> 

出于某种原因,html 不允许我显示我有 header/body 标签和 html 定义的文档标签,但我这样做了!

标签: google-apps-scriptunexpected-token

解决方案


您的 html 代码中有错字

<thead>
  <tr>
  <th><?= project ?></th><th><?= response></th>
  </tr>
  </thead>

应该是这样的

<thead>
  <tr>
  <th><?= project ?></th><th><?= response ?></th>
  </tr>
  </thead>

希望能解决你的问题


推荐阅读