Top Description Inners Fields Constructors Methods
org.apache.poi.ss.formula

public Class DataValidationEvaluator

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collections, .HashMap, .List, .Map, org.apache.poi.ss.formula.eval.BlankEval, .BoolEval, .ErrorEval, .NumberEval, .RefEval, .StringEval, .ValueEval, org.apache.poi.ss.usermodel.Cell, .CellType, .DataValidation, .DataValidationConstraint, .DataValidationConstraint.OperatorType, .DataValidationConstraint.ValidationType, .Sheet, .Workbook, org.apache.poi.ss.util.CellRangeAddressBase, .CellRangeAddressList, .CellReference, .SheetUtil, org.apache.poi.util.StringUtil

Evaluates Data Validation constraints.

For performance reasons, this class keeps a cache of all previously retrieved DataValidation instances. Be sure to call clearAllCachedValues() if any workbook validation definitions are added, modified, or deleted.

Changing cell values should be fine, as long as the corresponding WorkbookEvaluator#clearAllCachedResultValues() is called as well.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DataValidationEvaluator.DataValidationContext

This class organizes and encapsulates all the pieces of information related to a single data validation configuration for a single cell.

public static enum
DataValidationEvaluator.OperatorEnum

Not calling it OperatorType to avoid confusion for now with DataValidationConstraint.OperatorType.

public static enum
DataValidationEvaluator.ValidationEnum

Not calling it ValidationType to avoid confusion for now with DataValidationConstraint.ValidationType.

Field Summary

Modifier and TypeField and Description
private final Map<String, List<? extends DataValidation>>
validations

Expensive to compute, so cache them as they are retrieved.

private final Workbook
private final WorkbookEvaluator

Constructor Summary

AccessConstructor and Description
public
DataValidationEvaluator(Workbook
the workbook this operates on
wb
,
WorkbookEvaluatorProvider
provider for formula evaluation
provider
)

Use the same formula evaluation context used for other operations, so cell value changes are automatically noticed

Method Summary

Modifier and TypeMethod and Description
public void
clearAllCachedValues()

Call this whenever validation structures change, so future results stay in sync with the Workbook state.

public DataValidationEvaluator.DataValidationContext

Returns:

the DataValidationContext applicable to the given cell, or null if no validation applies
getValidationContextForCell
(CellReference
reference to check
cell
)

Finds and returns the DataValidationContext for the cell, if there is one.

public DataValidation

Returns:

the DataValidation applicable to the given cell, or null if no validation applies
getValidationForCell
(CellReference
reference to check - use this in case the cell does not actually exist yet
cell
)

Finds and returns the DataValidation for the cell, if there is one.

private List<? extends DataValidation>

Returns:

The DataValidations for the sheet
getValidations
(Sheet
The Sheet to load validations for.
sheet
)

Lazy load validations by sheet, since reading the CT* types is expensive

public List<ValueEval>

Returns:

returns an unmodifiable List of ValueEvals if applicable, or null
getValidationValuesForCell
(CellReference
reference to check - use this in case the cell does not actually exist yet
cell
)

If getValidationForCell(CellReference) returns an instance, and the ValidationType is ValidationType#LIST, return the valid values, whether they are from a static list or cell range.

protected static List<ValueEval>

Returns:

returns an unmodifiable List of ValueEvals, which may be empty
getValidationValuesForConstraint
(DataValidationEvaluator.DataValidationContext context)

static so enums can reference it without creating a whole instance

protected WorkbookEvaluator

Returns:

evaluator
getWorkbookEvaluator
()

public static boolean

Returns:

true if the cell or cached cell formula result type match the given type
isType
(Cell
The Cell to check.
cell
,
CellType
The CellType to check for.
type
)

Note that this assumes the cell cached value is up to date and in sync with data edits

public boolean

Returns:

true if the cell has no validation or the cell value passes the defined validation, false if it fails
isValidCell
(CellReference
The reference of the cell to evaluate
cellRef
)

Use the validation returned by getValidationForCell(CellReference) if you want the error display details.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

validationsback to summary
private final Map<String, List<? extends DataValidation>> validations

Expensive to compute, so cache them as they are retrieved.

Sheets don't implement equals, and since its an interface, there's no guarantee instances won't be recreated on the fly by some implementation. So we use sheet name.

