[英]Error in Google Apps Script when HTML form input field is empty
My Google Apps Script
receives data from an HTML
form
, then sends an email confirmation to a customer.我的
Google Apps Script
从HTML
form
接收数据,然后向客户发送 email 确认。 It gets the email address from input type=email
, and if this input field is left blank, the script produces an error.它从
input type=email
获取 email 地址,如果此输入字段留空,则脚本会产生错误。
Here is the relevant part of my Google Apps Script
:这是我的
Google Apps Script
的相关部分:
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()
});
}
How can I solve this so that when my referenced form
input
field is empty my script doesn't produce an error?我该如何解决这个问题,以便当我引用的
form
input
字段为空时,我的脚本不会产生错误?
(I know that one way to solve this is to put required
attribite on the input
, but the field is optional, so that doesn't fix my problem.) (我知道解决这个问题的一种方法是将
required
属性放在input
上,但该字段是可选的,所以这不能解决我的问题。)
Email
is not inputted.Email
时发生错误。 If my understanding is correct, how about the following modification?如果我的理解是正确的,那么下面的修改呢? Please think of this as just one of several answers.
请认为这只是几个答案之一。
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
is inputted to the type of email.Email
输入到 email 的类型。 So I thought that it is not required to confirm whether the value of Email
is the email.Email
的值是否是email。 If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.