【问题标题】:Error in Google Apps Script when HTML form input field is empty当 HTML 表单输入字段为空时,Google Apps 脚本中的错误
【发布时间】:2019-10-18 21:45:14
【问题描述】:

我的Google Apps ScriptHTML form 接收数据,然后向客户发送电子邮件确认。它从input type=email 获取电子邮件地址,如果此输入字段留空,则脚本会产生错误。

这是我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()
  });
}

我该如何解决这个问题,以便当我引用的 form input 字段为空时,我的脚本不会产生错误?

(我知道解决此问题的一种方法是将required 属性放在input 上,但该字段是可选的,因此无法解决我的问题。)

【问题讨论】:

    标签: javascript google-apps-script web-applications


    【解决方案1】:
    • 您不希望在未输入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的值是否为email。

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多