【问题标题】:How to restrict google forms submission based on two ID using GAS on Google Form or Google Sheets如何在谷歌表单或谷歌表格上使用 GAS 限制基于两个 ID 的谷歌表单提交
【发布时间】:2020-04-20 14:09:44
【问题描述】:

我正在处理一个查询 Google 表单,该表单收集参与者对某个问题的投票。我想根据 ID 号限制参与者。我在考虑三种方法:

  1. 如果输入的 ID 不在给定列表中,则阻止提交表单。 (我更喜欢这种方法,但到目前为止找不到任何有用的代码)

  2. 在表单提交后,通过 onFormSubmit 触发器使用 GAS on Google Form 删除链接响应电子表格中的行。这是我的代码不起作用:

    function onFormSubmit(e) {
     // Grab the session data again so that we can match it to the user's choices.
    var response = [];
    var values = SpreadsheetApp.openById('1rT9tKAi6ZSvZzBaXNSPMJAt4RKnW- 
     9lqiE9zvZV').getDataRange().getValues();
    
    for (var i = 1; i < values.length; i++) {
      var indiv = values[I];
      var Fname = indiv[0];
      var Lname = indiv[1];
      var ID1 = indiv[2];
      var ID2 = indiv[3];
    
      // For every selection in the response, find the matching ID1 and title
      // in the spreadsheet and add the session data to the response array.
      if (e.namedValues[ID1] == ID1) {
        response.push(indiv);
      } else {
      Browser.msgBox('Your ID number does not matches the list');
      }
    }
    
  3. 使用 Google 表格上的 GAS 通过 onChange 触发器提交表单后删除链接响应电子表格中的行。这是我最大的努力:

    function onChange(e) {  
     var refvalues = 
     SpreadsheetApp.getActive().getSheetByName('members_sheet').getDataRange().getValues();
     var  sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
     var  values = sheet.getDataRange().getValues();
    
     var indiv = values[values.length];
     var ID1 = indiv[2];
     var flag = 0;
      for (var i = 1; i < refvalues.length; i++) {
          var refindiv = refvalues[i];
          var refID1 = refindiv[2];
        if (ID1 == refID1) {
          flag = 1;
        }
     } 
    
     if (flag == 0) {
       sheet.deleteRow(values.length); 
     }
     };
    

我是 Javascript 编码的新手,因此我们将不胜感激。

//--------------------------------------------- --------------------------------//

感谢 ziganotschka 的回答,我将代码更新为:

function makeMultiForm() {
  var form = FormApp.create('Nazar Sanji')
                .setConfirmationMessage('Thank you! Your Vote have been 
    recorded');
  form.setTitle("Query");

  var ss = SpreadsheetApp.openById('1rT9tKAi6ZSvZzBaXNSPMJAt4RKnW- 
   9lqiE9zvZV5JJk');
  var ID1List = 
    ss.getSheetByName('members_sheet').getRange('C2:C4').getValues();//Ex [123 ; 555]
  var ID2List = 
    ss.getSheetByName('members_sheet').getRange('D2:D4').getValues();//Ex [aa ; bb]

  // Ex passwords: asd, 123, asd123
  const condition1 = ID1List.map(element => `${element}`).join('|')

  var IDarray =[];
  //Add items to IDarray Ex [123aa ; 555bb]
    for(var i=0; i<ID1List.length; i++){
        IDarray[i] = [ID1List[i][0]+ID2List[i][0]];
    }
  const condition2 = IDarray.map(element => `${element}`).join('|')

  // Start by laying out the bare-bones structure.  This defines the different
  // sections, and the bare widgets in each section.
  // Note that you can't add any flow-routing details at this point, because
  // the destinations most likely haven't been defined yet

  var itemFName = form.addTextItem().setTitle('First Name').setRequired(true);
  var itemLName = form.addTextItem().setTitle('Last Name').setRequired(true);
  var itemID1   = form.addTextItem().setTitle('First ID').setRequired(true);

  // Create valid ation for this question matching the ID1(ID Melli) that we got from the sheet
  var ID1Validation = FormApp.createTextValidation()
.setHelpText('Enter a Valid First ID')
.requireTextMatchesPattern(condition1)
.build();
  itemID1.setValidation(ID1Validation);

  //var sectID2 = form.addPageBreakItem().setTitle("Second ID");
  var itemID2 = form.addTextItem().setTitle('Second ID').setRequired(true);

  // Create valid ation for this question matching the ID2(ID Shenasnameh) that we got from the sheet
  var ID2Validation = FormApp.createTextValidation()
.setHelpText('Second ID does not match the First ID')
.requireTextMatchesPattern(condition2)
.build();
  itemID2.setValidation(ID2Validation);


  var sectVote = form.addPageBreakItem().setTitle("Final Vote");
  var VoteOptions = form.addMultipleChoiceItem().setTitle("Which Competition");
  VoteOptions.setChoices([
  VoteOptions.createChoice("Option 1"),
  VoteOptions.createChoice("Option 2")]);

}

最近的问题是关于 ID2validation。由于 condition2 是两个 ID 号的串联,因此参与者必须在 Google 表单的最后一个文本项中输入他/她的合并 ID(密码),这是不正确的。 (例如“123aa”)

我该如何解决这个问题?

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-forms


    【解决方案1】:

    如果输入的 ID 不在给定列表中,则阻止表单提交

    • 最简单的方法是结合文本验证,您甚至不需要编写代码
    • 在构建/编辑ID1问题时选择Regular expressionmatches并指定所有允许提交表单的ID,使用|作为分隔符

    更多信息

    • 如果您有动力以编程方式合并文本验证,请查看 herehere
    • 如果您更喜欢使用现有代码来删除行 - 将脚本附加到表单或将 电子表格,在这两种情况下,您都可以并且应该使用触发器 onFormSubmit(不是onChange!)
    • 从表单提交表中删除行将不起作用 - 它们会在下一次表单提交时返回
    • 将具有正确 ID 的行复制onFormSubmit 到辅助工作表可以工作,但它比使用文本验证更复杂

    【讨论】:

    • 亲爱的 ziganotschka, 感谢您分享解决方案。您的第二个链接上的问题答案与我的问题几乎相同。就我而言,参与者应提供两个密码(ID 号)以验证为合格个人以继续填写表格。最近的问题是将第二个 ID 号与数据验证字段中的第一个连接起来。请参考我对问题的回答。
    • 如果您希望在提交表单之前进行验证,很遗憾,无法动态检查 ID2 是否对应于所选 ID1。您需要要求用户在同一字段中输入他的 ID1 和 ID2 的组合。然后,您可以将其与电子表格中的串联值进行比较。小提示:下次,根据 Stackoverflow 社区准则,请修改您的旧问题,而不是提供更新作为答案(如果与原始问题有足够的不同,请发布新问题)。
    • 感谢您提醒指南和您的解决方案。实际上这是我第一次在 Stackoverflow 上发帖。
    【解决方案2】:

    如果您希望将所有 ID 保留在电子表格中,请在代码开头尝试此操作。

    function onFormSubmit(e) {
      const ss=SpreadsheetApp.openById('your id');
      const idsh=ss.getSheetByName('id sheet');
      const idrg=ss.getRange(2,1,idsh.getLastRow()-1,1);
      const idA=idrg.getValues().map(function(r){return r[0];});
      if (idA.indexOf(e.namedValues['ID1'])==-1) {
        Browser.msgBox('Your ID number does not match the list');
        return;
      }
      //rest of your code here
    
    }
    

    【讨论】:

    • 亲爱的库珀,感谢您的推荐。尝试后出现此错误:“参数(字符串、数字、数字、数字)与 SpreadsheetApp.Spreadsheet.getRange 的方法签名不匹配”
    • 在哪一行?我猜你的代码中还有其他 onEdits,因为我在我的代码中没有看到与该问题相关的一行。
    • 这是唯一的可能性ss.getRange(2,1,idsh.getLastRow()-1,1);,而且看起来还可以。
    • 得到这个错误的原因是const idrg=ss.getRange(2,1,idsh.getLastRow()-1,1); - 修改为const idrg=idsh.getRange(2,1,idsh.getLastRow()-1,1);
    • 错误消失了,但我没有在错误的 ID 上收到 msgbox,并且投票也在谷歌表格中提交。
    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多