workbookback to summary
private final Workbook workbook
workbookEvaluatorback to summary
private final WorkbookEvaluator workbookEvaluator

Constructor Detail

DataValidationEvaluatorback to summary
public DataValidationEvaluator(Workbook wb, WorkbookEvaluatorProvider provider)

Use the same formula evaluation context used for other operations, so cell value changes are automatically noticed

Parameters
wb:Workbook

the workbook this operates on

provider:WorkbookEvaluatorProvider

provider for formula evaluation

Method Detail

clearAllCachedValuesback to summary
public void clearAllCachedValues()

Call this whenever validation structures change, so future results stay in sync with the Workbook state.

getValidationContextForCellback to summary
public DataValidationEvaluator.DataValidationContext getValidationContextForCell(CellReference cell)

Finds and returns the DataValidationContext for the cell, if there is one. Lookup is based on the first match from DataValidation#getRegions() for the cell's sheet. DataValidation regions must be in the same sheet as the DataValidation. Allowed values expressions may reference other sheets, however.

Parameters
cell:CellReference

reference to check

Returns:DataValidationEvaluator.DataValidationContext

the DataValidationContext applicable to the given cell, or null if no validation applies

getValidationForCellback to summary
public DataValidation getValidationForCell(CellReference cell)

Finds and returns the DataValidation for the cell, if there is one. Lookup is based on the first match from DataValidation#getRegions() for the cell's sheet. DataValidation regions must be in the same sheet as the DataValidation. Allowed values expressions may reference other sheets, however.

Parameters
cell:CellReference

reference to check - use this in case the cell does not actually exist yet

Returns:DataValidation

the DataValidation applicable to the given cell, or null if no validation applies

getValidationsback to summary
private List<? extends DataValidation> getValidations(Sheet sheet)

Lazy load validations by sheet, since reading the CT* types is expensive

Parameters
sheet:Sheet

The Sheet to load validations for.

Returns:List<? extends DataValidation>

The DataValidations for the sheet

getValidationValuesForCellback to summary
public List<ValueEval> getValidationValuesForCell(CellReference cell)

If getValidationForCell(CellReference) returns an instance, and the ValidationType is ValidationType#LIST, return the valid values, whether they are from a static list or cell range.

For all other validation types, or no validation at all, this method returns null.

This method could throw an exception if the validation type is not LIST, but since this method is mostly useful in UI contexts, null seems the easier path.

Parameters
cell:CellReference

reference to check - use this in case the cell does not actually exist yet

Returns:List<ValueEval>

returns an unmodifiable List of ValueEvals if applicable, or null

getValidationValuesForConstraintback to summary
protected static List<ValueEval> getValidationValuesForConstraint(DataValidationEvaluator.DataValidationContext context)

static so enums can reference it without creating a whole instance

Returns:List<ValueEval>

returns an unmodifiable List of ValueEvals, which may be empty

getWorkbookEvaluatorback to summary
protected WorkbookEvaluator getWorkbookEvaluator()
Returns:WorkbookEvaluator

evaluator

isTypeback to summary
public static boolean isType(Cell cell, CellType type)

Note that this assumes the cell cached value is up to date and in sync with data edits

Parameters
cell:Cell

The Cell to check.

type:CellType

The CellType to check for.

Returns:boolean

true if the cell or cached cell formula result type match the given type

isValidCellback to summary
public boolean isValidCell(CellReference cellRef)

Use the validation returned by getValidationForCell(CellReference) if you want the error display details. This is the validation checked by this method, which attempts to replicate Excel's data validation rules.

Note that to properly apply some validations, care must be taken to offset the base validation formula by the relative position of the current cell, or the wrong value is checked.

Parameters
cellRef:CellReference

The reference of the cell to evaluate

Returns:boolean

true if the cell has no validation or the cell value passes the defined validation, false if it fails

org.apache.poi.ss.formula back to summary

public Class DataValidationEvaluator.DataValidationContext

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.poi.ss.formula.DataValidationEvaluator.DataValidationContext

This class organizes and encapsulates all the pieces of information related to a single data validation configuration for a single cell. It cleanly separates the validation region, the cells it applies to, the specific cell this instance references, and the validation configuration and current values if applicable.

Field Summary

Modifier and TypeField and Description
private final DataValidation
private final DataValidationEvaluator
private final CellRangeAddressBase
private final CellReference

