google-apps-script - 我可以在 Google Apps Script MailApp 中执行条件语句吗?
问题描述
我认为这会相当简单,但我不能有条件地在 MailApp 中工作。我想要做的就是通过电子邮件发送一个由表单创建的地址,如果有一个“address2”条目来添加该行。如果没有条件,大多数地址在电子邮件中都会有换行符。我想要类似于下面代码的东西,但 Google Script 不会让我保存它,因为 if 语句会引发语法错误。
MailApp.sendEmail({
to: "johndoe@gmail.com",
subject: "New Client",
htmlBody: "<p>New Client</p>" +
"<p>Contact Name: " + ContactName + "<br>" +
"Address:<br>" +
"<br>" +
address1 + "<br>" +
if(address2 !== 0)
{address2 + "<br>" +}
city + "," + state + zip "</p>".
});
请指教。谢谢。
解决方案
"<br>" + if(address2 !== 0) {address2 + "<br>" +} city + "," + state + zip "</p>"
不是有效的 JavaScript。if 语句不是可以在公式中使用的运算符;这就是三元运算符condition ? if_true : if_false
的用途。例子:
"<br>" + (address2 !== 0 ? address2 + "<br>" : "") + city + "," + state + zip + "</p>"
推荐阅读
- python - 如何在 Python 中检查视频是否有声音?
- css - 如何在 Angular 组件中设置 ::before 伪元素的 CSS 属性?
- ios - 在 IBAgent NSInternalInconsistencyException 之后无法在 Xcode 12 中编辑情节提要:无法创建工作间隔
- flutter - 如何从 Flutter 移动应用程序控制 Rapsberry pi?
- excel - 如何提取约会,包括重复?
- python - 操作/复制数据框(熊猫)并仅维护每个月的最后一天
- python - 如何将Kafka的时间戳转换为日期时间?
- java - 等效于Java中的时刻反应
- python - 如何使用 Python @singledispatch 注册 typing.Callable?
- c++ - c ++获取unordered_map向量的unordered_map的值