狐的傳說
2022-07-08 18:17:49
我正在處理一個查詢谷歌表單,它收集參與者對某個問題的投票。我想根據 ID 號限制參與者。我在考慮三種方法:如果輸入的 ID 不在給定列表中,則阻止提交表單。(我更喜歡這種方法,但到目前為止找不到任何有用的代碼)使用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'); }}使用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 編碼的新手,所以任何幫助都將不勝感激。
2 回答

Helenr
TA貢獻1780條經驗 獲得超4個贊
如果輸入的 ID 不在給定列表中,則阻止提交表單
最簡單的方法是合并文本驗證,您甚至不需要為它編寫代碼
只是在構建/編輯
ID1
問題時選擇Regular expression
,matches
并指定所有允許提交表單的 ID|
用作分隔符
更多信息

飲歌長嘯
TA貢獻1951條經驗 獲得超3個贊
如果您希望將所有 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
}
添加回答
舉報
0/150
提交
取消