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

我Google Apps Script从 接收数据HTML 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,但该字段是可选的,因此不能解决我的问题。)


呼如林
浏览 129回答 1
1回答

慕村9548890

您不希望在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是电子邮件。如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript