[英]How to calculate a variable in Google spreadsheet script, then return in confirmation email
I'm a little new at this so thanks in advance for any help! 我对此有些陌生,因此在此先感谢您的帮助! I've gotten a confirmation email working based on a submission trigger/event for a Google Form to a spreadsheet.
我收到了一封确认电子邮件,该电子邮件基于将Google表单提交到电子表格的提交触发/事件进行工作。 However, I want to be able to have the script grab two fields from the user's submission and then calculate a field to be included in the confirmation email.
但是,我希望脚本可以从用户提交的内容中抓取两个字段,然后计算要包含在确认电子邮件中的字段。 Once I added in the code to calculate the field (cost1), the confirmation email stopped sending.
一旦添加了用于计算字段(cost1)的代码,确认电子邮件便停止发送。 I checked the execution transcript and it ran much more quickly and said it executed successfully, but no email sent... Is there something wrong with my code or am I asking for the script to do too much?
我检查了执行记录,它运行得更快,并说成功执行了,但是没有发送电子邮件...我的代码有问题吗?还是我要求脚本执行太多操作?
The script: 剧本:
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);
}
}
return
is always the last thing to run in a JS functions and is short-circuiting your function before the MailApp.sendEmail
is run. return
总是在JS函数中运行的最后一件事,并且在运行MailApp.sendEmail
之前使函数短路。 Also, variables in JS are function scoped . 同样,JS中的变量是函数范围的 。 So, you don't need to declare
var
inside of your if
blocks. 因此,您无需在
if
块内部声明var
。
Below I've used the ternary operator [test ? if true : else]
下面我使用了三元运算符
[test ? if true : else]
[test ? if true : else]
, and set the long string to a variable. [test ? if true : else]
,然后将长字符串设置为变量。
Your cost calculation should look something like this: 您的费用计算应如下所示:
var dcebw = "Driver's Education Classroom & Behind the Wheel";
var cost1 = session1 === dcebw ? 400 : 180;
cost1 += session2 > 0 ? 180 : 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.