如何在MailApp消息中包含IF语句?

时间:2019-03-29 15:14:20

标签: javascript google-apps-script google-sheets gmail google-form

我已经创建了一个Google表单,现在创建了一个脚本来向发件人发送自动电子邮件。 MailApp函数工作正常,但是我想知道是否可以在消息(htmlbody)中包含IF语句,以便消息根据变量的值而变化。

我已经包含了IF语句,但是我无法使文本包含在消息中。

// Email content
  var subject = FormName;
  var message = 

      "Hi,<br><br>"+

      "Thank you for submitting the Questionnaire<br><br>"+

      "Here are your responses:<br><br>"+

      "1. "+Question1+": "+Response1+"<br>"+
      "2. "+Question2+": "+Response2+"<br>"+
      "3. "+Question3+": "+Response3+"<br>"

   if(Response1 == "Yes")
{
"Since you answered yes to question 1, you need to do XYZ."
}

   MailApp.sendEmail(emailAddress, subject, message,{'htmlBody':message});
}

我尝试在最后一个问题和响应组合后添加一个+,但这给了我一个语法错误。上面的代码省略了“因为回答为是,所以需要执行XYZ”。在电子邮件消息中,这不是期望的结果。

1 个答案:

答案 0 :(得分:0)

您可以检查javascript运算符,尤其是+ =,因为此运算符采用变量的值并将其附加新值。

例如:

var ex1 = "Hey";
ex1 += " Friend";
// ext1 = "Hey Friend";

如您所见,您可以采用此概念并将其用于示例中。那你会有类似的东西

// Email content
var subject = FormName;
var message = 

  "Hi,<br><br>"+

  "Thank you for submitting the Questionnaire<br><br>"+

  "Here are your responses:<br><br>"+

  "1. "+Question1+": "+Response1+"<br>"+
  "2. "+Question2+": "+Response2+"<br>"+
  "3. "+Question3+": "+Response3+"<br>";

 if(Response1 == "Yes")
  {
   message += "Since you answered yes to question 1, you need to do XYZ."
  }

  MailApp.sendEmail(emailAddress, subject, message,{'htmlBody':message});
}

希望您能明白。