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

public final Class WorkbookEvaluator

extends Object
Class Inheritance
Annotations
@Internal
Imports
java.util.Arrays, .Collection, .Collections, .IdentityHashMap, .Map, .Stack, .TreeSet, org.apache.logging.log4j.LogManager, .Logger, org.apache.logging.log4j.message.SimpleMessage, org.apache.poi.ss.SpreadsheetVersion, org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException, org.apache.poi.ss.formula.atp.AnalysisToolPak, org.apache.poi.ss.formula.eval.*, org.apache.poi.ss.formula.function.FunctionMetadataRegistry, org.apache.poi.ss.formula.functions.*, org.apache.poi.ss.formula.ptg.*, org.apache.poi.ss.formula.udf.AggregatingUDFFinder, .UDFFinder, org.apache.poi.ss.usermodel.CellType, org.apache.poi.ss.util.CellRangeAddressBase, .CellReference, org.apache.poi.util.Internal

Evaluates formula cells.

For performance reasons, this class keeps a cache of all previously calculated intermediate cell values. Be sure to call clearAllCachedResultValues() if any workbook cells are changed between calls to evaluate~ methods on this class.

Field Summary

Modifier and TypeField and Description
private EvaluationCache
private CollaboratingWorkbooksEnvironment
private final IEvaluationListener
private boolean
private final Map<String, Integer>
private final Map<EvaluationSheet, Integer>
private final IStabilityClassifier
private final AggregatingUDFFinder
private final EvaluationWorkbook
private int
_workbookIx

part of cache entry key (useful when evaluating multiple workbooks)

private boolean
dbgEvaluationOutputForNextEval

whether print detailed messages about the next formula evaluation

private int
private static final Logger
private static final Logger

Constructor Summary

AccessConstructor and Description
public
WorkbookEvaluator(EvaluationWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder
pass null for default (AnalysisToolPak only)
udfFinder
)

pack-priv
WorkbookEvaluator(EvaluationWorkbook workbook, IEvaluationListener evaluationListener, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)

Method Summary

Modifier and TypeMethod and Description
private NotImplementedException
addExceptionInfo(NotImplementedException inner, int sheetIndex, int rowIndex, int columnIndex)

Adds the current cell reference to the exception for easier debugging.

private boolean

Returns:

true if any Ptg references were shifted
adjustRegionRelativeReference
(Ptg[] ptgs, CellReference
cell within the region to use.
target
,
CellRangeAddressBase
containing the cell, OR, for conditional format rules with multiple ranges, the region with the top-left-most cell
region
)

Adjust formula relative references by the offset between the start of the given region and the given target cell.

pack-priv void
attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx)

public void
clearAllCachedResultValues()

Should be called whenever there are changes to input cells in the evaluated workbook.

private static int

Returns:

the number of tokens (starting from startIndex+1) that need to be skipped to achieve the specified distInBytes skip distance.
countTokensToBeSkipped
(Ptg[] ptgs, int startIndex, int distInBytes)

Calculates the number of tokens that the evaluator should skip upon reaching a tAttrSkip.

private static ValueEval

Returns:

a NumberEval, StringEval, BoolEval, or ErrorEval. Never null. BlankEval is converted to NumberEval#ZERO
dereferenceResult
(ValueEval evaluationResult, OperationEvaluationContext ec)

Dereferences a single value from any AreaEval or RefEval evaluation result.

public static ValueEval

Returns:

a NumberEval, StringEval, BoolEval, or ErrorEval. Never null. BlankEval is converted to NumberEval#ZERO
dereferenceResult
(ValueEval evaluationResult, int srcRowNum, int srcColNum)

Dereferences a single value from any AreaEval or RefEval evaluation result.

pack-priv void
detachFromEnvironment()

Discards the current workbook environment and attaches to the default 'empty' environment.

public ValueEval
public ValueEval

Returns:

