我有一个电子表格。我想从应用程序脚本发送电子邮件。
发送电子邮件的逻辑如下。
只发送1封电子邮件到A列(共名)中的每个唯一值,电子邮件的内容将包括所有行中的数据。仅在N列值为"Y“的情况下发送电子邮件。
预期结果:
该脚本只发送3封电子邮件:
电子邮件1:这是为了提醒您,以下房屋租赁合同"HCMC 1“已经到期。
电子邮件2:这是为了提醒您,以下房屋租赁合同"HCMC 4“已经到期。
电子邮件3:这是为了提醒您,以下房屋租赁合同"HCMC 5“已经到期。
下面有这个脚本,但会导致错误“缺少无效的正则表达式/”。请帮我改正一下。
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
}我有电子邮件模板如下
<!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>发布于 2021-08-07 09:25:30
您的错误与regex无关(尽管有错误消息),但与模板中html的转义有关。
您的问题的原因是由于<?= of <?= clientname ?>。这是Printing scriptlet中使用的格式,但是任何转义都需要在Apps脚本中完成。如果脚本允许不受信任的用户输入,上下文转义是很重要的,但在脚本中并非如此。
您需要使用Force-printing scriptlet;语法类似于这个<?!= ... ?>或<?!= clientname ?>。这个脚本不允许用户输入;它包含您想要插入的所有HTML或脚本,它们完全按照指定的方式插入。
删除现有的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>
<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];您将注意到assignee和contractenddate都在html中使用。
@Tanaike在这个答案 on StackOverflow中发现了这个问题。
https://webapps.stackexchange.com/questions/158250
复制相似问题