Constructor Summary

AccessConstructor and Description
public
DataValidationContext(DataValidation dv, DataValidationEvaluator dve, CellRangeAddressBase region, CellReference target)

Populate the context with the necessary values.

Method Summary

Modifier and TypeMethod and Description
public DataValidationEvaluator

Returns:

the dve
getEvaluator
()

public String
public String
public int
public int
public int
public CellRangeAddressBase

Returns:

the region
getRegion
()

public int
public CellReference

Returns:

the target
getTarget
()

public DataValidation

Returns:

the dv
getValidation
()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

dvback to summary
private final DataValidation dv
dveback to summary
private final DataValidationEvaluator dve
regionback to summary
private final CellRangeAddressBase region
targetback to summary
private final CellReference target

Constructor Detail

DataValidationContextback to summary
public DataValidationContext(DataValidation dv, DataValidationEvaluator dve, CellRangeAddressBase region, CellReference target)

Populate the context with the necessary values.

Method Detail

getEvaluatorback to summary
public DataValidationEvaluator getEvaluator()
Returns:DataValidationEvaluator

the dve

getFormula1back to summary
public String getFormula1()
getFormula2back to summary
public String getFormula2()
getOffsetColumnsback to summary
public int getOffsetColumns()
getOffsetRowsback to summary
public int getOffsetRows()
getOperatorback to summary
public int getOperator()
getRegionback to summary
public CellRangeAddressBase getRegion()
Returns:CellRangeAddressBase

the region

getSheetIndexback to summary
public int getSheetIndex()
getTargetback to summary
public CellReference getTarget()
Returns:CellReference

the target

getValidationback to summary
public DataValidation getValidation()
Returns:DataValidation

the dv

org.apache.poi.ss.formula back to summary

public sealed Enum DataValidationEvaluator.OperatorEnum

extends Enum<DataValidationEvaluator.OperatorEnum>
Class Inheritance

Not calling it OperatorType to avoid confusion for now with DataValidationConstraint.OperatorType. Definition order matches OOXML type ID indexes

Field Summary

Modifier and TypeField and Description
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum
public static final DataValidationEvaluator.OperatorEnum

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public abstract boolean

Returns:

true if the comparison is valid
isValid
(Double
won't be null, assumption is previous checks handled that
cellValue
,
Double
if null, value assumed invalid, anything passes, per Excel behavior
v1
,
Double
null if not needed. If null when needed, assume anything passes, per Excel behavior
v2
)

Evaluates comparison using operator instance rules