value
evaluate
(String
to evaluate
formula
,
CellReference
defines the optional sheet and row/column base for the formula, if it is relative
ref
)

Evaluate a formula outside a cell value, e.g. conditional format rules or data validation expressions

public ValueEval

Returns:

value
evaluate
(String
The formula to evaluate
formula
,
CellReference
cell context for the operation
target
,
CellRangeAddressBase
containing the cell
region
)

Some expressions need to be evaluated in terms of an offset from the top left corner of a region, such as some data validation and conditional format expressions, when those constraints apply to contiguous cells.

private ValueEval
evaluate(String formula, CellReference target, CellRangeAddressBase region, FormulaType formulaType)

private ValueEval

Returns:

never null, never BlankEval
evaluateAny
(EvaluationCell srcCell, int sheetIndex, int rowIndex, int columnIndex, EvaluationTracker tracker)

pack-priv ValueEval
public ValueEval

Returns:

ValueEval for one or more values
evaluateList
(String
The formula to evaluate
formula
,
CellReference
cell context for the operation
target
,
CellRangeAddressBase
containing the cell
region
)

Some expressions need to be evaluated in terms of an offset from the top left corner of a region, such as some data validation and conditional format expressions, when those constraints apply to contiguous cells.

pack-priv ValueEval
evaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec)

YK: Used by OperationEvaluationContext to resolve indirect names.

pack-priv ValueEval
evaluateReference(EvaluationSheet sheet, int sheetIndex, int rowIndex, int columnIndex, EvaluationTracker tracker)

Used by the lazy ref evals whenever they need to get the value of a contained cell.

public FreeRefFunction
pack-priv CollaboratingWorkbooksEnvironment
private ValueEval
private ValueEval
getEvalForPtg(Ptg ptg, OperationEvaluationContext ec)

returns an appropriate Eval impl instance for the Ptg.

pack-priv IEvaluationListener
pack-priv EvaluationName
getName(String name, int sheetIndex)

public static Collection<String>

Returns:

names of functions NOT supported by POI
getNotSupportedFunctionNames
()

Return a collection of functions that POI does not support

pack-priv WorkbookEvaluator

Returns:

the evaluator for another workbook which is part of the same CollaboratingWorkbooksEnvironment
getOtherWorkbookEvaluator
(String workbookName)

pack-priv EvaluationSheet
getSheet(int sheetIndex)

private int
pack-priv int

Returns:

-1 if sheet with specified name does not exist
getSheetIndex
(String sheetName)

Case-insensitive.

pack-priv int
getSheetIndexByExternIndex(int externSheetIndex)

pack-priv String
getSheetName(int sheetIndex)

also for debug use.

public static Collection<String>

Returns:

names of functions supported by POI
getSupportedFunctionNames
()

Return a collection of functions that POI can evaluate

pack-priv static ValueEval

Returns:

BlankEval if cell is null or blank, never null
getValueFromNonFormulaCell
(EvaluationCell
may be null
cell
)

Gets the value from a non-formula cell.

pack-priv EvaluationWorkbook
public boolean
public boolean
public void
notifyDeleteCell(EvaluationCell cell)

Should be called to tell the cell value cache that the specified cell has just been deleted.

public void
notifyUpdateCell(EvaluationCell cell)

Should be called to tell the cell value cache that the specified (value or formula) cell has changed.

private ValueEval
public static void
registerFunction(String
the function name
name
,
FreeRefFunction
the function to register
func
)

Register a ATP function in runtime.

public static void
registerFunction(String
the function name
name
,
Function
the function to register
func
)

Register a function in runtime.

public void
public void
setIgnoreMissingWorkbooks(boolean
whether to ignore missing references to external workbooks
ignore
)

