evaluators) {
CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators);
}
-
+
@Override
public WorkbookEvaluator _getWorkbookEvaluator() {
return _bookEvaluator;
}
-
- /**
- * Should be called whenever there are major changes (e.g. moving sheets) to input cells
- * in the evaluated workbook. If performance is not critical, a single call to this method
- * may be used instead of many specific calls to the notify~ methods.
- *
- * Failure to call this method after changing cell values will cause incorrect behaviour
- * of the evaluate~ methods of this class
- */
- @Override
+
+ /**
+ * Should be called whenever there are major changes (e.g. moving sheets) to input cells
+ * in the evaluated workbook. If performance is not critical, a single call to this method
+ * may be used instead of many specific calls to the notify~ methods.
+ *
+ * Failure to call this method after changing cell values will cause incorrect behaviour
+ * of the evaluate~ methods of this class
+ */
+ @Override
public void clearAllCachedResultValues() {
- _bookEvaluator.clearAllCachedResultValues();
- }
- /**
- * Should be called to tell the cell value cache that the specified (value or formula) cell
- * has changed.
- * Failure to call this method after changing cell values will cause incorrect behaviour
- * of the evaluate~ methods of this class
- */
- public void notifyUpdateCell(HSSFCell cell) {
- _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));
- }
+ _bookEvaluator.clearAllCachedResultValues();
+ }
+ /**
+ * Should be called to tell the cell value cache that the specified (value or formula) cell
+ * has changed.
+ * Failure to call this method after changing cell values will cause incorrect behaviour
+ * of the evaluate~ methods of this class
+ */
+ public void notifyUpdateCell(HSSFCell cell) {
+ _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));
+ }
@Override
public void notifyUpdateCell(Cell cell) {
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
}
- /**
- * Should be called to tell the cell value cache that the specified cell has just been
- * deleted.
- * Failure to call this method after changing cell values will cause incorrect behaviour
- * of the evaluate~ methods of this class
- */
- public void notifyDeleteCell(HSSFCell cell) {
- _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));
- }
- @Override
+ /**
+ * Should be called to tell the cell value cache that the specified cell has just been
+ * deleted.
+ * Failure to call this method after changing cell values will cause incorrect behaviour
+ * of the evaluate~ methods of this class
+ */
+ public void notifyDeleteCell(HSSFCell cell) {
+ _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));
+ }
+ @Override
public void notifyDeleteCell(Cell cell) {
- _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));
- }
+ _bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));
+ }
- /**
- * Should be called to tell the cell value cache that the specified (value or formula) cell
- * has changed.
- * Failure to call this method after changing cell values will cause incorrect behaviour
- * of the evaluate~ methods of this class
- */
- @Override
+ /**
+ * Should be called to tell the cell value cache that the specified (value or formula) cell
+ * has changed.
+ * Failure to call this method after changing cell values will cause incorrect behaviour
+ * of the evaluate~ methods of this class
+ */
+ @Override
public void notifySetFormula(Cell cell) {
- _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
- }
+ _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
+ }
- /**
- * If cell contains a formula, the formula is evaluated and returned,
- * else the CellValue simply copies the appropriate cell value from
- * the cell and also its cell type. This method should be preferred over
- * evaluateInCell() when the call should not modify the contents of the
- * original cell.
- *
- * @param cell may be null
signifying that the cell is not present (or blank)
- * @return null
if the supplied cell is null
or blank
- */
- @Override
- public CellValue evaluate(Cell cell) {
- if (cell == null) {
- return null;
- }
+ /**
+ * If cell contains a formula, the formula is evaluated and returned,
+ * else the CellValue simply copies the appropriate cell value from
+ * the cell and also its cell type. This method should be preferred over
+ * evaluateInCell() when the call should not modify the contents of the
+ * original cell.
+ *
+ * @param cell may be null
signifying that the cell is not present (or blank)
+ * @return null
if the supplied cell is null
or blank
+ */
+ @Override
+ public CellValue evaluate(Cell cell) {
+ if (cell == null) {
+ return null;
+ }
- switch (cell.getCellTypeEnum()) {
- case BOOLEAN:
- return CellValue.valueOf(cell.getBooleanCellValue());
- case ERROR:
- return CellValue.getError(cell.getErrorCellValue());
- case FORMULA:
- return evaluateFormulaCellValue(cell);
- case NUMERIC:
- return new CellValue(cell.getNumericCellValue());
- case STRING:
- return new CellValue(cell.getRichStringCellValue().getString());
- case BLANK:
- return null;
- default:
- throw new IllegalStateException("Bad cell type (" + cell.getCellTypeEnum() + ")");
- }
-
- }
+ switch (cell.getCellTypeEnum()) {
+ case BOOLEAN:
+ return CellValue.valueOf(cell.getBooleanCellValue());
+ case ERROR:
+ return CellValue.getError(cell.getErrorCellValue());
+ case FORMULA:
+ return evaluateFormulaCellValue(cell);
+ case NUMERIC:
+ return new CellValue(cell.getNumericCellValue());
+ case STRING:
+ return new CellValue(cell.getRichStringCellValue().getString());
+ case BLANK:
+ return null;
+ default:
+ throw new IllegalStateException("Bad cell type (" + cell.getCellTypeEnum() + ")");
+ }
+ }
- /**
- * If cell contains formula, it evaluates the formula, and saves the result of the formula. The
- * cell remains as a formula cell. If the cell does not contain formula, this method returns -1
- * and leaves the cell unchanged.
- *
- * Note that the type of the formula result is returned, so you know what kind of
- * cached formula result is also stored with the formula.
- *
- * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
- *
- * Be aware that your cell will hold both the formula, and the result. If you want the cell
- * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
- * @param cell The cell to evaluate
- * @return -1 for non-formula cells, or the type of the formula result
- */
- @Override
- public int evaluateFormulaCell(Cell cell) {
- return evaluateFormulaCellEnum(cell).getCode();
- }
-
- /**
- * If cell contains formula, it evaluates the formula, and saves the result of the formula. The
- * cell remains as a formula cell. If the cell does not contain formula, rather than throwing an
- * exception, this method returns {@link CellType#_NONE} and leaves the cell unchanged.
- *
- * Note that the type of the formula result is returned, so you know what kind of
- * cached formula result is also stored with the formula.
- *
- * CellType evaluatedCellType = evaluator.evaluateFormulaCell(cell);
- *
- * Be aware that your cell will hold both the formula, and the result. If you want the cell
- * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
- * @param cell The cell to evaluate
- * @return {@link CellType#_NONE} for non-formula cells, or the type of the formula result
- * @since POI 3.15 beta 3
- * @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
- */
- @Internal
- @Override
- public CellType evaluateFormulaCellEnum(Cell cell) {
- if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
- return CellType._NONE;
- }
- CellValue cv = evaluateFormulaCellValue(cell);
- // cell remains a formula cell, but the cached value is changed
- setCellValue(cell, cv);
- return cv.getCellType();
- }
+ /**
+ * If cell contains formula, it evaluates the formula, and saves the result of the formula. The
+ * cell remains as a formula cell. If the cell does not contain formula, this method returns -1
+ * and leaves the cell unchanged.
+ *
+ * Note that the type of the formula result is returned, so you know what kind of
+ * cached formula result is also stored with the formula.
+ *
+ * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
+ *
+ * Be aware that your cell will hold both the formula, and the result. If you want the cell
+ * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
+ * @param cell The cell to evaluate
+ * @return -1 for non-formula cells, or the type of the formula result
+ */
+ @Override
+ public int evaluateFormulaCell(Cell cell) {
+ return evaluateFormulaCellEnum(cell).getCode();
+ }
- /**
- * If cell contains formula, it evaluates the formula, and
- * puts the formula result back into the cell, in place
- * of the old formula.
- * Else if cell does not contain formula, this method leaves
- * the cell unchanged.
- * Note that the same instance of HSSFCell is returned to
- * allow chained calls like:
- *
- * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
- *
- * Be aware that your cell value will be changed to hold the
- * result of the formula. If you simply want the formula
- * value computed for you, use {@link #evaluateFormulaCellEnum(Cell)}}
- */
- @Override
- public HSSFCell evaluateInCell(Cell cell) {
- if (cell == null) {
- return null;
- }
- HSSFCell result = (HSSFCell) cell;
- if (cell.getCellTypeEnum() == CellType.FORMULA) {
- CellValue cv = evaluateFormulaCellValue(cell);
- setCellValue(cell, cv);
- setCellType(cell, cv); // cell will no longer be a formula cell
- }
- return result;
- }
- private static void setCellType(Cell cell, CellValue cv) {
- CellType cellType = cv.getCellType();
- switch (cellType) {
- case BOOLEAN:
- case ERROR:
- case NUMERIC:
- case STRING:
- cell.setCellType(cellType);
- return;
- case BLANK:
- // never happens - blanks eventually get translated to zero
- case FORMULA:
- // this will never happen, we have already evaluated the formula
- default:
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
- }
-
- }
+ /**
+ * If cell contains formula, it evaluates the formula, and saves the result of the formula. The
+ * cell remains as a formula cell. If the cell does not contain formula, rather than throwing an
+ * exception, this method returns {@link CellType#_NONE} and leaves the cell unchanged.
+ *
+ * Note that the type of the formula result is returned, so you know what kind of
+ * cached formula result is also stored with the formula.
+ *
+ * CellType evaluatedCellType = evaluator.evaluateFormulaCell(cell);
+ *
+ * Be aware that your cell will hold both the formula, and the result. If you want the cell
+ * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
+ * @param cell The cell to evaluate
+ * @return {@link CellType#_NONE} for non-formula cells, or the type of the formula result
+ * @since POI 3.15 beta 3
+ * @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
+ */
+ @Internal
+ @Override
+ public CellType evaluateFormulaCellEnum(Cell cell) {
+ if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
+ return CellType._NONE;
+ }
+ CellValue cv = evaluateFormulaCellValue(cell);
+ // cell remains a formula cell, but the cached value is changed
+ setCellValue(cell, cv);
+ return cv.getCellType();
+ }
- private static void setCellValue(Cell cell, CellValue cv) {
- CellType cellType = cv.getCellType();
- switch (cellType) {
- case BOOLEAN:
- cell.setCellValue(cv.getBooleanValue());
- break;
- case ERROR:
- cell.setCellErrorValue(cv.getErrorValue());
- break;
- case NUMERIC:
- cell.setCellValue(cv.getNumberValue());
- break;
- case STRING:
- cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
- break;
- case BLANK:
- // never happens - blanks eventually get translated to zero
- case FORMULA:
- // this will never happen, we have already evaluated the formula
- default:
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
- }
- }
+ /**
+ * If cell contains formula, it evaluates the formula, and
+ * puts the formula result back into the cell, in place
+ * of the old formula.
+ * Else if cell does not contain formula, this method leaves
+ * the cell unchanged.
+ * Note that the same instance of HSSFCell is returned to
+ * allow chained calls like:
+ *
+ * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
+ *
+ * Be aware that your cell value will be changed to hold the
+ * result of the formula. If you simply want the formula
+ * value computed for you, use {@link #evaluateFormulaCellEnum(Cell)}}
+ */
+ @Override
+ public HSSFCell evaluateInCell(Cell cell) {
+ if (cell == null) {
+ return null;
+ }
+ HSSFCell result = (HSSFCell) cell;
+ if (cell.getCellTypeEnum() == CellType.FORMULA) {
+ CellValue cv = evaluateFormulaCellValue(cell);
+ setCellValue(cell, cv);
+ setCellType(cell, cv); // cell will no longer be a formula cell
+ }
+ return result;
+ }
+ private static void setCellType(Cell cell, CellValue cv) {
+ CellType cellType = cv.getCellType();
+ switch (cellType) {
+ case BOOLEAN:
+ case ERROR:
+ case NUMERIC:
+ case STRING:
+ cell.setCellType(cellType);
+ return;
+ case BLANK:
+ // never happens - blanks eventually get translated to zero
+ case FORMULA:
+ // this will never happen, we have already evaluated the formula
+ default:
+ throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
+ }
+ }
- /**
- * Loops over all cells in all sheets of the supplied
- * workbook.
- * For cells that contain formulas, their formulas are
- * evaluated, and the results are saved. These cells
- * remain as formula cells.
- * For cells that do not contain formulas, no changes
- * are made.
- * This is a helpful wrapper around looping over all
- * cells, and calling evaluateFormulaCell on each one.
- */
- public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
- evaluateAllFormulaCells(wb, new HSSFFormulaEvaluator(wb));
- }
-
- /**
- * Loops over all cells in all sheets of the supplied
- * workbook.
- * For cells that contain formulas, their formulas are
- * evaluated, and the results are saved. These cells
- * remain as formula cells.
- * For cells that do not contain formulas, no changes
- * are made.
- * This is a helpful wrapper around looping over all
- * cells, and calling evaluateFormulaCell on each one.
- */
- public static void evaluateAllFormulaCells(Workbook wb) {
- FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
- evaluateAllFormulaCells(wb, evaluator);
- }
- private static void evaluateAllFormulaCells(Workbook wb, FormulaEvaluator evaluator) {
- for(int i=0; i