Class DataValidation

数据验证

访问数据验证规则。如需创建新规则,请使用 SpreadsheetApp.newDataValidation()DataValidationBuilder。您可以使用 Range.setDataValidation(rule) 为范围设置验证规则。

// Log information about the data validation rule for cell A1. const cell = SpreadsheetApp.getActive().getRange('A1'); const rule = cell.getDataValidation(); if (rule != null) {   const criteria = rule.getCriteriaType();   const args = rule.getCriteriaValues();   Logger.log('The data validation rule is %s %s', criteria, args); } else {   Logger.log('The cell does not have a data validation rule.'); }

方法

方法返回类型简介
copy()DataValidationBuilder根据此规则的设置为数据验证规则创建构建器。
getAllowInvalid()Boolean如果规则在输入数据验证失败时显示警告,则返回 true;如果完全拒绝输入,则返回 false
getCriteriaType()DataValidationCriteria获取规则的条件类型(如 DataValidationCriteria 枚举中所定义)。
getCriteriaValues()Object[]获取规则条件的参数数组。
getHelpText()String获取规则的帮助文本,如果未设置帮助文本,则返回 null

详细文档

copy()

根据此规则的设置为数据验证规则创建构建器。

// Change existing data validation rules that require a date in 2013 to require // a date in 2014. const oldDates = [new Date('1/1/2013'), new Date('12/31/2013')]; const newDates = [new Date('1/1/2014'), new Date('12/31/2014')]; const sheet = SpreadsheetApp.getActiveSheet(); const range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); const rules = range.getDataValidations();  for (let i = 0; i < rules.length; i++) {   for (let j = 0; j < rules[i].length; j++) {     const rule = rules[i][j];      if (rule != null) {       const criteria = rule.getCriteriaType();       const args = rule.getCriteriaValues();        if (criteria === SpreadsheetApp.DataValidationCriteria.DATE_BETWEEN &&           args[0].getTime() === oldDates[0].getTime() &&           args[1].getTime() === oldDates[1].getTime()) {         // Create a builder from the existing rule, then change the dates.         rules[i][j] = rule.copy().withCriteria(criteria, newDates).build();       }     }   } } range.setDataValidations(rules);

返回

DataValidationBuilder - 基于此规则的设置的构建器


getAllowInvalid()

如果规则在输入数据验证失败时显示警告,则返回 true;如果完全拒绝输入,则返回 false。新数据验证规则的默认值为 true

返回

Boolean - 如果规则允许输入数据验证失败的数据,则为 true;否则为 false


getCriteriaType()

获取规则的条件类型(如 DataValidationCriteria 枚举中所定义)。如需获取条件的参数,请使用 getCriteriaValues()。如需使用这些值创建或修改数据验证规则,请参阅 DataValidationBuilder.withCriteria(criteria, args)

// Log information about the data validation rule for cell A1. const cell = SpreadsheetApp.getActive().getRange('A1'); const rule = cell.getDataValidation(); if (rule != null) {   const criteria = rule.getCriteriaType();   const args = rule.getCriteriaValues();   Logger.log('The data validation rule is %s %s', criteria, args); } else {   Logger.log('The cell does not have a data validation rule.'); }

返回

DataValidationCriteria - 数据验证条件的类型


getCriteriaValues()

获取规则条件的参数数组。如需获取条件类型,请使用 getCriteriaType()。如需使用这些值创建或修改数据验证规则,请参阅 DataValidationBuilder.withCriteria(criteria, args)

// Log information about the data validation rule for cell A1. const cell = SpreadsheetApp.getActive().getRange('A1'); const rule = cell.getDataValidation(); if (rule != null) {   const criteria = rule.getCriteriaType();   const args = rule.getCriteriaValues();   Logger.log('The data validation rule is %s %s', criteria, args); } else {   Logger.log('The cell does not have a data validation rule.'); }

返回

Object[] - 与规则的条件类型相适应的参数数组;参数数量及其类型与 DataValidationBuilder 类的相应 require...() 方法匹配


getHelpText()

获取规则的帮助文本,如果未设置帮助文本,则返回 null

返回

String - 规则的帮助文本,如果未设置帮助文本,则为 null