脚本onForm Submit触发器无法正常工作

时间:2019-07-08 19:49:04

标签: google-apps-script triggers google-form

我创建了一个Google表单,该表单将接受最新的回复并将所选的选项移至其他部分。因此,如果有人签出笔记本电脑,则一旦他们提交了表格,笔记本电脑的选择就会出现在签入部分。当我手动运行脚本时,它可以很好地工作,但是一旦添加触发器,它就可以在头几次工作,然后开始为一个提交创建多个触发器,然后在表单上创建多个都具有相同含义的新复选框。例如,当只有一个笔记本电脑时,我将有三种不同的笔记本电脑选择。因此,我不得不离开触发器,我查看了有关此问题的其他类似问题,但它们都涉及电子表格,但是我的工作完全是使用Google表单,因此我不确定这些解决方案是否对我有用。

我没有添加我的所有代码,因为其中一部分是同一件事,只是使用不同的变量将选择从签入移至签出。

var form = FormApp.openById('1I5uMesHbeVZ_RSP8wxmmpPA7-Sgcc4b6dzzH305c8K8');

/**
Responds to a form submission event when the on formSubmit trigger is 
enabled
*
* @param {Event} e The event parameter created by a form submission
*/

//function that gets checkout responses
function myFunction(e) {
  //getting form responses
  var formResponses = form.getResponses();

  //getting latest response
  var latestFR = formResponses[form.getResponses().length-1];

  //getting the item/question responses, checkout check in
  var itemResponses = latestFR.getItemResponses();

 //looping through item responses to see which item has a response
  for (var i=0; i<itemResponses.length; i++) {
    switch (itemResponses[i].getItem().getTitle()) {
    //if only response to checkout
      case "Checkout":
        var outAnswer = itemResponses[i].getResponse();
        outAnswer.forEach(addOut);
        outAnswer.forEach(deleteOut);
        break;
     //if only response to check in
      case "Check In":
        var inAnswer = itemResponses[i].getResponse();
        inAnswer.forEach(addToCheckOut);
        inAnswer.forEach(deleteIn);
        break;
     //if response to both check out/in
      case "Checkout" && "Check In":
        var outAnswer = itemResponses[i].getResponse();
        var inAnswer = itemResponses[i].getResponse();
        outAnswer.forEach(addOut);
        outAnswer.forEach(deleteOut);
        inAnswer.forEach(addToCheckOut);
        inAnswer.forEach(deleteIn);
        break;
    }}
  //getting email response to send email
  var email = itemResponses[0].getResponse();

  //testing to see if it gets the latest submission
  //delete my email later
  var subject = 'Response';
  var emailTo = [email];
  var body = 'Response is' + outAnswer + inAnswer;
  MailApp.sendEmail(emailTo, subject, body, {
    htmlBody: body});
}

//function that adds the latest response from checkout to check in 
section
function addOut(outAnswer) {
  //getting check in section item with its choices
  var a = form.getItems(FormApp.ItemType.CHECKBOX)[1].asCheckboxItem();
  //getting choices from check in
  var choices = a.getChoices();
  //creating new choice for check in
  var choice = a.createChoice(outAnswer);
  //adding the choice to the choices
  choices.push(choice);
  //setting the choices with new choice for check in 
  a.setChoices(choices);
}     

//function that deletes answer from checkout
//only works when its a string so convert outAnswer to string value with 
toString but only works with a single choice
function deleteOut(outAnswer) {
   var del = form.getItems(FormApp.ItemType.CHECKBOX) 
 [0].asCheckboxItem();
   del.setChoices(del.getChoices().filter(function (choice) {
     return choice.getValue() !== outAnswer.toString(); }));
  }

1 个答案:

答案 0 :(得分:0)

您将需要执行与电子表格答案建议相同的操作,创建脚本锁并将其用于转储快速连续的触发器。

只需在脚本顶部添加以下几行:

  var lock = LockService.getScriptLock();
  try {
    lock.waitLock(3000); 
     } catch (e) {Logger.log('Could not obtain lock after 3 seconds.');}
Utilities.sleep(3000);

您还可以添加“ lock.releaseLock();”到脚本末尾,但这不是必需的,可以自行锁定释放。

所有此代码的作用是在触发后三秒钟内拒绝任何新提交。如果这还不够,那么将等待锁中的时间和睡眠时间更改为5000(表单通常需要不到三秒钟的时间来运行这样的脚本,因此您迫使脚本花费更长的时间)。