Whether to ignore missing references to external workbooks and use cached formula results in the main workbook instead.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_cacheback to summary
private EvaluationCache _cache
_collaboratingWorkbookEnvironmentback to summary
private CollaboratingWorkbooksEnvironment _collaboratingWorkbookEnvironment
_evaluationListenerback to summary
private final IEvaluationListener _evaluationListener
_ignoreMissingWorkbooksback to summary
private boolean _ignoreMissingWorkbooks
_sheetIndexesByNameback to summary
private final Map<String, Integer> _sheetIndexesByName
_sheetIndexesBySheetback to summary
private final Map<EvaluationSheet, Integer> _sheetIndexesBySheet
_stabilityClassifierback to summary
private final IStabilityClassifier _stabilityClassifier
_udfFinderback to summary
private final AggregatingUDFFinder _udfFinder
_workbookback to summary
private final EvaluationWorkbook _workbook
_workbookIxback to summary
private int _workbookIx

part of cache entry key (useful when evaluating multiple workbooks)

dbgEvaluationOutputForNextEvalback to summary
private boolean dbgEvaluationOutputForNextEval

whether print detailed messages about the next formula evaluation

dbgEvaluationOutputIndentback to summary
private int dbgEvaluationOutputIndent
EVAL_LOGback to summary
private static final Logger EVAL_LOG
LOGback to summary
private static final Logger LOG

Constructor Detail

WorkbookEvaluatorback to summary
public WorkbookEvaluator(EvaluationWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)
Parameters
udfFinder:UDFFinder

pass null for default (AnalysisToolPak only)

WorkbookEvaluatorback to summary
pack-priv WorkbookEvaluator(EvaluationWorkbook workbook, IEvaluationListener evaluationListener, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder)

Method Detail

addExceptionInfoback to summary
private NotImplementedException addExceptionInfo(NotImplementedException inner, int sheetIndex, int rowIndex, int columnIndex)

Adds the current cell reference to the exception for easier debugging. Would be nice to get the formula text as well, but that seems to require too much digging around and casting to get the FormulaRenderingWorkbook.

adjustRegionRelativeReferenceback to summary
private boolean adjustRegionRelativeReference(Ptg[] ptgs, CellReference target, CellRangeAddressBase region)

Adjust formula relative references by the offset between the start of the given region and the given target cell. That is, treat the region top-left cell as "A1" for the purposes of evaluating relative reference components (row and/or column), and further move references by the position of the target within the region.

formula ref + range top-left + current cell range offset which simplifies to formula ref + current cell ref

Parameters
target:CellReference

cell within the region to use.

region:CellRangeAddressBase

containing the cell, OR, for conditional format rules with multiple ranges, the region with the top-left-most cell

Returns:boolean

true if any Ptg references were shifted

Exceptions
IndexOutOfBoundsException:
if the resulting shifted row/column indexes are over the document format limits
IllegalArgumentException:
if target is not within region.
attachToEnvironmentback to summary
pack-priv void attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx)
clearAllCachedResultValuesback to summary
public void clearAllCachedResultValues()

Should be called whenever there are changes to input cells in the evaluated workbook. Failure to call this method after changing cell values will cause incorrect behaviour of the evaluate~ methods of this class

countTokensToBeSkippedback to summary
private static int countTokensToBeSkipped(Ptg[] ptgs, int startIndex, int distInBytes)

Calculates the number of tokens that the evaluator should skip upon reaching a tAttrSkip.

Returns:int

the number of tokens (starting from startIndex+1) that need to be skipped to achieve the specified distInBytes skip distance.

dereferenceResultback to summary
private static ValueEval dereferenceResult(ValueEval evaluationResult, OperationEvaluationContext ec)

Dereferences a single value from any AreaEval or RefEval evaluation result. If the supplied evaluationResult is just a plain value, it is returned as-is.

Returns:ValueEval

a NumberEval, StringEval, BoolEval, or ErrorEval. Never null. BlankEval is converted to NumberEval#ZERO

dereferenceResultback to summary
public static ValueEval dereferenceResult(ValueEval evaluationResult, int srcRowNum, int srcColNum)

