我对此有点新意,所以提前感谢任何帮助!我收到了一封确认电子邮件,其工作原理是基于Google表单提交触发/事件的电子表格。但是,我希望能够让脚本从用户的提交中获取两个字段,然后计算要包含在确认电子邮件中的字段。一旦我在代码中添加了计算字段(cost1),确认电子邮件就会停止发送。我检查了执行记录,它运行得更快,说它执行成功,但没有发送电子邮件......我的代码有问题,还是我要求脚本做太多?
剧本:
function EmailFormConfirmation() {
var sheetName = "Form Responses";
var columnNumber = 3;
var subject = "Summer School Registration Confirmation";
var gn = 2;
var sfn = 4;
var sln = 5;
var s1 = 13;
var s2 = 14;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName(sheetName);
var numRows = dataSheet.getLastRow();
var email = dataSheet.getRange(numRows,columnNumber).getValue();
var guardianName = dataSheet.getRange(numRows,gn).getValue();
var studentFirst = dataSheet.getRange(numRows,sfn).getValue();
var studentLast = dataSheet.getRange(numRows,sln).getValue();
var session1 = dataSheet.getRange(numRows,s1).getValue();
var session2 = dataSheet.getRange(numRows,s2).getValue();
var emailPattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/; //email reg expression
var validEmailAddress = emailPattern.test(email);
var cost1 = 0
if(session1 === "Driver's Education Classroom & Behind the Wheel")
{
var cost1 = cost1+400
}
else
{
var cost1 = cost1+180
}
if(session2 > 0)
{
var cost1 = cost1+180
return(cost1)
}
else
{
return(cost1)
};
var body = "Dear" + " " + guardianName + "," +
"\n\nThank you for submitting summer school registration for" + " " + studentFirst + " " + studentLast + "." +
"\n\nYou have selected the following for Session 1:" + " " + session1 +
"\n\nYou have selected the following for Session 2:" + " " + session2 +
"\n\nIf any changes need to be made, please contact the office." +
"\n\nYour total fees due for summer school are:" + " " + "$" + cost1 +
"\n\n\nSummer School Registrar";
if (validEmailAddress) {
MailApp.sendEmail(email, subject,body);
}
}
答案 0 :(得分:1)
return
始终是在JS函数中运行的最后一件事,并且在MailApp.sendEmail
运行之前使您的函数短路。此外,JS中的变量是功能范围。因此,您无需在var
块内声明if
。
下面我使用了三元运算符[test ? if true : else]
,并将长字符串设置为变量。
您的费用计算应如下所示:
var dcebw = "Driver's Education Classroom & Behind the Wheel";
var cost1 = session1 === dcebw ? 400 : 180;
cost1 += session2 > 0 ? 180 : 0;