如何根据答案总数而不是答案数限制Google表单提交

时间:2018-02-20 13:32:59

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

我创建了一个简单的注册表单,只要求:

  1. 电子邮件地址(必填)
  2. 姓名(必填)
  3. 加一个人的姓名(不需要 - 如果有的话)
  4. 我可以根据回复数限制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, the event is full.";
    
        // do the math
        if(responses.length >= 100) {
            form.setAcceptingResponses(false).setCustomClosedFormMessage(msg);
        }
    }
    

    但我无法弄清楚如何将限制设定为1和2的总和(即剧院中的座位总数)

    有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

在表单上使用onFormSubmit Event ,每次提交表单时都会运行脚本。然后,您可以在plusOne列上循环,检查空值,以获得与会者总数。

function onEdit(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Form Responses"); //change to match your sheet.
  var currentGuests = sheet.getRange("GuestCountCell with A1Notation").getValue(); //get current count from cell on spreadsheet defined above.

  //Do other stuff
}

答案 1 :(得分:1)

试试这段代码。您需要在form submit trigger

上运行此功能
function calculateTodayReservedSeats() {
    var ss = SpreadsheetApp.openById(SPREADSHEET_ID);
    var sheet = ss.getSheetByName(SHEET_NAME);
    var lastRow = sheet.getLastRow();
    var person1 = sheet.getRange(2, PERSON1_COLUMN_INDEX, lastRow - 1, 1).getValues();
    var person2 = sheet.getRange(2, PERSON2_COLUMN_INDEX, lastRow - 1, 1).getValues();
    var i, count;
    for (i = 0; i < person1.length; i++) {
        count += person1[i][0];
        count += person2[i][0];
    }

    if (count > LIMIT) {
        //Do your stuff
        //You can disable your form
    }
}

您需要定义以下常量:

  • SPREADSHEET_ID
  • SHEET_NAME
  • PERSON1_COLUMN_INDEX
  • PERSON2_COLUMN_INDEX
  • LIMIT

这是未经测试的代码,让我知道如果这不起作用,我们可以调试。如果它有效,那么你可以接受解决方案/ upvote。

答案 2 :(得分:1)

感谢@Chris W@Umair的不安帮助,我终于能够创建一个能够满足我最初想要的形式。

以下脚本放在表单的脚本编辑器中,并在表单提交时触发:

public class Hook : MonoBehaviour {

    // Stores a Cord and its "other" Hook
    Dictionary<Hook, Cord> attached = new Dictionary<Hook, Cord>();

    // Add a Cord by retrieving its "other" Hook and using it as a key
    public void Attach(Cord c) {
        if (!ConflictTest(c)){
            attached.Add(GetOtherHook(c), c);
        }
    }

    // Remove a cord based on its "other" Hook
    public void Detach(Cord c) {
        attached.Remove(GetOtherHook(c));
    }

    // Determine whether current connections contain this Cord's "other" Hook
    bool ConflictTest(Cord toAttach) {
        return attached.ContainsKey(GetOtherHook(toAttach));
    }

    // Retrieves the "other" (non-current Hook) a cord is attached to
    private Hook GetOtherHook(Cord c) {
        return c.end != this ? c.end : c.start;
    }
}

只有附加的表单和电子表格设置如下: 从原始(响应)表填充第二张表(总计),并添加额外的(D)列。 D2-D20具有countif公式(function FormDisabling() { var form = FormApp.getActiveForm(); var msg = 'Sorry, the event is full.'; var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Total'); var range = sheet.getRange('d21’); var data = range.getValue(); if (data <= 40) { form.setAcceptingResponses(false).setCustomClosedFormMessage(msg); } } ),而单元格D21具有这些值。如果其值大于脚本中设置的数字,则表单将禁用,并显示自定义消息。

我确信原始问题的方法不那么简单。我很抱歉这些愚蠢的问题和评论,这是我第一次涉足Apps脚本(以及一段时间的逻辑思考)。