简体   繁体   English

如何在 Google Form 或 Google Sheets 上使用 GAS 根据两个 ID 限制 google forms 提交

[英]How to restrict google forms submission based on two ID using GAS on Google Form or Google Sheets

I'm working on a query Google Form which collects participants' votes to a certain question.我正在处理一个查询谷歌表单,它收集参与者对某个问题的投票。 I want to restrict participants based on the ID number.我想根据 ID 号限制参与者。 I was thinking of three approaches:我在考虑三种方法:

  1. Prevent form submission if the entered ID is not on the given list.如果输入的 ID 不在给定列表中,则阻止提交表单。 (I prefer this approach but couldn't find any useful code for it so far) (我更喜欢这种方法,但到目前为止找不到任何有用的代码)

  2. Delete rows in the linked responses spreadsheet after form submission using GAS on Google Form through onFormSubmit trigger.使用Google 表单上的 GAS通过 onFormSubmit 触发器提交表单后,删除链接响应电子表格中的行。 Here is my code which is not working:这是我的代码不起作用:

     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. Delete rows in the linked responses spreadsheet after form submission using GAS on Google Sheets through onChange trigger.使用Google 表格上的 GAS通过 onChange 触发器提交表单后,删除链接响应电子表格中的行。 Here is my best effort:这是我的最大努力:

     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); } };

I'm totally new in Javascript coding so any help would be appreciated.我是 Javascript 编码的新手,因此将不胜感激。

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

Thanks to the ziganotschka answer I update my code to this:感谢 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")]);

}

The recent issue is on ID2validation.最近的问题是关于 ID2validation。 Since condition2 is the concatenation of two ID numbers, the participant has to enter his/her merged ID's (passwords) in the last text item in the Google Form which is not correct.由于条件2 是两个 ID 号的串联,因此参与者必须在 Google 表单的最后一个文本项中输入他/她的合并 ID(密码),这是不正确的。 (Ex. '123aa') (例如“123aa”)

How can I fix this?我怎样才能解决这个问题?

Prevent form submission if the entered ID is not on the given list如果输入的 ID 不在给定列表中,则阻止提交表单

  • The easiest way would be to incorporate text validation, you do not even need to code for it最简单的方法是合并文本验证,您甚至不需要为它编写代码
  • Just chose when building / editing the ID1 question Regular expression , matches and specify all IDs that shall be allowed to submit the form using |只是在构建/编辑ID1问题Regular expression时选择, matches并指定应允许使用|提交表单的所有 ID as separator作为分隔符

在此处输入图像描述

Further information更多信息

  • If you feel motivated to incorporated the text validation programmatically, have a look here and here如果您有动力以编程方式合并文本验证,请查看此处此处
  • If you prefer to work on your already existing code to delete rows - it does not matter either you attach the script to the form or the spreadsheet, in both cases you can and should use the trigger onFormSubmit (not onChange !)如果您更喜欢使用现有代码来删除行 - 将脚本附加到表单或电子表格都没有关系,在这两种情况下,您都可以并且应该使用触发器onFormSubmit (而不是onChange !)
  • Deleting rows from the form submission sheet will not work - they come back at the next form submission从表单提交表中删除行将不起作用 - 它们会在下一次表单提交时返回
  • Copying onFormSubmit the rows with the right ID to a secondary sheet can work, but it more complicated than using text validation将具有正确 ID 的onFormSubmit行复制到辅助工作表可以工作,但它比使用文本验证更复杂

If you would prefer keeping all of the ids on a spreadsheet try this in the beginning of your code.如果您希望将所有 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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有Google表单的情况下向Google表格提交HTML表单 - How to Submit an HTML Form to Google Sheets…without Google Forms Google 表格 - 重新格式化提交表单的日期 - Google Sheets - Reformat date on form submission 如何使用Google Analytics(分析)跟踪表单提交 - How to track a form submission using Google Analytics 如何根据谷歌表格中的范围自动更新谷歌表单中的下拉列表? - How to automatically update dropdown in a Google Form based on range in Google Sheets? Google表单提交页面 - Google Forms Submission Page 如何根据提交的表单在Google Analytics(分析)中跟踪转化? - How do I track a conversion in Google Analytics Based on Form Submission? 如何使用 GAS 中的 p5.js 库引用特定的 Google 表格单元格 - How would I reference a specific Google Sheets cell using the p5.js library in GAS 从一个Google表单提交中创建两个Google日历活动 - Create two Google Calendar events from one Google Form submission 在Excel / Google表格中提交表单时复制volatile函数的单元格值 - Copy cell value of volatile function on form submission in Excel/Google Sheets HTML 使用 Apps 脚本登陆“谢谢”页面向 Google 表格提交表单 - HTML Form Submission to Google Sheets using Apps Scripts Landing "Thank You" Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM