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

标签 javascript google-apps-script web-applications

我的 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/

相关文章:

javascript - 是否需要检查元素是否具有属性?

google-apps-script - 如何根据 Google Docs 电子表格中的当前日期设置行的背景颜色?

java - Web 应用程序上的 Eclipse tomcat 启动 java.lang.ClassNotFoundException : org. mockito.Mockito

java - 如何使用 File 对象在 maven webapp 中加载属性文件?

javascript - 哪些浏览器支持 HTML5 WebSocket API?

javascript - 未捕获的类型错误 : Cannot read property 'style' of null concerning a script in an echo

javascript - redux-actions Typescript 声明一个 Action

javascript - Firebase orderByKey() 键的顺序不一致

google-apps-script - 解析我从 UrlFetch 收到的 XML 数据

javascript - Google Sheets 带注释的时间线显示精确值?