如何在Google Apps Script中禁用AllowInvalid?代码设置位置咨询
解决Google Apps Script数据验证拒绝无效输入的问题
嘿,这事儿很容易搞定!首先得纠正个小细节:你要调用的是setAllowInvalid(false)而不是getAllowInvalid(false)——get是用来读取当前设置的,咱们要设置规则得用set方法才行。
正确的位置是在构建验证规则的链式调用里,放在.requireValueInList(DropDownList)之后、.build()之前,这样就能把拒绝无效输入的配置加到验证规则里了。
修改后的完整代码如下:
let validationRule = SpreadsheetApp.newDataValidation() .requireValueInList(DropDownList) .setAllowInvalid(false) // 就是这里!添加这一行 .build(); sheet.getRange(3,3,1,1).setDataValidation(validationRule);
这样设置之后,当有人输入不在DropDownList里的内容时,单元格会直接拒绝该输入,完全符合你的需求~
内容的提问来源于stack exchange,提问作者Shivam Sahil
相关产品推荐
相关产品推荐

