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