我的 Google Apps 脚本
从 HTML
表单
接收数据,然后向客户发送电子邮件确认。它从 input type=email
获取电子邮件地址,如果此输入字段留空,脚本会产生错误。
这是我的 Google Apps 脚本
的相关部分:
function doPost(e) {
var email = e.parameter.Email;
MailApp.sendEmail({
to: email,
subject: "Submission Received",
body: "We have received your submission. Thank you.",
htmlBody: HtmlService.createHtmlOutputFromFile(
"Confirmation"
).getContent()
});
}
我该如何解决这个问题,以便当我引用的 form
input
字段为空时,我的脚本不会产生错误?
(我知道解决这个问题的一种方法是将 required
attribite 放在 input
上,但是该字段是可选的,所以这不能解决我的问题。 )
最佳答案
- 您不希望在未输入
Email
时发生错误。
如果我的理解是正确的,下面的修改怎么样?请将此视为几个答案之一。
修改后的脚本:
function doPost(e) {
if ("Email" in e.parameter && e.parameter.Email != "") { // Added
var email = e.parameter.Email;
MailApp.sendEmail({
to: email,
subject: "Submission Received",
body: "We have received your submission. Thank you.",
htmlBody: HtmlService.createHtmlOutputFromFile(
"Confirmation"
).getContent()
});
} else { // Added
// If "Email" is not inputted, for example, you can do something here.
}
}
- 根据您的问题,
Email
被输入到电子邮件类型中。所以我想不需要确认Email
的值是否是邮箱。
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
关于javascript - HTML 表单输入字段为空时 Google Apps 脚本出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58458563/