Dereferences a single value from any AreaEval or RefEval evaluation result. If the supplied evaluationResult is just a plain value, it is returned as-is.

Returns:ValueEval

a NumberEval, StringEval, BoolEval, or ErrorEval. Never null. BlankEval is converted to NumberEval#ZERO

detachFromEnvironmentback to summary
pack-priv void detachFromEnvironment()

Discards the current workbook environment and attaches to the default 'empty' environment. Also resets evaluation cache.

evaluateback to summary
public ValueEval evaluate(EvaluationCell srcCell)
evaluateback to summary
public ValueEval evaluate(String formula, CellReference ref)

Evaluate a formula outside a cell value, e.g. conditional format rules or data validation expressions

Parameters
formula:String

to evaluate

ref:CellReference

defines the optional sheet and row/column base for the formula, if it is relative

Returns:ValueEval

value

evaluateback to summary
public ValueEval evaluate(String formula, CellReference target, CellRangeAddressBase region)

Some expressions need to be evaluated in terms of an offset from the top left corner of a region, such as some data validation and conditional format expressions, when those constraints apply to contiguous cells. When a relative formula is used, it must be evaluated by shifting by the target offset position relative to the top left of the range.

Returns a single value e.g. a cell formula result or boolean value for conditional formatting.

Parameters
formula:String

The formula to evaluate

target:CellReference

cell context for the operation

region:CellRangeAddressBase

containing the cell

Returns:ValueEval

value

Exceptions
IllegalArgumentException:
if target does not define a sheet name to evaluate the formula on.
evaluateback to summary
private ValueEval evaluate(String formula, CellReference target, CellRangeAddressBase region, FormulaType formulaType)
evaluateAnyback to summary
private ValueEval evaluateAny(EvaluationCell srcCell, int sheetIndex, int rowIndex, int columnIndex, EvaluationTracker tracker)
Returns:ValueEval

never null, never BlankEval

evaluateFormulaback to summary
pack-priv ValueEval evaluateFormula(OperationEvaluationContext ec, Ptg[] ptgs)
Annotations
@Internal
evaluateListback to summary
public ValueEval evaluateList(String formula, CellReference target, CellRangeAddressBase region)

Some expressions need to be evaluated in terms of an offset from the top left corner of a region, such as some data validation and conditional format expressions, when those constraints apply to contiguous cells. When a relative formula is used, it must be evaluated by shifting by the target offset position relative to the top left of the range.

Returns a ValueEval that may be one or more values, such as the allowed values for a data validation constraint.

Parameters
formula:String

The formula to evaluate

target:CellReference

cell context for the operation

region:CellRangeAddressBase

containing the cell

Returns:ValueEval

ValueEval for one or more values

Exceptions
IllegalArgumentException:
if target does not define a sheet name to evaluate the formula on.
evaluateNameFormulaback to summary
pack-priv ValueEval evaluateNameFormula(Ptg[] ptgs, OperationEvaluationContext ec)

YK: Used by OperationEvaluationContext to resolve indirect names.

evaluateReferenceback to summary
pack-priv ValueEval evaluateReference(EvaluationSheet sheet, int sheetIndex, int rowIndex, int columnIndex, EvaluationTracker tracker)

Used by the lazy ref evals whenever they need to get the value of a contained cell.

findUserDefinedFunctionback to summary
public FreeRefFunction findUserDefinedFunction(String functionName)
getEnvironmentback to summary
pack-priv CollaboratingWorkbooksEnvironment getEnvironment()
getEvalForNameRecordback to summary
private ValueEval getEvalForNameRecord(EvaluationName nameRecord, OperationEvaluationContext ec)
getEvalForPtgback to summary
private ValueEval getEvalForPtg(Ptg ptg, OperationEvaluationContext ec)

returns an appropriate Eval impl instance for the Ptg. The Ptg must be one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg, StringPtg, BoolPtg

