【问题标题】:Google script - Gmail.App, email body with if statementGoogle 脚本 - Gmail.App,带有 if 语句的电子邮件正文
【发布时间】:2021-12-30 14:54:08
【问题描述】:

我正在尝试根据某些条件从 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,感谢您的预订。 您已预订:一些 - 旅游 旅游详情: 旅行日期:2021 年 12 月 24 日星期五"

它在第一个 if 语句处停止。在这个特定的例子中,if 语句 == true,所以它应该添加它的内容以便继续脚本。

【问题讨论】:

    标签: google-apps-script google-sheets gmail-api


    【解决方案1】:

    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);
    
    

    输出:

    【讨论】:

    • 非常感谢,这解决了问题。就是这么简单... :)
    • 没问题@KrzysztofB。如果这回答了您的问题,请单击左侧的接受按钮(复选图标)。通过这样做,社区中可能与您有同样担忧的其他人将知道他们的问题可以得到解决。如果您无法使用接受按钮,请随时告诉我。 how to accept answer
    • 还在学习stackoverflow。感谢您的指点,我已经接受了您的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2011-06-15
    • 2015-08-06
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多