Class DataValidation Stay organized with collections Save and categorize content based on your preferences.
Page Summary
Use
SpreadsheetApp.newDataValidation()andDataValidationBuilderto create new data validation rules.The
Range.setDataValidation(rule)method sets a data validation rule for a specific range.Data validation rules have methods to get their criteria type, criteria values, whether invalid input is allowed, and associated help text.
The
copy()method creates a builder based on an existing data validation rule's settings, allowing for modifications.
Access data validation rules. To create a new rule, useSpreadsheet andData. You can useRange.setDataValidation(rule) to set the validation rule for a range.
// Log information about the data validation rule for cell A1.constcell=SpreadsheetApp.getActive().getRange('A1');construle=cell.getDataValidation();if(rule!=null){constcriteria=rule.getCriteriaType();constargs=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.');}
Methods
| Method | Return type | Brief description |
|---|---|---|
copy() | Data | Creates a builder for a data validation rule based on this rule's settings. |
get | Boolean | Returnstrue if the rule shows a warning when input fails data validation, orfalse if it rejects the input entirely. |
get | Data | Gets the rule's criteria type as defined in theData enum. |
get | Object[] | Gets an array of arguments for the rule's criteria. |
get | String | Gets the rule's help text, ornull if no help text is set. |
Detailed documentation
copy()
Creates a builder for a data validation rule based on this rule's settings.
// Change existing data validation rules that require a date in 2013 to require// a date in 2014.constoldDates=[newDate('1/1/2013'),newDate('12/31/2013')];constnewDates=[newDate('1/1/2014'),newDate('12/31/2014')];constsheet=SpreadsheetApp.getActiveSheet();constrange=sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns());construles=range.getDataValidations();for(leti=0;i <rules.length;i++){for(letj=0;j <rules[i].length;j++){construle=rules[i][j];if(rule!=null){constcriteria=rule.getCriteriaType();constargs=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);
Return
Data — a builder based on this rule's settings
getAllowInvalid()
Returnstrue if the rule shows a warning when input fails data validation, orfalse if it rejects the input entirely. The default for new data validation rules istrue.
Return
Boolean —true if the rule allows input that fails data validation;false if not
getCriteriaType()
Gets the rule's criteria type as defined in theData enum. To get thearguments for the criteria, useget. To use these values to create ormodify a data validation rule, seeData.
// Log information about the data validation rule for cell A1.constcell=SpreadsheetApp.getActive().getRange('A1');construle=cell.getDataValidation();if(rule!=null){constcriteria=rule.getCriteriaType();constargs=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.');}
Return
Data — the type of data validation criteria
getCriteriaValues()
Gets an array of arguments for the rule's criteria. To get the criteria type, useget. To use these values to create or modify a data validation rule, seeData.
// Log information about the data validation rule for cell A1.constcell=SpreadsheetApp.getActive().getRange('A1');construle=cell.getDataValidation();if(rule!=null){constcriteria=rule.getCriteriaType();constargs=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.');}
Return
Object[] — an array of arguments appropriate to the rule's criteria type; the number of arguments and their type match the correspondingrequire...() method of theData class
getHelpText()
Gets the rule's help text, ornull if no help text is set.
Return
String — the rule's help text, ornull if no help text is set
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.