public static DataValidationEvaluator.OperatorEnum
public static DataValidationEvaluator.OperatorEnum[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BETWEENback to summary
public static final DataValidationEvaluator.OperatorEnum BETWEEN
EQUALback to summary
public static final DataValidationEvaluator.OperatorEnum EQUAL
GREATER_OR_EQUALback to summary
public static final DataValidationEvaluator.OperatorEnum GREATER_OR_EQUAL
GREATER_THANback to summary
public static final DataValidationEvaluator.OperatorEnum GREATER_THAN
IGNOREDback to summary
public static final DataValidationEvaluator.OperatorEnum IGNORED
LESS_OR_EQUALback to summary
public static final DataValidationEvaluator.OperatorEnum LESS_OR_EQUAL
LESS_THANback to summary
public static final DataValidationEvaluator.OperatorEnum LESS_THAN
NOT_BETWEENback to summary
public static final DataValidationEvaluator.OperatorEnum NOT_BETWEEN
NOT_EQUALback to summary
public static final DataValidationEvaluator.OperatorEnum NOT_EQUAL

Constructor Detail

OperatorEnumback to summary
private OperatorEnum()

Method Detail

isValidback to summary
public abstract boolean isValid(Double cellValue, Double v1, Double v2)

Evaluates comparison using operator instance rules

Parameters
cellValue:Double

won't be null, assumption is previous checks handled that

v1:Double

if null, value assumed invalid, anything passes, per Excel behavior

v2:Double

null if not needed. If null when needed, assume anything passes, per Excel behavior

Returns:boolean

true if the comparison is valid

valueOfback to summary
public static DataValidationEvaluator.OperatorEnum valueOf(String name)
valuesback to summary
public static DataValidationEvaluator.OperatorEnum[] values()
org.apache.poi.ss.formula back to summary

public sealed Enum DataValidationEvaluator.ValidationEnum

extends Enum<DataValidationEvaluator.ValidationEnum>
Class Inheritance

Not calling it ValidationType to avoid confusion for now with DataValidationConstraint.ValidationType. Definition order matches OOXML type ID indexes

Field Summary

Modifier and TypeField and Description
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum
public static final DataValidationEvaluator.ValidationEnum

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private Double

Returns:

numeric value or null if not defined or the formula evaluates to an empty/missing cell.
evalOrConstant
(String
The text of the formula or a numeric value
formula
,
DataValidationEvaluator.DataValidationContext
The DataValidationContext which is used for evaluation
context
)

Evaluate a numeric formula value as either a constant or numeric expression.

public static boolean

Returns:

true if validation passes
isValid
(Cell
Cell to check validity of
cell
,
DataValidationEvaluator.DataValidationContext
The Data Validation to check against
context
)

Validates against the type defined in context, as an index of the enum values array.

protected boolean

Returns:

if the cell is a valid numeric cell for the validation or not
isValidNumericCell
(Cell cell, DataValidationEvaluator.DataValidationContext context)

Uses the cell value, which may be the cached formula result value.

protected boolean
isValidNumericValue(Double value, final DataValidationEvaluator.DataValidationContext context)

Is the number a valid option for the validation?

public boolean
public static DataValidationEvaluator.ValidationEnum
public static DataValidationEvaluator.ValidationEnum[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ANYback to summary
public static final DataValidationEvaluator.ValidationEnum ANY
DATEback to summary
public static final DataValidationEvaluator.ValidationEnum DATE
DECIMALback to summary
public static final DataValidationEvaluator.ValidationEnum DECIMAL
FORMULAback to summary
public static final DataValidationEvaluator.ValidationEnum FORMULA
INTEGERback to summary
public static final DataValidationEvaluator.ValidationEnum INTEGER
LISTback to summary
public static final DataValidationEvaluator.ValidationEnum LIST
TEXT_LENGTHback to summary
public static final DataValidationEvaluator.ValidationEnum TEXT_LENGTH
TIMEback to summary
public static final DataValidationEvaluator.ValidationEnum TIME

Constructor Detail

ValidationEnumback to summary
private ValidationEnum()

Method Detail

evalOrConstantback to summary
private Double evalOrConstant(String formula, DataValidationEvaluator.DataValidationContext context) throws NumberFormatException

Evaluate a numeric formula value as either a constant or numeric expression. Note that Excel treats validations with constraint formulas that evaluate to null as valid, but evaluations in error or non-numeric are marked invalid.

Parameters
formula:String

The text of the formula or a numeric value

context:DataValidationEvaluator.DataValidationContext

The DataValidationContext which is used for evaluation

Returns:Double

numeric value or null if not defined or the formula evaluates to an empty/missing cell.

Exceptions
NumberFormatException:
if the formula is non-numeric when it should be
isValidback to summary
public static boolean isValid(Cell cell, DataValidationEvaluator.DataValidationContext context)

Validates against the type defined in context, as an index of the enum values array.

Parameters
cell:Cell

Cell to check validity of

context:DataValidationEvaluator.DataValidationContext

The Data Validation to check against

Returns:boolean

true if validation passes

Exceptions
ArrayIndexOutOfBoundsException:
if the constraint type is an invalid index
isValidNumericCellback to summary
protected boolean isValidNumericCell(Cell cell, DataValidationEvaluator.DataValidationContext context)

Uses the cell value, which may be the cached formula result value. We won't re-evaluate cells here. This validation would be after the cell value was updated externally. Excel allows invalid values through methods like copy/paste, and only validates them when the user interactively edits the cell.

Returns:boolean

if the cell is a valid numeric cell for the validation or not

isValidNumericValueback to summary
protected boolean isValidNumericValue(Double value, final DataValidationEvaluator.DataValidationContext context)

Is the number a valid option for the validation?

isValidValueback to summary
public boolean isValidValue(Cell cell, DataValidationEvaluator.DataValidationContext context)
valueOfback to summary
public static DataValidationEvaluator.ValidationEnum valueOf(String name)
valuesback to summary
public static DataValidationEvaluator.ValidationEnum[] values()