Google 形成正则表达式 "Doesn't Match" 在特定范围之间不起作用
Google Forms Regex "Doesn't Match" Between Specific Range Doesn't Work
我需要有关在 Google 表单的简答题中使用 Regex 作为数据验证的帮助。根据我的理解,如果我使用这个表达式:
"Doesn't Match"
+ [a-zA-Z]{1,}|[0-9]{1,15}|[0-9]{17,140}
我应该还能填满16位数字的答案。但是,我根本无法输入任何数字。有人对此有任何解决方案吗?
作为旁注,我不能使用“匹配”或“包含”,因为我必须 link 它到 Google 电子表格以进行“唯一值”数据验证以及 Google 表单不支持多重数据验证。这是我当前的脚本:
//Still needs much better solution, but this will do for now
function checkNIK(){
//Get current form
var form = FormApp.getActiveForm();
//Open spreadsheet containing NIK
var ss = SpreadsheetApp.openById(<id>);
//Get sheet
var responses = ss.getSheetByName('Form responses 1');
//Get list of all NIK in column F, which contains a few hundreds 16-digits numbers.
var disallowedArray = responses.getRange('F:F').getValues();
//Clean NIK list
disallowedArray = disallowedArray.filter(item => item != ''); //Empty cells
disallowedArray = disallowedArray.filter(item => item != 'NIK'); //Cell named "NIK"
//Transform NIK list array into a single string
var disallowedString = disallowedArray.join("|");
//Append additional expressions
//Doesn't work, adding "|[0-9]{17,140}" makes the form unable to accept 16 digits number anymore
disallowedString = disallowedString.concat('|[a-zA-Z\s]{1,}|[0-9]{1,15}|[0-9]{17,140}');
//Print regex just to make sure
//console.log(disallowedString);
//Select the question you want to update
var item = form.getItemById(<id>).asTextItem();
//Create validation rule
var validation = FormApp.createTextValidation()
.setHelpText('Wrong or duplicate NIK.')
.requireTextDoesNotMatchPattern(disallowedString)
.build();
item.setValidation(validation);
}
如果我做错了什么,无论是主题还是题外,请告诉我。
虽然我无法提供您遇到此类错误的原因。我找到了一个解决方法,让您能够输入 16 位数字。
我只是改变了表达式的顺序然后测试它是否有效:
Regex: [0-9]{17,140}|[0-9]{1,15}
输入:15位数字
输入:16位
输入:17位
您需要将正则表达式括在方括号中。
([a-zA-Z]{1,}|[0-9]{1,15}|[0-9]{17,140})
我需要有关在 Google 表单的简答题中使用 Regex 作为数据验证的帮助。根据我的理解,如果我使用这个表达式:
"Doesn't Match"
+ [a-zA-Z]{1,}|[0-9]{1,15}|[0-9]{17,140}
我应该还能填满16位数字的答案。但是,我根本无法输入任何数字。有人对此有任何解决方案吗?
作为旁注,我不能使用“匹配”或“包含”,因为我必须 link 它到 Google 电子表格以进行“唯一值”数据验证以及 Google 表单不支持多重数据验证。这是我当前的脚本:
//Still needs much better solution, but this will do for now
function checkNIK(){
//Get current form
var form = FormApp.getActiveForm();
//Open spreadsheet containing NIK
var ss = SpreadsheetApp.openById(<id>);
//Get sheet
var responses = ss.getSheetByName('Form responses 1');
//Get list of all NIK in column F, which contains a few hundreds 16-digits numbers.
var disallowedArray = responses.getRange('F:F').getValues();
//Clean NIK list
disallowedArray = disallowedArray.filter(item => item != ''); //Empty cells
disallowedArray = disallowedArray.filter(item => item != 'NIK'); //Cell named "NIK"
//Transform NIK list array into a single string
var disallowedString = disallowedArray.join("|");
//Append additional expressions
//Doesn't work, adding "|[0-9]{17,140}" makes the form unable to accept 16 digits number anymore
disallowedString = disallowedString.concat('|[a-zA-Z\s]{1,}|[0-9]{1,15}|[0-9]{17,140}');
//Print regex just to make sure
//console.log(disallowedString);
//Select the question you want to update
var item = form.getItemById(<id>).asTextItem();
//Create validation rule
var validation = FormApp.createTextValidation()
.setHelpText('Wrong or duplicate NIK.')
.requireTextDoesNotMatchPattern(disallowedString)
.build();
item.setValidation(validation);
}
如果我做错了什么,无论是主题还是题外,请告诉我。
虽然我无法提供您遇到此类错误的原因。我找到了一个解决方法,让您能够输入 16 位数字。
我只是改变了表达式的顺序然后测试它是否有效:
Regex: [0-9]{17,140}|[0-9]{1,15}
输入:15位数字
输入:16位
输入:17位
您需要将正则表达式括在方括号中。
([a-zA-Z]{1,}|[0-9]{1,15}|[0-9]{17,140})