gpt4 book ai didi

javascript - MailApp 的 html 模板中 undefined variable

转载 作者:行者123 更新时间:2023-12-03 00:45:36 26 4
gpt4 key购买 nike

我正在尝试使用 MailApp 发送 HTML 电子邮件。这是我将从中提取数据的表:

enter image description here

在阅读了 SO 上的文档和帖子后,我仍然无法确定为什么发出的电子邮件在变量部分显示未定义。

enter image description here

如果有人能指出我正确的方向,我将不胜感激。

这些是我的代码:

sendHtmlEmail.gs

function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = ss.getLastRow();

var quotaLeft = MailApp.getRemainingDailyQuota();
if((lastRow-1) > quotaLeft){
Browser.msgBox("You have " + quotaLeft + " email quota left and you're trying to send " + (lastRow-1) + " emails. Emails were not sent.")
} else {
for (var row = 2; row <= lastRow; row++){
var currentEmail = ss.getRange(row, 4).getValue();
var currentContact = ss.getRange(row, 3).getValue();
var currentNewSize = ss.getRange(row,8).getValue();
var currentSub = ss.getRange(row, 9).getValue();
var returnData = [currentContact, currentNewSize, currentSub];
var html = HtmlService.createTemplateFromFile('htmlTemplate');

html.data = returnData;
var template = html.evaluate().getContent();
var subjectLine = "Important notice";
MailApp.sendEmail({to: currentEmail, subject: subjectLine, htmlBody: template});
} //close for loop
Browser.msgBox("You have successfully sent " + (lastRow-1) + " emails.")
} //close else statement
}

htmlTemplate.html

<!DOCTYPE html><html><head><base target="_top"></head><body style="text-align: center;font-family: Arial;">
<div id="center" style="width:300px;border: 2px dotted grey;background:#ececec;margin:25px;margin-left:auto;
margin-right:auto;padding:15px;"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Simple_Alert.svg/600px-Simple_Alert.svg.png"
width="180" style="margin:10px 0px"><br /><div style=" border: 2px dotted grey;background:white;margin-right:auto; margin-left:auto;
padding:10px;"><h2>Alert</h2><h3>Important Notice <br /><br />
+ <?= data.currentContact ?> + '<br />' + <?= data.currentNewSize ?> + '<br />' + <?= data.currentSub ?> + '<br /></h3></div></body></html>'

我知道这与 htmlTemplate data.currentNewSize 等有关,但在调整我的代码后,它们仍然显示未定义,如图所示。

最佳答案

您在方法中使用数组,而模板需要一个对象。

替换

    var returnData = [currentContact, currentNewSize, currentSub];

    var returnData = {currentContact: currentContact, currentNewSize: currentNewSize, currentSub: currentSub}

关于javascript - MailApp 的 html 模板中 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53261064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com