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()
تُنشئ أداة لإنشاء قاعدة للتحقّق من صحة البيانات استنادًا إلى إعدادات هذه القاعدة.
// 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[]
: صفيف من الوسيطات المناسبة لنوع معايير القاعدة، ويتطابق عدد الوسيطات
ونوعها مع طريقة require...()
المقابلة لفئة DataValidationBuilder
getHelpText()
تحصل على نص مساعدة القاعدة، أو null
في حال عدم ضبط نص مساعدة.
الإرجاع
String
: نص مساعدة القاعدة، أو null
في حال عدم ضبط نص مساعدة
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003e\u003ccode\u003eDataValidation\u003c/code\u003e objects give you access to data validation rules applied to spreadsheet ranges.\u003c/p\u003e\n"],["\u003cp\u003eYou can use the \u003ccode\u003eSpreadsheetApp.newDataValidation()\u003c/code\u003e and \u003ccode\u003eDataValidationBuilder\u003c/code\u003e classes to programmatically create new validation rules in your sheet.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDataValidation\u003c/code\u003e provides methods such as \u003ccode\u003egetCriteriaType()\u003c/code\u003e, \u003ccode\u003egetCriteriaValues()\u003c/code\u003e and \u003ccode\u003egetHelpText()\u003c/code\u003e to examine details of an existing validation rule.\u003c/p\u003e\n"],["\u003cp\u003eUsing \u003ccode\u003ecopy()\u003c/code\u003e and \u003ccode\u003ewithCriteria()\u003c/code\u003e on an existing \u003ccode\u003eDataValidation\u003c/code\u003e object helps modify current validation rules without recreating them from scratch.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetAllowInvalid()\u003c/code\u003e allows you to determine if a rule will show warnings or reject invalid input outright.\u003c/p\u003e\n"]]],[],null,["# Class DataValidation\n\nDataValidation\n\nAccess data validation rules. To create a new rule, use [SpreadsheetApp.newDataValidation()](/apps-script/reference/spreadsheet/spreadsheet-app#newDataValidation()) and [DataValidationBuilder](/apps-script/reference/spreadsheet/data-validation-builder). You can use\n[Range.setDataValidation(rule)](/apps-script/reference/spreadsheet/range#setDataValidation(DataValidation)) to set the validation rule for a range.\n\n```javascript\n// Log information about the data validation rule for cell A1.\nconst cell = SpreadsheetApp.getActive().getRange('A1');\nconst rule = cell.getDataValidation();\nif (rule != null) {\n const criteria = rule.getCriteriaType();\n const args = rule.getCriteriaValues();\n Logger.log('The data validation rule is %s %s', criteria, args);\n} else {\n Logger.log('The cell does not have a data validation rule.');\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|\n| [copy()](#copy()) | [DataValidationBuilder](/apps-script/reference/spreadsheet/data-validation-builder) | Creates a builder for a data validation rule based on this rule's settings. |\n| [getAllowInvalid()](#getAllowInvalid()) | `Boolean` | Returns `true` if the rule shows a warning when input fails data validation, or `false` if it rejects the input entirely. |\n| [getCriteriaType()](#getCriteriaType()) | [DataValidationCriteria](/apps-script/reference/spreadsheet/data-validation-criteria) | Gets the rule's criteria type as defined in the [DataValidationCriteria](/apps-script/reference/spreadsheet/data-validation-criteria) enum. |\n| [getCriteriaValues()](#getCriteriaValues()) | `Object[]` | Gets an array of arguments for the rule's criteria. |\n| [getHelpText()](#getHelpText()) | `String` | Gets the rule's help text, or `null` if no help text is set. |\n\nDetailed documentation\n----------------------\n\n### `copy()`\n\nCreates a builder for a data validation rule based on this rule's settings.\n\n```javascript\n// Change existing data validation rules that require a date in 2013 to require\n// a date in 2014.\nconst oldDates = [new Date('1/1/2013'), new Date('12/31/2013')];\nconst newDates = [new Date('1/1/2014'), new Date('12/31/2014')];\nconst sheet = SpreadsheetApp.getActiveSheet();\nconst range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());\nconst rules = range.getDataValidations();\n\nfor (let i = 0; i \u003c rules.length; i++) {\n for (let j = 0; j \u003c rules[i].length; j++) {\n const rule = rules[i][j];\n\n if (rule != null) {\n const criteria = rule.getCriteriaType();\n const args = rule.getCriteriaValues();\n\n if (criteria === SpreadsheetApp.DataValidationCriteria.DATE_BETWEEN &&\n args[0].getTime() === oldDates[0].getTime() &&\n args[1].getTime() === oldDates[1].getTime()) {\n // Create a builder from the existing rule, then change the dates.\n rules[i][j] = rule.copy().withCriteria(criteria, newDates).build();\n }\n }\n }\n}\nrange.setDataValidations(rules);\n```\n\n#### Return\n\n\n[DataValidationBuilder](/apps-script/reference/spreadsheet/data-validation-builder) --- a builder based on this rule's settings\n\n*** ** * ** ***\n\n### `get``Allow``Invalid()`\n\nReturns `true` if the rule shows a warning when input fails data validation, or `false` if it rejects the input entirely. The default for new data validation rules is `true`.\n\n#### Return\n\n\n`Boolean` --- `true` if the rule allows input that fails data validation; `false` if not\n\n*** ** * ** ***\n\n### `get``Criteria``Type()`\n\nGets the rule's criteria type as defined in the [DataValidationCriteria](/apps-script/reference/spreadsheet/data-validation-criteria) enum. To get the\narguments for the criteria, use [getCriteriaValues()](#getCriteriaValues()). To use these values to create or\nmodify a data validation rule, see [DataValidationBuilder.withCriteria(criteria, args)](/apps-script/reference/spreadsheet/data-validation-builder#withCriteria(DataValidationCriteria,Object)).\n\n```javascript\n// Log information about the data validation rule for cell A1.\nconst cell = SpreadsheetApp.getActive().getRange('A1');\nconst rule = cell.getDataValidation();\nif (rule != null) {\n const criteria = rule.getCriteriaType();\n const args = rule.getCriteriaValues();\n Logger.log('The data validation rule is %s %s', criteria, args);\n} else {\n Logger.log('The cell does not have a data validation rule.');\n}\n```\n\n#### Return\n\n\n[DataValidationCriteria](/apps-script/reference/spreadsheet/data-validation-criteria) --- the type of data validation criteria\n\n*** ** * ** ***\n\n### `get``Criteria``Values()`\n\nGets an array of arguments for the rule's criteria. To get the criteria type, use [getCriteriaType()](#getCriteriaType()). To use these values to create or modify a data validation rule, see [DataValidationBuilder.withCriteria(criteria, args)](/apps-script/reference/spreadsheet/data-validation-builder#withCriteria(DataValidationCriteria,Object)).\n\n```javascript\n// Log information about the data validation rule for cell A1.\nconst cell = SpreadsheetApp.getActive().getRange('A1');\nconst rule = cell.getDataValidation();\nif (rule != null) {\n const criteria = rule.getCriteriaType();\n const args = rule.getCriteriaValues();\n Logger.log('The data validation rule is %s %s', criteria, args);\n} else {\n Logger.log('The cell does not have a data validation rule.');\n}\n```\n\n#### Return\n\n\n`Object[]` --- an array of arguments appropriate to the rule's criteria type; the number of arguments\nand their type match the corresponding `require...()` method of the [DataValidationBuilder](/apps-script/reference/spreadsheet/data-validation-builder) class\n\n*** ** * ** ***\n\n### `get``Help``Text()`\n\nGets the rule's help text, or `null` if no help text is set.\n\n#### Return\n\n\n`String` --- the rule's help text, or `null` if no help text is set"]]