Google 脚本 - Gmail.App,带有 if 语句的电子邮件正文
Google script - Gmail.App, email body with if statement
我正在尝试根据某些条件从 Google 电子表格创建旅游确认电子邮件。
我创建了一个变量,如果语句和 ID 未显示在电子邮件中,我将添加该变量。
var mailContent =
'Hello '+customerName +', thank you for the booking.<br><br>'+ "You have booked: "+tourName +" - "+tourType
+ "<br><br><b>Tour Details:</b> "
+"<br>Travel Date: "+tourDate
if (tourLang !== "--------"){
+"<br>Language: "+tourLang
}
+"<br>Pickup adress: "+tourPickup
if (tourTime !== ""){
+"<br>Pickup Time: "+tourTime
}
+"<br>Number of Participants: "+tourPax
+"<br><br> <b>Contact Details: </b>"
+"<br> Main customer: "+customerName
+"<br> Phone: "+customerPhone
if (customerOtherNames !== ""){
+"<br> Names of all Participants: "+customerOtherNames
}
GmailApp.sendEmail(
customerEmail,
'Booking Confirmation '+voucherRealNumber,
mailContent,
{
htmlBody: mailContent,
attachments: [createPDF],
bcc: "test@test.com"
});
电子邮件看起来像这样:
“您好XYZ,感谢您的预订。
您已经预订:Some - Tour
旅游详情:
旅行日期:2021 年 12 月 24 日星期五
它在第一个 if 语句处停止。在这个特定的例子中,if 语句 == true,所以它应该添加它的内容以便继续脚本。
您需要指定在 if
语句中追加时使用的变量。您没有指定要附加字符串的变量,这就是它没有显示预期输出的原因。请参阅下面的示例脚本:
脚本:
customerName = 'XYZ';
tourName = 'Some';
tourType = 'Tour';
tourDate = 'Fri Dec 24 2021';
tourLang = 'lang';
tourTime = 'time';
customerOtherNames = 'other names';
tourPickup = 'pickup';
tourPax = 'pax';
customerPhone = 'phone';
var mailContent =
'Hello ' + customerName + ', thank you for the booking.<br><br>' + "You have booked: " + tourName + " - " + tourType
+ "<br><br><b>Tour Details:</b> "
+ "<br>Travel Date: " + tourDate
if (tourLang !== "--------") {
mailContent += "<br>Language: " + tourLang
}
mailContent += "<br>Pickup adress: " + tourPickup
if (tourTime !== "") {
mailContent += "<br>Pickup Time: " + tourTime
}
mailContent += "<br>Number of Participants: " + tourPax
+ "<br><br> <b>Contact Details: </b>"
+ "<br> Main customer: " + customerName
+ "<br> Phone: " + customerPhone
if (customerOtherNames !== "") {
mailContent += "<br> Names of all Participants: " + customerOtherNames
}
Logger.log(mailContent);
输出:
我正在尝试根据某些条件从 Google 电子表格创建旅游确认电子邮件。
我创建了一个变量,如果语句和 ID 未显示在电子邮件中,我将添加该变量。
var mailContent =
'Hello '+customerName +', thank you for the booking.<br><br>'+ "You have booked: "+tourName +" - "+tourType
+ "<br><br><b>Tour Details:</b> "
+"<br>Travel Date: "+tourDate
if (tourLang !== "--------"){
+"<br>Language: "+tourLang
}
+"<br>Pickup adress: "+tourPickup
if (tourTime !== ""){
+"<br>Pickup Time: "+tourTime
}
+"<br>Number of Participants: "+tourPax
+"<br><br> <b>Contact Details: </b>"
+"<br> Main customer: "+customerName
+"<br> Phone: "+customerPhone
if (customerOtherNames !== ""){
+"<br> Names of all Participants: "+customerOtherNames
}
GmailApp.sendEmail(
customerEmail,
'Booking Confirmation '+voucherRealNumber,
mailContent,
{
htmlBody: mailContent,
attachments: [createPDF],
bcc: "test@test.com"
});
电子邮件看起来像这样:
“您好XYZ,感谢您的预订。 您已经预订:Some - Tour 旅游详情: 旅行日期:2021 年 12 月 24 日星期五
它在第一个 if 语句处停止。在这个特定的例子中,if 语句 == true,所以它应该添加它的内容以便继续脚本。
您需要指定在 if
语句中追加时使用的变量。您没有指定要附加字符串的变量,这就是它没有显示预期输出的原因。请参阅下面的示例脚本:
脚本:
customerName = 'XYZ';
tourName = 'Some';
tourType = 'Tour';
tourDate = 'Fri Dec 24 2021';
tourLang = 'lang';
tourTime = 'time';
customerOtherNames = 'other names';
tourPickup = 'pickup';
tourPax = 'pax';
customerPhone = 'phone';
var mailContent =
'Hello ' + customerName + ', thank you for the booking.<br><br>' + "You have booked: " + tourName + " - " + tourType
+ "<br><br><b>Tour Details:</b> "
+ "<br>Travel Date: " + tourDate
if (tourLang !== "--------") {
mailContent += "<br>Language: " + tourLang
}
mailContent += "<br>Pickup adress: " + tourPickup
if (tourTime !== "") {
mailContent += "<br>Pickup Time: " + tourTime
}
mailContent += "<br>Number of Participants: " + tourPax
+ "<br><br> <b>Contact Details: </b>"
+ "<br> Main customer: " + customerName
+ "<br> Phone: " + customerPhone
if (customerOtherNames !== "") {
mailContent += "<br> Names of all Participants: " + customerOtherNames
}
Logger.log(mailContent);