Class Validation
Range.setValidation(...);
read back with Sheet.validations(). Crosses the boundary as flat JSON.
Types: whole, decimal, list, date,
time, textLength, custom. Operators: between,
notBetween, equal, notEqual, greaterThan,
lessThan, greaterThanOrEqual, lessThanOrEqual.
Operands (formula1, formula2) are formulas without a
leading =, saved as given: a number ("10"), a cell reference
("$A$1") or a function call ("DATE(2020,1,1)"). Unknown
tokens are not rejected; see each builder method for what they become.
sheet.range("B2:B100").setValidation(
Validation.wholeNumber("between", "1", "10"));
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether a blank cell passes the rule.static Validation.Builderbuilder()A builder for any validation type.static ValidationA custom-formula constraint: an entry is valid when the formula returns TRUE.static ValidationA date constraint, for exampledate("between", "DATE(2020,1,1)", "DATE(2020,12,31)").static ValidationA decimal-number constraint, for exampledecimal("greaterThan", "0", null).The text of the error alert; an empty string when none.How firmly a failure is reported:stop(rejects the entry),warningorinformation; an empty string when unset.The title of the error alert; an empty string when none.intThe first column of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back.intfirstRow()The first row of the rule's first range (0-based, inclusive).formula1()The first operand as a formula without a leading=or_xlfn.prefixes, ornullwhen none is set.formula2()The second operand of abetween/notBetweenrule, ornullwhen none is set.static ValidationBuilds a rule from the engine's JSON, the form behindSheet.validations().The text of the input hint; an empty string when none.The title of the input hint; an empty string when none.intThe last column of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back.intlastRow()The last row of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back.static ValidationA drop-down list of the given allowed values, with its in-cell arrow shown.operator()The comparison token.booleanWhether a list rule offers its items through an in-cell drop-down arrow.booleanWhether an invalid entry raises the error alert.booleanWhether the input hint shows when the cell is selected.static ValidationtextLength(String operator, String formula1, String formula2) A text-length constraint on the number of characters, for exampletextLength("lessThanOrEqual", "50", null).static ValidationA time constraint, for exampletime("between", "TIME(9,0,0)", "TIME(17,0,0)").A builder pre-loaded with this rule, for read–modify–write round-trips.toJson()The rule as the JSON object sent to the engine.type()The validation type token;"none"for a read-back rule that allows any value, an empty string when unset.static ValidationwholeNumber(String operator, String formula1, String formula2) A whole-number constraint, for examplewholeNumber("between", "1", "10").
-
Method Details
-
list
A drop-down list of the given allowed values, with its in-cell arrow shown.The values are written as one quoted list literal, as Excel writes it (
"Draft,Approved"); an unquoted list would be read as a name. A double quote inside a value is escaped by doubling it.- Throws:
IllegalArgumentException- if no value is given, or a value is null or contains a comma — the list literal separates its values with commas, so such a value would silently become several. List values like that from a range withbuilder().type("list").formula1("$X$1:$X$9").
-
wholeNumber
A whole-number constraint, for examplewholeNumber("between", "1", "10").- Parameters:
operator- a comparison token; seeValidation.Builder.operator(String)formula1- the first operand, a formula without a leading=formula2- the second operand, used only bybetweenandnotBetween;nullotherwise
-
decimal
A decimal-number constraint, for exampledecimal("greaterThan", "0", null).- Parameters:
operator- a comparison token; seeValidation.Builder.operator(String)formula1- the first operand, a formula without a leading=formula2- the second operand, used only bybetweenandnotBetween;nullotherwise
-
date
A date constraint, for exampledate("between", "DATE(2020,1,1)", "DATE(2020,12,31)").The operands are formulas, not date text. Pass a
DATEcall, a date serial number ("43831"is 2020-01-01 in the 1900 date system) or a cell reference. A string such as"2020-01-01"is taken as a formula: Excel reads it as the subtraction 2020 - 1 - 1, the serial number 2018, which is not the intended date.- Parameters:
operator- a comparison token; seeValidation.Builder.operator(String)formula1- the first operand, a formula without a leading=formula2- the second operand, used only bybetweenandnotBetween;nullotherwise
-
time
A time constraint, for exampletime("between", "TIME(9,0,0)", "TIME(17,0,0)"). The operands are formulas: aTIMEcall, a day fraction ("0.5"is noon) or a cell reference.- Parameters:
operator- a comparison token; seeValidation.Builder.operator(String)formula1- the first operand, a formula without a leading=formula2- the second operand, used only bybetweenandnotBetween;nullotherwise
-
textLength
A text-length constraint on the number of characters, for exampletextLength("lessThanOrEqual", "50", null).- Parameters:
operator- a comparison token; seeValidation.Builder.operator(String)formula1- the first operand, a formula without a leading=formula2- the second operand, used only bybetweenandnotBetween;nullotherwise
-
custom
A custom-formula constraint: an entry is valid when the formula returns TRUE. Write the formula for the top-left cell of the range, without a leading=, for examplecustom("ISNUMBER(A1)"); relative references shift for the other cells, as in Excel. -
builder
A builder for any validation type. It starts withallowBlank = true,showErrorMessage = trueanderrorStyle = "stop"; a rule with no operator usesbetween. -
toBuilder
A builder pre-loaded with this rule, for read–modify–write round-trips. -
toJson
The rule as the JSON object sent to the engine. Used by the SDK. -
fromJson
Builds a rule from the engine's JSON, the form behindSheet.validations(). Used by the SDK. -
type
The validation type token;"none"for a read-back rule that allows any value, an empty string when unset. -
operator
The comparison token. A read-back rule always carries one (betweenwhen the file had none); an empty string when unset. -
formula1
The first operand as a formula without a leading=or_xlfn.prefixes, ornullwhen none is set. -
formula2
The second operand of abetween/notBetweenrule, ornullwhen none is set. -
allowBlank
public boolean allowBlank()Whether a blank cell passes the rule. -
showDropdown
public boolean showDropdown()Whether a list rule offers its items through an in-cell drop-down arrow. Alwaysfalsefor a rule that is not a list. -
errorStyle
How firmly a failure is reported:stop(rejects the entry),warningorinformation; an empty string when unset. -
errorTitle
The title of the error alert; an empty string when none. -
errorMessage
The text of the error alert; an empty string when none. -
showErrorMessage
public boolean showErrorMessage()Whether an invalid entry raises the error alert. -
inputTitle
The title of the input hint; an empty string when none. -
inputMessage
The text of the input hint; an empty string when none. -
showInputMessage
public boolean showInputMessage()Whether the input hint shows when the cell is selected. -
firstRow
public int firstRow()The first row of the rule's first range (0-based, inclusive). A rule that covers several ranges reports only its first range. 0 for a rule that was built, not read back. -
firstColumn
public int firstColumn()The first column of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back. -
lastRow
public int lastRow()The last row of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back. -
lastColumn
public int lastColumn()The last column of the rule's first range (0-based, inclusive); 0 for a rule that was built, not read back.
-