Package org.apache.poi.ss.formula
Class FormulaParser
java.lang.Object
org.apache.poi.ss.formula.FormulaParser
This class parses a formula string into a List of tokens in RPN order.
Inspired by "Lets Build a Compiler" by Jack Crenshaw
BNF for the formula expression is :
<expression> ::= <term> [<addop> <term>]*
<term> ::= <factor> [ <mulop> <factor> ]*
<factor> ::= <number> | (<expression>) | <cellRef> | <function>
<function> ::= <functionName> ([expression [, expression]*])
For POI internal use only
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic intstatic Ptg[]parse(String formula, FormulaParsingWorkbook workbook, FormulaType formulaType, int sheetIndex) Parse a formula into an array of tokens Side effect: creates name (Workbook.createName()) if formula contains unrecognized names (names are likely UDFs)static Ptg[]parse(String formula, FormulaParsingWorkbook workbook, FormulaType formulaType, int sheetIndex, int rowIndex) Parse a formula into an array of tokens Side effect: creates name (Workbook.createName()) if formula contains unrecognized names (names are likely UDFs)static Area3DPxgparseStructuredReference(String tableText, FormulaParsingWorkbook workbook, int rowIndex) Parse a structured reference.static voidsetMaxNestingDepth(int maxNestingDepth) The parser is recursive, so deeply nested formulas could otherwise exhaust the thread stack.
-
Field Details
-
DEFAULT_MAX_NESTING_DEPTH
public static final int DEFAULT_MAX_NESTING_DEPTHThe default value forgetMaxNestingDepth(). Excel itself allows 64 levels of nested functions.- See Also:
-
-
Method Details
-
setMaxNestingDepth
public static void setMaxNestingDepth(int maxNestingDepth) The parser is recursive, so deeply nested formulas could otherwise exhaust the thread stack. Formulas with more levels of nested parentheses, function calls and unary signs than this are rejected with aFormulaParseException.- Parameters:
maxNestingDepth- the maximum nesting depth (defaults toDEFAULT_MAX_NESTING_DEPTH)- Throws:
IllegalArgumentException- ifmaxNestingDepthis negative- Since:
- 6.0.0
-
getMaxNestingDepth
public static int getMaxNestingDepth()- Returns:
- the maximum levels of nested parentheses, function calls and unary signs in a formula
- Since:
- 6.0.0
- See Also:
-
parse
public static Ptg[] parse(String formula, FormulaParsingWorkbook workbook, FormulaType formulaType, int sheetIndex, int rowIndex) Parse a formula into an array of tokens Side effect: creates name (Workbook.createName()) if formula contains unrecognized names (names are likely UDFs)- Parameters:
formula- the formula to parseworkbook- the parent workbookformulaType- the type of the formulasheetIndex- the 0-based index of the sheet this formula belongs to. The sheet index is required to resolve sheet-level names.-1means that the scope of the name will be ignored and the parser will match names only by namerowIndex- - the related cell's row index in 0-based form (-1 if the formula is not cell related) used to handle structured references that have the "#This Row" quantifier. Use rowIndex=-1 orparseStructuredReference(String, FormulaParsingWorkbook, int)if formula does not contain structured references.- Returns:
- array of parsed tokens
- Throws:
FormulaParseException- if the formula has incorrect syntax or is otherwise invalid
-
parse
public static Ptg[] parse(String formula, FormulaParsingWorkbook workbook, FormulaType formulaType, int sheetIndex) Parse a formula into an array of tokens Side effect: creates name (Workbook.createName()) if formula contains unrecognized names (names are likely UDFs)- Parameters:
formula- the formula to parseworkbook- the parent workbookformulaType- the type of the formulasheetIndex- the 0-based index of the sheet this formula belongs to. The sheet index is required to resolve sheet-level names.-1means that the scope of the name will be ignored and the parser will match names only by name- Returns:
- array of parsed tokens
- Throws:
FormulaParseException- if the formula has incorrect syntax or is otherwise invalid
-
parseStructuredReference
public static Area3DPxg parseStructuredReference(String tableText, FormulaParsingWorkbook workbook, int rowIndex) Parse a structured reference. Converts the structured reference to the area that represent it.- Parameters:
tableText- - The structured reference textworkbook- - the parent workbookrowIndex- - the 0-based cell's row index ( used to handle "#This Row" quantifiers )- Returns:
- the area that being represented by the structured reference.
-