GAS的新手。我有一个Google Form可以输入电子表格。在观看了教程视频并阅读了其他帖子之后,我尝试创建一个脚本,该脚本将向第二列中的地址发送电子邮件,并根据另一列中的是或否发送另一封电子邮件(第25列)。它还包括另一列(26),我希望将发送电子邮件的日期填入其中,以确保每次运行此脚本时都不会发送重复的邮件。我已调试,但没有出现任何错误,但无法正常工作-我尚未收到电子邮件。帮助将不胜感激!这是一个示例电子表格:https://docs.google.com/spreadsheets/d/1AKaSOk1ZbnfKeadgB_mugnqplKzJJ3bDQ_KsRY0sMvQ/edit?usp=sharing
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Field Trip Requests");
var data = responses.getRange(2,1,responses.getLastRow()-1,25).getValues();
data.forEach(function(row,i) {
// variables
var recipient = row[1];
var destination = row[6];
var approval = row[24];
var emailSent = row[25];
if(emailSent == ' ') {
if(approval == "Y") {
var body = "Your field trip request for " +
destination +
" has been approved! If you requested transportation, Najma will make arrangements and contact you if she requires more information." +
"<br><br>" +
"Cheers," +
"<br><br>" +
"Boaty McBoatface";
}
else if(approval == "N") {
var body = "Your field trip request for " +
destination +
" has not been approved. Please come and see me and we can chat!" +
"<br><br>" +
"Cheers," +
"<br><br>" +
"Boaty McBoatface";
}
var subject = "Your Field Trip Request";
MailApp.sendEmail(recipient, subject, body)
var d = new Date();
responses.getRange(i + 1, 25).setValue(d);
}
})
}
如果此var data = responses.getRange(2,1,responses.getLastRow()-1,25).getValues();
是正确的,则不存在行[25],如下所示:
data.forEach(function(row,i) {
// variables
var recipient = row[1];
var destination = row[6];
var approval = row[24];
var emailSent = row[25];
行从零开始到24结束
行是一个数组
也许这就是您想要的:
function sendEmail() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Field Trip Requests");
var data=sh.getRange(2,1,responses.getLastRow()-1,25).getValues();
data.forEach(function(row,i) {
var recipient=row[0];
var destination=row[5];
var approval=row[23];
var emailSent=row[24];
if(emailSent=='' && approval=='Y') {
var body=Utilities.formatString('Your field trip request for %s has been approved! If you requested transportation, Najma will make arrangements and contact you if she requires more information. <br><br>Cheers<br><br>Boaty McBoatface',destination);
}else if(emailSent=='' && approval == "N") {
var body=Utilities.formatString('Your field trip request for %shas not been approved. Please come and see me and we can chat!<br><br>Cheers,<br><br>Boaty McBoatface',destination);
}
var subject="Your Field Trip Request";
MailApp.sendEmail(recipient, subject, body);
sh.getRange(i+1,25).setValue(Utilities.formatDate(new Date, Session.getScriptTimeZone(), "MM/dd/yyyy"));
})
}
脚本的一个问题是,当它应该使用基于0的索引时,您正在使用基于1的索引
var recipient = row[1];
var destination = row[6];
var approval = row[24];
var emailSent = row[25];
上面是因为JavaScript对数组元素(以及其他东西)使用基于0的索引,所以代替了上面的用法
var recipient = row[0];
var destination = row[5];
var approval = row[23];
var emailSent = row[24];
另一个问题是以下情况:
emailSent == ' ' /* is emailSent equal to a blank space? */
应该是
emailSent == '' /* is emailSent equal to an empty string? */
以上是因为getValues()
为空单元格返回一个空字符串