如何在Google表单中停止Onsubmit

时间:2013-06-24 18:34:59

标签: javascript webforms google-apps-script google-form

在Google表单中,我编写了一个检查字段的验证检查,如果它超过了计数,那么我需要停止表单提交并提醒用户该错误。我能够将错误文本添加到div,但无法停止表单提交。

如何停止表单提交。我已经尝试过e.preventDefault()但它没有用。任何人都可以帮忙。我附上了在谷歌表单中提交表单事件时调用的代码。

function onFormSubmit(e) {
  Logger.log("test log", "testing");
var timestamp = e.values[0];
var programname = e.values[1];
var center = e.values[2];
var fullname = e.values[3];
var board = e.values[4];
var programtype = e.values[5];
var programobjective = e.values[6];
var calltoaction = e.values[7];
var begindate = e.values[8];
var enddate = e.values[9];
var marketing = e.values[10];
var budget = e.values[11];
var expectedparticipation = e.values[12];
var registrationfee = e.values[13];
var announcement = e.values[14];
var insightannouncement = e.values[15];
var additionallanguage = e.values[16];
var checklist = e.values[17];
var speakerprofile = e.values[18];
var noticeboarditem = e.values[19];
var toAddress = e.values[20];
var programcategory = e.values[21];
var liability = e.values[22];
var datesofannouncement = e.values[23];
var explaination = e.values[24];
var additionalquestion = e.values[25];
var location = e.values[26];
var externalpartners = e.values[27];
var intergenerational = e.values[28];
var announcementlanguage = e.values[29];

 // var app = DocumentApp.getUi();

  var app = UiApp.getActiveApplication();

  Logger.log("test log in checkAnnouncement: " + announcement + " insight " + insightannouncement, "testing");
 // var announcement = e.values[14];
  //var insightannouncement = e.values[15]; 
  //var toAddress = e.values[20];
  //var htmlBody = "Thank you for your <b>Program Package</b> report submitted on <i>";
  //var subject = "test subject";
  Logger.log(announcement, "testing");
  Logger.log(insightannouncement, "testing");
 // Logger.log(toAddress, "testing");

  announcement = announcement.replace(/(^\s*)|(\s*$)/gi,"");
  announcement = announcement.replace(/[ ]{2,}/gi," ");
  announcement = announcement.replace(/\n /,"\n");

  insightannouncement = insightannouncement.replace(/(^\s*)|(\s*$)/gi,"");
  insightannouncement = insightannouncement.replace(/[ ]{2,}/gi," ");
  insightannouncement = insightannouncement.replace(/\n /,"\n");

  if(announcement.split(' ').length > 3)
  {
     app.getElementById('entry_15').setText("*Announcement should be less than 30 words").setStyleAttribute("color", "#F00");
     e.preventDefault(); 
  }
  else if(insightannouncement.split(' ').length > 5)
  {
    app.getElementById('entry_32').setText("*insightAnnouncement should be less than 50 words").setStyleAttribute("color", "#F00");
   e.preventDefault(); 
  }

}

2 个答案:

答案 0 :(得分:2)

Google表单的数据验证仅限于提交后,例如调用onFormSubmission触发器时。在那个时间点,没有能力与表单UI进行交互。

您的触发器功能接收的事件是FormResponse对象,该对象不包含preventDefault()方法。

参考

答案 1 :(得分:0)

假设上述答案是正确的,并且此事件在提交后触发,它们在技术上是正确的,您无法停止提交,但您可以从表单的集合中删除响应(如果我,则应删除它)我正确理解了API的一部分。