データの入力規則にアクセスします。新しいルールを作成するには、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