如果 REGEX 表达式的条件与使用 Google 应用程序脚本输入的单元格不匹配,您如何拒绝输入

How do you reject input if criteria of REGEX expression is not a match to a cells input using Google apps script

所以我使用 Google 应用程序脚本进行了简单的数据验证。唯一的问题是用户仍然可以输入他们喜欢的任何内容(他们只是在单元格的右上角看到红色的无效输入标记,以告知他们输入不正确)。如果输入无效,我如何才能使输入实际上被单元格拒绝?

如有任何帮助,我们将不胜感激!谢谢!

您需要使用.setAllowInvalid(false)

function onOpen(e) {
  var range = SpreadsheetApp.getActive().getRange('B2:B');
  var rule = SpreadsheetApp.newDataValidation()
    .requireFormulaSatisfied('=REGEXMATCH(B2,"([#0-9a-fA-F]){7}|green|red|blue|purple|yellow|orange|brown|grey|gray|pink|black|white|lime|cyan")')
    .setAllowInvalid(false) // Set to false to reject input
    .build();
  range.setDataValidation(rule);
}