Class DataValidation

DataValidation

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

// Log information about the data validation rule for cell A1.
var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = cell.getDataValidation();
if (rule != null) {
  var criteria = rule.getCriteriaType();
  var 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.
var oldDates = [new Date('1/1/2013'), new Date('12/31/2013')];
var newDates = [new Date('1/1/2014'), new Date('12/31/2014')];
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var rules = range.getDataValidations();

for (var i = 0; i < rules.length; i++) {
  for (var j = 0; j < rules[i].length; j++) {
    var rule = rules[i][j];

    if (rule != null) {
      var criteria = rule.getCriteriaType();
      var 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.
var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = cell.getDataValidation();
if (rule != null) {
  var criteria = rule.getCriteriaType();
  var 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.
var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = cell.getDataValidation();
if (rule != null) {
  var criteria = rule.getCriteriaType();
  var 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