You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:32:48