首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缺少/(斜杠)的无效正则表达式的问题

缺少/(斜杠)的无效正则表达式的问题
EN

Web Applications用户
提问于 2021-08-06 02:26:24
回答 1查看 1.5K关注 0票数 0

我有一个电子表格。我想从应用程序脚本发送电子邮件。

发送电子邮件的逻辑如下。

只发送1封电子邮件到A列(共名)中的每个唯一值,电子邮件的内容将包括所有行中的数据。仅在N列值为"Y“的情况下发送电子邮件。

预期结果:

该脚本只发送3封电子邮件:

电子邮件1:这是为了提醒您,以下房屋租赁合同"HCMC 1“已经到期。

  • 房屋租契于2021年7月11日届满
  • B屋租约将于2021年7月11日届满。

电子邮件2:这是为了提醒您,以下房屋租赁合同"HCMC 4“已经到期。

  • C屋租约于2021年7月11日届满。

电子邮件3:这是为了提醒您,以下房屋租赁合同"HCMC 5“已经到期。

  • 丁屋租约将于2021年7月11日届满。

下面有这个脚本,但会导致错误“缺少无效的正则表达式/”。请帮我改正一下。

代码语言:javascript
复制
function HLReminderAtYE1(){
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 at year end
 
/*var templateText = SS.getSheetByName("Template").getRange(1,1).getValue();*/
 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([row[1],row[7]]);
             const messageBody = HtmlService.createTemplateFromFile("email");
                   messageBody.clientname = client;
             mailto =  row[12];
             htmlforemail = messageBody.evaluate().getContent();
            }
        }) // end inside matching row loop
        ;

            MailApp.sendEmail(
               mailto,          // email address
               client + "- House lease contract expire", // Subject line
               "",{htmlBody: htmlforemail}); 
    })// end unique client loop
}

我有电子邮件模板如下

代码语言:javascript
复制
<!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 <?= clientname ?> has expired. Please action accordingly.</p>
      </div>
      <div> 
       <? messageList.forEach(r=>{ ?> 
        
        
       <p> <?= r[1]?> house lease contract end on <?= r[2] ?> </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 <?= clientname ?> has expired. Please help provide us with the updated house lease contracts. Thank you.</p>
      <div>
        <p> </p>
        <p> Should you have any question, please contact us. </p>
        <p> </p>
        <p> Best regards, </p>
      </div>
  </body>
</html>
EN

回答 1

Web Applications用户

发布于 2021-08-07 09:25:30

您的错误与regex无关(尽管有错误消息),但与模板中html的转义有关。

您的问题的原因是由于<?= of <?= clientname ?>。这是Printing scriptlet中使用的格式,但是任何转义都需要在Apps脚本中完成。如果脚本允许不受信任的用户输入,上下文转义是很重要的,但在脚本中并非如此。

您需要使用Force-printing scriptlet;语法类似于这个<?!= ... ?><?!= clientname ?>。这个脚本不允许用户输入;它包含您想要插入的所有HTML或脚本,它们完全按照指定的方式插入。

删除现有的HTML并替换以下内容

代码语言:javascript
复制
<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 <?!= clientname ?> has expired. Please action accordingly.</p>
      </div>
      <div>      
       <p> <?!= assignee?> house lease contract end on <?!= contractenddate ?> </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 <?!= clientname ?> has expired. Please help provide us with the updated house lease contracts. Thank you.</p>
      <div>
        <p> </p>
        <p> Should you have any question, please contact us. </p>
        <p> </p>
        <p> Best regards, </p>
      </div>
  </body>
</html>  

脚本

删除messageList.push([row[1],row[7]]);

messageBody.clientname = client;之后添加两行新行

  • messageBody.assignee = row[1];
  • messageBody.contractenddate = row[7];

您将注意到assigneecontractenddate都在html中使用。

@Tanaike在这个答案 on StackOverflow中发现了这个问题。

票数 1
EN
页面原文内容由Web Applications提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://webapps.stackexchange.com/questions/158250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档