简体   繁体   English

当 HTML 表单输入字段为空时,Google Apps 脚本出错

[英]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 ScriptHTML 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上,但该字段是可选的,所以这不能解决我的问题。)

  • You don't want to occur an error when 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.请认为这只是几个答案之一。

Modified script:修改后的脚本:

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.
  }
}
  • From your question, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM