我需要根据从谷歌表单上的一个特定问题中选择的不同条件,通过 Appscript 向不同的人发送电子邮件。例如。所有EBO选择发送到电子邮件addy 1,所有非EBO选择发送到电子邮件addy 2,然后所有选择发送到电子邮件addy 3。下面是我所拥有的,但无论在表单上选择什么选项,它都只发送第一个 if 语句的电子邮件。
function variedEmails() {
var aSheet = SpreadsheetApp.getActiveSpreadsheet();
//Take the last row. Row is converted to string and put into an array
var lastSheet = aSheet.getSheetValues(aSheet.getLastRow(), 1, 1, aSheet.getLastColumn());
var lastString = lastSheet.toString();
var data = lastString.split(",");
//Take the first column (header values). Row is converted to string and put into an array
var headerSheet = aSheet.getSheetValues(1, 1, 1, aSheet.getLastColumn());
var headerString = headerSheet.toString();
var headers = headerString.split(",");
//omit the time and Timezone from timestamps in form
var dateTime = data[0].split(" ");
var dateTime1 = data[9].split(" ");
data[0] = dateTime[0] + " " + dateTime[1] + " " + dateTime[2] + " " + dateTime[3];
data[9] = dateTime1[0] + " " + dateTime1[1] + " " + dateTime1[2] + " " + dateTime1[3];
//string to build an HTML table
var table = "<table style='border: 1px solid border-collapse: collapse'>";
for( var i = 0; i < headers.length; i++ ) {
//determine if cell is blank, and only add data if data exists for that cell
if( data[i] != "" ) {
table += "<tr style='border: 1px solid'><td style='border: 1px solid'>"+ headers[i] + "</td><td style='border: 1px solid'>" + data[i] + "</td></tr>";
}
}
table += "</table>";
if(data[4] == "Increase Usage Priority (Ex: EBO)"){
MailApp.sendEmail({
to: "[email protected]",
subject: "Usage Priority - Mass Change Request Form Response",
htmlBody: table, noReply: true
})
if(data[4] != "Increase Usage Priority (Ex: EBO)"){
MailApp.sendEmail({
to: "[email protected]",
subject: "Mass Change Request Form Response",
htmlBody: table, noReply: true
})
MailApp.sendEmail({
to: "[email protected]",
subject: "All - Mass Change Request Form Response",
htmlBody: table, noReply: true
})
}
}
}
我实际上是在盯着它看了一个小时后才弄清楚这一点的。我在错误的位置放置了一些右括号,这导致 if 语句相互嵌套,而不是让脚本单独读取每个 if 语句。正确的代码如下:
if(data[4] == "Increase Usage Priority (Ex: EBO)"){
MailApp.sendEmail({
to: "[email protected]",
subject: "Usage Priority - Mass Change Request Form Response",
htmlBody: table, noReply: true
})
}
if(data[4] != "Increase Usage Priority (Ex: EBO)"){
MailApp.sendEmail({
to: "[email protected]",
subject: "Mass Change Request Form Response",
htmlBody: table, noReply: true
})
}
MailApp.sendEmail({
to: "[email protected]",
subject: "All - Mass Change Request Form Response",
htmlBody: table, noReply: true
})
}