special Note: OperationPtg subtypes cannot be passed here!

getEvaluationListenerback to summary
pack-priv IEvaluationListener getEvaluationListener()
getNameback to summary
pack-priv EvaluationName getName(String name, int sheetIndex)
getNotSupportedFunctionNamesback to summary
public static Collection<String> getNotSupportedFunctionNames()

Return a collection of functions that POI does not support

Returns:Collection<String>

names of functions NOT supported by POI

getOtherWorkbookEvaluatorback to summary
pack-priv WorkbookEvaluator getOtherWorkbookEvaluator(String workbookName) throws WorkbookNotFoundException
Returns:WorkbookEvaluator

the evaluator for another workbook which is part of the same CollaboratingWorkbooksEnvironment

getSheetback to summary
pack-priv EvaluationSheet getSheet(int sheetIndex)
getSheetIndexback to summary
private int getSheetIndex(EvaluationSheet sheet)
getSheetIndexback to summary
pack-priv int getSheetIndex(String sheetName)

Case-insensitive.

Returns:int

-1 if sheet with specified name does not exist

getSheetIndexByExternIndexback to summary
pack-priv int getSheetIndexByExternIndex(int externSheetIndex)
getSheetNameback to summary
pack-priv String getSheetName(int sheetIndex)

also for debug use. Used in toString methods

getSupportedFunctionNamesback to summary
public static Collection<String> getSupportedFunctionNames()

Return a collection of functions that POI can evaluate

Returns:Collection<String>

names of functions supported by POI

getValueFromNonFormulaCellback to summary
pack-priv static ValueEval getValueFromNonFormulaCell(EvaluationCell cell)

Gets the value from a non-formula cell.

Parameters
cell:EvaluationCell

may be null

Returns:ValueEval

BlankEval if cell is null or blank, never null

getWorkbookback to summary
pack-priv EvaluationWorkbook getWorkbook()
isDebugEvaluationOutputForNextEvalback to summary
public boolean isDebugEvaluationOutputForNextEval()
isIgnoreMissingWorkbooksback to summary
public boolean isIgnoreMissingWorkbooks()
notifyDeleteCellback to summary
public void notifyDeleteCell(EvaluationCell cell)

Should be called to tell the cell value cache that the specified cell has just been deleted.

notifyUpdateCellback to summary
public void notifyUpdateCell(EvaluationCell cell)

Should be called to tell the cell value cache that the specified (value or formula) cell has changed.

processNameEvalback to summary
private ValueEval processNameEval(ValueEval eval, OperationEvaluationContext ec)
registerFunctionback to summary
public static void registerFunction(String name, FreeRefFunction func)

Register a ATP function in runtime.

Parameters
name:String

the function name

func:FreeRefFunction

the function to register

Exceptions
IllegalArgumentException:
if the function is unknown or already registered.
Since
3.8 beta6
registerFunctionback to summary
public static void registerFunction(String name, Function func)

Register a function in runtime.

Parameters
name:String

the function name

func:Function

the function to register

Exceptions
IllegalArgumentException:
if the function is unknown or already registered.
Since
3.8 beta6
setDebugEvaluationOutputForNextEvalback to summary
public void setDebugEvaluationOutputForNextEval(boolean value)
setIgnoreMissingWorkbooksback to summary
public void setIgnoreMissingWorkbooks(boolean ignore)

Whether to ignore missing references to external workbooks and use cached formula results in the main workbook instead.

In some cases external workbooks referenced by formulas in the main workbook are not available. With this method you can control how POI handles such missing references:

  • by default ignoreMissingWorkbooks=false and POI throws WorkbookNotFoundException if an external reference cannot be resolved
  • if ignoreMissingWorkbooks=true then POI uses cached formula result that already exists in the main workbook
Parameters
ignore:boolean

whether to ignore missing references to external workbooks

See Also
Bug 52575 for details