import org.apache.poi.hssf.record.formula.eval.StringEval;\r
import org.apache.poi.hssf.record.formula.eval.ValueEval;\r
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;\r
+import org.apache.poi.ss.formula.IStabilityClassifier;\r
import org.apache.poi.ss.formula.WorkbookEvaluator;\r
import org.apache.poi.ss.usermodel.Cell;\r
import org.apache.poi.ss.usermodel.CellValue;\r
*/\r
public class HSSFFormulaEvaluator implements FormulaEvaluator {\r
\r
- private WorkbookEvaluator _bookEvaluator;\r
+ private WorkbookEvaluator _bookEvaluator;\r
\r
- /**\r
- * @deprecated (Sep 2008) HSSFSheet parameter is ignored\r
- */\r
- public HSSFFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook) {\r
- this(workbook);\r
- if (false) {\r
- sheet.toString(); // suppress unused parameter compiler warning\r
- }\r
- }\r
- public HSSFFormulaEvaluator(HSSFWorkbook workbook) {\r
- _bookEvaluator = new WorkbookEvaluator(HSSFEvaluationWorkbook.create(workbook));\r
- }\r
+ /**\r
+ * @deprecated (Sep 2008) HSSFSheet parameter is ignored\r
+ */\r
+ public HSSFFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook) {\r
+ this(workbook);\r
+ if (false) {\r
+ sheet.toString(); // suppress unused parameter compiler warning\r
+ }\r
+ }\r
+ public HSSFFormulaEvaluator(HSSFWorkbook workbook) {\r
+ this(workbook, null);\r
+ }\r
+ /**\r
+ * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>\r
+ * for the (conservative) assumption that any cell may have its definition changed after \r
+ * evaluation begins.\r
+ */\r
+ public HSSFFormulaEvaluator(HSSFWorkbook workbook, IStabilityClassifier stabilityClassifier) {\r
+ _bookEvaluator = new WorkbookEvaluator(HSSFEvaluationWorkbook.create(workbook), stabilityClassifier);\r
+ }\r
\r
- /**\r
- * Coordinates several formula evaluators together so that formulas that involve external\r
- * references can be evaluated.\r
- * @param workbookNames the simple file names used to identify the workbooks in formulas\r
- * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")\r
- * @param evaluators all evaluators for the full set of workbooks required by the formulas.\r
- */\r
- public static void setupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators) {\r
- WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];\r
- for (int i = 0; i < wbEvals.length; i++) {\r
- wbEvals[i] = evaluators[i]._bookEvaluator;\r
- }\r
- CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);\r
- }\r
+ /**\r
+ * Coordinates several formula evaluators together so that formulas that involve external\r
+ * references can be evaluated.\r
+ * @param workbookNames the simple file names used to identify the workbooks in formulas\r
+ * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")\r
+ * @param evaluators all evaluators for the full set of workbooks required by the formulas.\r
+ */\r
+ public static void setupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators) {\r
+ WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];\r
+ for (int i = 0; i < wbEvals.length; i++) {\r
+ wbEvals[i] = evaluators[i]._bookEvaluator;\r
+ }\r
+ CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);\r
+ }\r
\r
- /**\r
- * Does nothing\r
- * @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell\r
- */\r
- public void setCurrentRow(HSSFRow row) {\r
- // do nothing\r
- if (false) {\r
- row.getClass(); // suppress unused parameter compiler warning\r
- }\r
- }\r
+ /**\r
+ * Does nothing\r
+ * @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell\r
+ */\r
+ public void setCurrentRow(HSSFRow row) {\r
+ // do nothing\r
+ if (false) {\r
+ row.getClass(); // suppress unused parameter compiler warning\r
+ }\r
+ }\r
\r
- /**\r
- * Should be called whenever there are major changes (e.g. moving sheets) to input cells\r
- * in the evaluated workbook. If performance is not critical, a single call to this method\r
- * may be used instead of many specific calls to the notify~ methods.\r
- *\r
- * Failure to call this method after changing cell values will cause incorrect behaviour\r
- * of the evaluate~ methods of this class\r
- */\r
- public void clearAllCachedResultValues() {\r
- _bookEvaluator.clearAllCachedResultValues();\r
- }\r
- /**\r
- * Should be called to tell the cell value cache that the specified (value or formula) cell\r
- * has changed.\r
- * Failure to call this method after changing cell values will cause incorrect behaviour\r
- * of the evaluate~ methods of this class\r
- */\r
- public void notifyUpdateCell(HSSFCell cell) {\r
- _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));\r
- }\r
- /**\r
- * Should be called to tell the cell value cache that the specified cell has just been\r
- * deleted.\r
- * Failure to call this method after changing cell values will cause incorrect behaviour\r
- * of the evaluate~ methods of this class\r
- */\r
- public void notifyDeleteCell(HSSFCell cell) {\r
- _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));\r
- }\r
- public void notifyDeleteCell(Cell cell) {\r
- _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));\r
- }\r
+ /**\r
+ * Should be called whenever there are major changes (e.g. moving sheets) to input cells\r
+ * in the evaluated workbook. If performance is not critical, a single call to this method\r
+ * may be used instead of many specific calls to the notify~ methods.\r
+ *\r
+ * Failure to call this method after changing cell values will cause incorrect behaviour\r
+ * of the evaluate~ methods of this class\r
+ */\r
+ public void clearAllCachedResultValues() {\r
+ _bookEvaluator.clearAllCachedResultValues();\r
+ }\r
+ /**\r
+ * Should be called to tell the cell value cache that the specified (value or formula) cell\r
+ * has changed.\r
+ * Failure to call this method after changing cell values will cause incorrect behaviour\r
+ * of the evaluate~ methods of this class\r
+ */\r
+ public void notifyUpdateCell(HSSFCell cell) {\r
+ _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));\r
+ }\r
+ /**\r
+ * Should be called to tell the cell value cache that the specified cell has just been\r
+ * deleted.\r
+ * Failure to call this method after changing cell values will cause incorrect behaviour\r
+ * of the evaluate~ methods of this class\r
+ */\r
+ public void notifyDeleteCell(HSSFCell cell) {\r
+ _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));\r
+ }\r
+ public void notifyDeleteCell(Cell cell) {\r
+ _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));\r
+ }\r
\r
- /**\r
- * Should be called to tell the cell value cache that the specified (value or formula) cell\r
- * has changed.\r
- * Failure to call this method after changing cell values will cause incorrect behaviour\r
- * of the evaluate~ methods of this class\r
- */\r
- public void notifySetFormula(Cell cell) {\r
- _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));\r
- }\r
+ /**\r
+ * Should be called to tell the cell value cache that the specified (value or formula) cell\r
+ * has changed.\r
+ * Failure to call this method after changing cell values will cause incorrect behaviour\r
+ * of the evaluate~ methods of this class\r
+ */\r
+ public void notifySetFormula(Cell cell) {\r
+ _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));\r
+ }\r
\r
- /**\r
- * If cell contains a formula, the formula is evaluated and returned,\r
- * else the CellValue simply copies the appropriate cell value from\r
- * the cell and also its cell type. This method should be preferred over\r
- * evaluateInCell() when the call should not modify the contents of the\r
- * original cell.\r
- *\r
- * @param cell may be <code>null</code> signifying that the cell is not present (or blank)\r
- * @return <code>null</code> if the supplied cell is <code>null</code> or blank\r
- */\r
- public CellValue evaluate(Cell cell) {\r
- if (cell == null) {\r
- return null;\r
- }\r
+ /**\r
+ * If cell contains a formula, the formula is evaluated and returned,\r
+ * else the CellValue simply copies the appropriate cell value from\r
+ * the cell and also its cell type. This method should be preferred over\r
+ * evaluateInCell() when the call should not modify the contents of the\r
+ * original cell.\r
+ *\r
+ * @param cell may be <code>null</code> signifying that the cell is not present (or blank)\r
+ * @return <code>null</code> if the supplied cell is <code>null</code> or blank\r
+ */\r
+ public CellValue evaluate(Cell cell) {\r
+ if (cell == null) {\r
+ return null;\r
+ }\r
\r
- switch (cell.getCellType()) {\r
- case HSSFCell.CELL_TYPE_BOOLEAN:\r
- return CellValue.valueOf(cell.getBooleanCellValue());\r
- case HSSFCell.CELL_TYPE_ERROR:\r
- return CellValue.getError(cell.getErrorCellValue());\r
- case HSSFCell.CELL_TYPE_FORMULA:\r
- return evaluateFormulaCellValue(cell);\r
- case HSSFCell.CELL_TYPE_NUMERIC:\r
- return new CellValue(cell.getNumericCellValue());\r
- case HSSFCell.CELL_TYPE_STRING:\r
- return new CellValue(cell.getRichStringCellValue().getString());\r
- case HSSFCell.CELL_TYPE_BLANK:\r
- return null;\r
- }\r
- throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");\r
- }\r
+ switch (cell.getCellType()) {\r
+ case HSSFCell.CELL_TYPE_BOOLEAN:\r
+ return CellValue.valueOf(cell.getBooleanCellValue());\r
+ case HSSFCell.CELL_TYPE_ERROR:\r
+ return CellValue.getError(cell.getErrorCellValue());\r
+ case HSSFCell.CELL_TYPE_FORMULA:\r
+ return evaluateFormulaCellValue(cell);\r
+ case HSSFCell.CELL_TYPE_NUMERIC:\r
+ return new CellValue(cell.getNumericCellValue());\r
+ case HSSFCell.CELL_TYPE_STRING:\r
+ return new CellValue(cell.getRichStringCellValue().getString());\r
+ case HSSFCell.CELL_TYPE_BLANK:\r
+ return null;\r
+ }\r
+ throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");\r
+ }\r
\r
\r
- /**\r
- * If cell contains formula, it evaluates the formula, and saves the result of the formula. The\r
- * cell remains as a formula cell. If the cell does not contain formula, this method returns -1\r
- * and leaves the cell unchanged.\r
- *\r
- * Note that the type of the <em>formula result</em> is returned, so you know what kind of\r
- * cached formula result is also stored with the formula.\r
- * <pre>\r
- * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);\r
- * </pre>\r
- * Be aware that your cell will hold both the formula, and the result. If you want the cell\r
- * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}\r
- * @param cell The cell to evaluate\r
- * @return -1 for non-formula cells, or the type of the <em>formula result</em>\r
- */\r
- public int evaluateFormulaCell(Cell cell) {\r
- if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {\r
- return -1;\r
- }\r
- CellValue cv = evaluateFormulaCellValue(cell);\r
- // cell remains a formula cell, but the cached value is changed\r
- setCellValue(cell, cv);\r
- return cv.getCellType();\r
- }\r
+ /**\r
+ * If cell contains formula, it evaluates the formula, and saves the result of the formula. The\r
+ * cell remains as a formula cell. If the cell does not contain formula, this method returns -1\r
+ * and leaves the cell unchanged.\r
+ *\r
+ * Note that the type of the <em>formula result</em> is returned, so you know what kind of\r
+ * cached formula result is also stored with the formula.\r
+ * <pre>\r
+ * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);\r
+ * </pre>\r
+ * Be aware that your cell will hold both the formula, and the result. If you want the cell\r
+ * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}\r
+ * @param cell The cell to evaluate\r
+ * @return -1 for non-formula cells, or the type of the <em>formula result</em>\r
+ */\r
+ public int evaluateFormulaCell(Cell cell) {\r
+ if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {\r
+ return -1;\r
+ }\r
+ CellValue cv = evaluateFormulaCellValue(cell);\r
+ // cell remains a formula cell, but the cached value is changed\r
+ setCellValue(cell, cv);\r
+ return cv.getCellType();\r
+ }\r
\r
- /**\r
- * If cell contains formula, it evaluates the formula, and\r
- * puts the formula result back into the cell, in place\r
- * of the old formula.\r
- * Else if cell does not contain formula, this method leaves\r
- * the cell unchanged.\r
- * Note that the same instance of HSSFCell is returned to\r
- * allow chained calls like:\r
- * <pre>\r
- * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();\r
- * </pre>\r
- * Be aware that your cell value will be changed to hold the\r
- * result of the formula. If you simply want the formula\r
- * value computed for you, use {@link #evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)}}\r
- * @param cell\r
- */\r
- public HSSFCell evaluateInCell(Cell cell) {\r
- if (cell == null) {\r
- return null;\r
- }\r
- HSSFCell result = (HSSFCell) cell;\r
- if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {\r
- CellValue cv = evaluateFormulaCellValue(cell);\r
- setCellType(cell, cv); // cell will no longer be a formula cell\r
- setCellValue(cell, cv);\r
- }\r
- return result;\r
- }\r
- private static void setCellType(Cell cell, CellValue cv) {\r
- int cellType = cv.getCellType();\r
- switch (cellType) {\r
- case HSSFCell.CELL_TYPE_BOOLEAN:\r
- case HSSFCell.CELL_TYPE_ERROR:\r
- case HSSFCell.CELL_TYPE_NUMERIC:\r
- case HSSFCell.CELL_TYPE_STRING:\r
- cell.setCellType(cellType);\r
- return;\r
- case HSSFCell.CELL_TYPE_BLANK:\r
- // never happens - blanks eventually get translated to zero\r
- case HSSFCell.CELL_TYPE_FORMULA:\r
- // this will never happen, we have already evaluated the formula\r
- }\r
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");\r
- }\r
+ /**\r
+ * If cell contains formula, it evaluates the formula, and\r
+ * puts the formula result back into the cell, in place\r
+ * of the old formula.\r
+ * Else if cell does not contain formula, this method leaves\r
+ * the cell unchanged.\r
+ * Note that the same instance of HSSFCell is returned to\r
+ * allow chained calls like:\r
+ * <pre>\r
+ * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();\r
+ * </pre>\r
+ * Be aware that your cell value will be changed to hold the\r
+ * result of the formula. If you simply want the formula\r
+ * value computed for you, use {@link #evaluateFormulaCell(Cell)}}\r
+ */\r
+ public HSSFCell evaluateInCell(Cell cell) {\r
+ if (cell == null) {\r
+ return null;\r
+ }\r
+ HSSFCell result = (HSSFCell) cell;\r
+ if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {\r
+ CellValue cv = evaluateFormulaCellValue(cell);\r
+ setCellType(cell, cv); // cell will no longer be a formula cell\r
+ setCellValue(cell, cv);\r
+ }\r
+ return result;\r
+ }\r
+ private static void setCellType(Cell cell, CellValue cv) {\r
+ int cellType = cv.getCellType();\r
+ switch (cellType) {\r
+ case HSSFCell.CELL_TYPE_BOOLEAN:\r
+ case HSSFCell.CELL_TYPE_ERROR:\r
+ case HSSFCell.CELL_TYPE_NUMERIC:\r
+ case HSSFCell.CELL_TYPE_STRING:\r
+ cell.setCellType(cellType);\r
+ return;\r
+ case HSSFCell.CELL_TYPE_BLANK:\r
+ // never happens - blanks eventually get translated to zero\r
+ case HSSFCell.CELL_TYPE_FORMULA:\r
+ // this will never happen, we have already evaluated the formula\r
+ }\r
+ throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");\r
+ }\r
\r
- private static void setCellValue(Cell cell, CellValue cv) {\r
- int cellType = cv.getCellType();\r
- switch (cellType) {\r
- case HSSFCell.CELL_TYPE_BOOLEAN:\r
- cell.setCellValue(cv.getBooleanValue());\r
- break;\r
- case HSSFCell.CELL_TYPE_ERROR:\r
- cell.setCellErrorValue(cv.getErrorValue());\r
- break;\r
- case HSSFCell.CELL_TYPE_NUMERIC:\r
- cell.setCellValue(cv.getNumberValue());\r
- break;\r
- case HSSFCell.CELL_TYPE_STRING:\r
- cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));\r
- break;\r
- case HSSFCell.CELL_TYPE_BLANK:\r
- // never happens - blanks eventually get translated to zero\r
- case HSSFCell.CELL_TYPE_FORMULA:\r
- // this will never happen, we have already evaluated the formula\r
- default:\r
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");\r
- }\r
- }\r
+ private static void setCellValue(Cell cell, CellValue cv) {\r
+ int cellType = cv.getCellType();\r
+ switch (cellType) {\r
+ case HSSFCell.CELL_TYPE_BOOLEAN:\r
+ cell.setCellValue(cv.getBooleanValue());\r
+ break;\r
+ case HSSFCell.CELL_TYPE_ERROR:\r
+ cell.setCellErrorValue(cv.getErrorValue());\r
+ break;\r
+ case HSSFCell.CELL_TYPE_NUMERIC:\r
+ cell.setCellValue(cv.getNumberValue());\r
+ break;\r
+ case HSSFCell.CELL_TYPE_STRING:\r
+ cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));\r
+ break;\r
+ case HSSFCell.CELL_TYPE_BLANK:\r
+ // never happens - blanks eventually get translated to zero\r
+ case HSSFCell.CELL_TYPE_FORMULA:\r
+ // this will never happen, we have already evaluated the formula\r
+ default:\r
+ throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");\r
+ }\r
+ }\r
\r
- /**\r
- * Loops over all cells in all sheets of the supplied\r
- * workbook.\r
- * For cells that contain formulas, their formulas are\r
- * evaluated, and the results are saved. These cells\r
- * remain as formula cells.\r
- * For cells that do not contain formulas, no changes\r
- * are made.\r
- * This is a helpful wrapper around looping over all\r
- * cells, and calling evaluateFormulaCell on each one.\r
- */\r
- public static void evaluateAllFormulaCells(HSSFWorkbook wb) {\r
- HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);\r
- for(int i=0; i<wb.getNumberOfSheets(); i++) {\r
- HSSFSheet sheet = wb.getSheetAt(i);\r
+ /**\r
+ * Loops over all cells in all sheets of the supplied\r
+ * workbook.\r
+ * For cells that contain formulas, their formulas are\r
+ * evaluated, and the results are saved. These cells\r
+ * remain as formula cells.\r
+ * For cells that do not contain formulas, no changes\r
+ * are made.\r
+ * This is a helpful wrapper around looping over all\r
+ * cells, and calling evaluateFormulaCell on each one.\r
+ */\r
+ public static void evaluateAllFormulaCells(HSSFWorkbook wb) {\r
+ HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);\r
+ for(int i=0; i<wb.getNumberOfSheets(); i++) {\r
+ HSSFSheet sheet = wb.getSheetAt(i);\r
\r
- for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {\r
- HSSFRow r = (HSSFRow)rit.next();\r
+ for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {\r
+ HSSFRow r = (HSSFRow)rit.next();\r
\r
- for (Iterator cit = r.cellIterator(); cit.hasNext();) {\r
- HSSFCell c = (HSSFCell)cit.next();\r
- if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)\r
- evaluator.evaluateFormulaCell(c);\r
- }\r
- }\r
- }\r
- }\r
+ for (Iterator cit = r.cellIterator(); cit.hasNext();) {\r
+ HSSFCell c = (HSSFCell)cit.next();\r
+ if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)\r
+ evaluator.evaluateFormulaCell(c);\r
+ }\r
+ }\r
+ }\r
+ }\r
\r
- /**\r
- * Returns a CellValue wrapper around the supplied ValueEval instance.\r
- * @param eval\r
- */\r
- private CellValue evaluateFormulaCellValue(Cell cell) {\r
- ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));\r
- if (eval instanceof NumberEval) {\r
- NumberEval ne = (NumberEval) eval;\r
- return new CellValue(ne.getNumberValue());\r
- }\r
- if (eval instanceof BoolEval) {\r
- BoolEval be = (BoolEval) eval;\r
- return CellValue.valueOf(be.getBooleanValue());\r
- }\r
- if (eval instanceof StringEval) {\r
- StringEval ne = (StringEval) eval;\r
- return new CellValue(ne.getStringValue());\r
- }\r
- if (eval instanceof ErrorEval) {\r
- return CellValue.getError(((ErrorEval)eval).getErrorCode());\r
- }\r
- throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");\r
- }\r
+ /**\r
+ * Returns a CellValue wrapper around the supplied ValueEval instance.\r
+ * @param eval\r
+ */\r
+ private CellValue evaluateFormulaCellValue(Cell cell) {\r
+ ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));\r
+ if (eval instanceof NumberEval) {\r
+ NumberEval ne = (NumberEval) eval;\r
+ return new CellValue(ne.getNumberValue());\r
+ }\r
+ if (eval instanceof BoolEval) {\r
+ BoolEval be = (BoolEval) eval;\r
+ return CellValue.valueOf(be.getBooleanValue());\r
+ }\r
+ if (eval instanceof StringEval) {\r
+ StringEval ne = (StringEval) eval;\r
+ return new CellValue(ne.getStringValue());\r
+ }\r
+ if (eval instanceof ErrorEval) {\r
+ return CellValue.getError(((ErrorEval)eval).getErrorCode());\r
+ }\r
+ throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");\r
+ }\r
}\r