如何在达到Google表单提交限制时创建候补名单?

时间:2014-08-28 18:01:04

标签: google-apps-script registration

限制可以提交Google表单的人数的最佳方法是什么,然后在达到限制后为其他人提供加入等待名单的选项?

我可以使用以下脚本在达到定义的限制时关闭表单,但是希望在此时打开另一个表单以收集等候名单提交。

function closeForm() {
    // get active form
    var form = FormApp.getActiveForm();

    // retrieve number of responses thusfar
    var responses = form.getResponses();

    // set close message
    var msg = "We're sorry, but this session is full. Future sessions will be announced in the CISL Daily Bulletin. Thank you for your interest.";

    // do the math
    if(responses.length >= 1) {
        form.setAcceptingResponses(false).setCustomClosedFormMessage(msg);
    }
}

我已查看Google文档帮助论坛并在此处进行了搜索。到目前为止,我发现的很少是基于较旧版本的Google表单。

有关如何执行此操作的任何建议吗?

1 个答案:

答案 0 :(得分:0)

不确定这样做的最佳方法是什么,但我想到的解决方案按预期工作但 - 这是一个严重警告 - 有点曲折 ...

无论如何,如果您的表单不是太复杂,可能很容易设置。下面的示例使用仅包含文本字段的表单,因此代码非常简单。如果您使用不同的问题类型,则必须为您使用的每种类型添加条件,并因此调整addElement。

全球的想法是制作一个备用表单来保存所有现有字段,删除所有字段并为等待列表创建一个“给你的名字”字段。

然后restore函数可以毫无痛苦地重新构建表单。

表单公共URL将始终相同,表单可以无限期地重复使用...这是此脚本的主要(唯一)优势。

也许有人会建议一个更聪明和/或更简单的解决方案,我很乐意从中学习。

编辑:我添加了一个onFormSubmit触发器创建功能,并将逻辑更改为最适合正常工作流程:而响应数量为<限制,脚本返回没有任何影响。达到限制时,表单会自动修改。

function createTrigger(){
  var form = FormApp.getActiveForm();
  ScriptApp.newTrigger('checkFormForLimit').forForm(form).onFormSubmit().create();
}

function checkFormForLimit() {
  var form = FormApp.getActiveForm();
  var responses = form.getResponses();
  Logger.log(responses.length);
  if(responses.length<5) {return};// do nothing if not over the limit
  if(DriveApp.getFilesByName('backup'+form.getTitle()).hasNext()){ // check if bkp already exists
    var formBkp = FormApp.openById(DriveApp.getFilesByName('backup'+form.getTitle()).next().getId());// if it does, open it
  }else{
    var formBkp = FormApp.create('backup'+form.getTitle());// else create it
  }
  var msg = "We're sorry, but this session is full. Future sessions will be announced in the CISL Daily Bulletin. Thank you for your interest.";
  formBkp.setDescription(form.getDescription());
  form.setDescription(msg);
  var items = form.getItems();
  for(var n in items){
    var item = items[n];
    if(item.getType()==FormApp.ItemType.TEXT){
      formBkp.addTextItem().setTitle(item.getTitle());
      form.deleteItem(item);
    }
  }
  form.addTextItem().setTitle('Type your name to subscribe to the wait list')
}

function restoreForm(){
  var form = FormApp.getActiveForm();
  form.deleteItem(form.getItems()[0]);
  var formBkp = FormApp.openById(DriveApp.getFilesByName('backup'+form.getTitle()).next().getId());
  form.setDescription(formBkp.getDescription());
  var items = formBkp.getItems();
  for(var n in items){
    var item = items[n];
    if(item.getType()==FormApp.ItemType.TEXT){
      form.addTextItem().setTitle(item.getTitle());
      formBkp.deleteItem(item);
    }
  }
}
相关问题