簡體   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

我正在處理一個查詢谷歌表單,它收集參與者對某個問題的投票。 我想根據 ID 號限制參與者。 我在考慮三種方法:

  1. 如果輸入的 ID 不在給定列表中,則阻止提交表單。 (我更喜歡這種方法,但到目前為止找不到任何有用的代碼)

  2. 使用Google 表單上的 GAS通過 onFormSubmit 觸發器提交表單后,刪除鏈接響應電子表格中的行。 這是我的代碼不起作用:

     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。 由於條件2 是兩個 ID 號的串聯,因此參與者必須在 Google 表單的最后一個文本項中輸入他/她的合並 ID(密碼),這是不正確的。 (例如“123aa”)

我怎樣才能解決這個問題?

如果輸入的 ID 不在給定列表中,則阻止提交表單

  • 最簡單的方法是合並文本驗證,您甚至不需要為它編寫代碼
  • 只是在構建/編輯ID1問題Regular expression時選擇, matches並指定應允許使用|提交表單的所有 ID 作為分隔符

在此處輸入圖像描述

更多信息

  • 如果您有動力以編程方式合並文本驗證,請查看此處此處
  • 如果您更喜歡使用現有代碼來刪除行 - 將腳本附加到表單或電子表格都沒有關系,在這兩種情況下,您都可以並且應該使用觸發器onFormSubmit (而不是onChange !)
  • 從表單提交表中刪除行將不起作用 - 它們會在下一次表單提交時返回
  • 將具有正確 ID 的onFormSubmit行復制到輔助工作表可以工作,但它比使用文本驗證更復雜

如果您希望將所有 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM