存取資料驗證規則。如要建立新規則,請使用 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() | Data | 根據這項規則的設定,為資料驗證規則建立建構工具。 |
get | Boolean | 如果規則在輸入資料未通過資料驗證時顯示警告,則傳回 true;如果規則完全拒絕輸入資料,則傳回 false。 |
get | Data | 取得 Data 列舉中定義的規則條件類型。 |
get | Object[] | 取得規則條件的引數陣列。 |
get | 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 (如果未設定說明文字)