【问题标题】:How to include an IF statement within a MailApp message?如何在 MailApp 消息中包含 IF 语句?
【发布时间】:2019-03-29 15:14:19
【问题描述】:

我创建了一个 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”。在电子邮件中,这不是预期的结果。

【问题讨论】:

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


    【解决方案1】:

    您可以检查 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});
    }
    

    我希望你能明白。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      相关资源
      最近更新 更多