From: Nick Burch Date: Mon, 9 May 2011 14:04:21 +0000 (+0000) Subject: As Java doesn't allow static methods on interfaces, allow HSSFFormulaEvaluator or... X-Git-Tag: REL_3_8_BETA3~39 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e94feeee12d4356cf5e46b44894032b64f6d0110;p=poi.git As Java doesn't allow static methods on interfaces, allow HSSFFormulaEvaluator or XSSFFormulaEvaluator to generically recalculate all formulas in a workbook git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1101033 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/spreadsheet/eval.xml b/src/documentation/content/xdocs/spreadsheet/eval.xml index a0c9896ca0..b19517e061 100644 --- a/src/documentation/content/xdocs/spreadsheet/eval.xml +++ b/src/documentation/content/xdocs/spreadsheet/eval.xml @@ -197,7 +197,6 @@ if (cell!=null) {
Re-calculating all formulas in a Workbook - FileInputStream fis = new FileInputStream("/somepath/test.xls"); Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls") FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); @@ -212,6 +211,11 @@ for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) { } } + +

Alternately, if you know which of HSSF or XSSF you're working + with, then you can call the static + evaluateAllFormulaCells method on the appropriate + HSSFFormulaEvaluator or XSSFFormulaEvaluator class.

diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java index 0250619c3d..86c85f02b3 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java @@ -17,21 +17,21 @@ package org.apache.poi.hssf.usermodel; -import java.util.Iterator; - +import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment; +import org.apache.poi.ss.formula.IStabilityClassifier; +import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.formula.eval.BoolEval; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.udf.UDFFinder; -import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment; -import org.apache.poi.ss.formula.IStabilityClassifier; -import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; /** * Evaluates formula cells.

@@ -46,6 +46,7 @@ import org.apache.poi.ss.usermodel.Row; public class HSSFFormulaEvaluator implements FormulaEvaluator { private WorkbookEvaluator _bookEvaluator; + private HSSFWorkbook _book; /** * @deprecated (Sep 2008) HSSFSheet parameter is ignored @@ -55,9 +56,11 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator { if (false) { sheet.toString(); // suppress unused parameter compiler warning } + this._book = workbook; } public HSSFFormulaEvaluator(HSSFWorkbook workbook) { this(workbook, null); + this._book = workbook; } /** * @param stabilityClassifier used to optimise caching performance. Pass null @@ -296,21 +299,52 @@ public class HSSFFormulaEvaluator implements FormulaEvaluator { * cells, and calling evaluateFormulaCell on each one. */ public static void evaluateAllFormulaCells(HSSFWorkbook wb) { - HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); - for(int i=0; i rit = sheet.rowIterator(); rit.hasNext();) { - Row r = rit.next(); + 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 cit = r.cellIterator(); cit.hasNext();) { - Cell c = cit.next(); - if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) - evaluator.evaluateFormulaCell(c); - } - } - } + for(Row r : sheet) { + for (Cell c : r) { + if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { + evaluator.evaluateFormulaCell(c); + } + } + } + } } + + /** + * 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 void evaluateAll() { + evaluateAllFormulaCells(_book, this); + } /** * Returns a CellValue wrapper around the supplied ValueEval instance. diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java index a1393dc485..c33fbd49f0 100644 --- a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java @@ -202,7 +202,7 @@ public final class WorkbookEvaluator { int sheetIndex = getSheetIndex(cell.getSheet()); _cache.notifyDeleteCell(_workbookIx, sheetIndex, cell); } - + private int getSheetIndex(EvaluationSheet sheet) { Integer result = _sheetIndexesBySheet.get(sheet); if (result == null) { diff --git a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java index 3d1076fdf5..adbb685ffb 100644 --- a/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java +++ b/src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java @@ -56,6 +56,16 @@ public interface FormulaEvaluator { */ void notifyUpdateCell(Cell cell); + /** + * Loops over all cells in all sheets of the associated 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. + */ + void evaluateAll(); + /** * If cell contains a formula, the formula is evaluated and returned, * else the CellValue simply copies the appropriate cell value from diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java index e114e0d8f3..104d5c97b4 100644 --- a/src/java/org/apache/poi/ss/util/SheetUtil.java +++ b/src/java/org/apache/poi/ss/util/SheetUtil.java @@ -61,6 +61,7 @@ public class SheetUtil { public void notifyUpdateCell(Cell cell) {} public CellValue evaluate(Cell cell) {return null; } public Cell evaluateInCell(Cell cell) { return null; } + public void evaluateAll() {} public int evaluateFormulaCell(Cell cell) { return cell.getCachedFormulaResultType(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java index 6d2583b90d..3f8ce85a63 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java @@ -17,21 +17,19 @@ package org.apache.poi.xssf.usermodel; -import java.util.Iterator; - +import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; +import org.apache.poi.ss.formula.IStabilityClassifier; +import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.formula.eval.BoolEval; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.udf.UDFFinder; -import org.apache.poi.ss.formula.IStabilityClassifier; -import org.apache.poi.ss.formula.WorkbookEvaluator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; /** * Evaluates formula cells.

@@ -46,6 +44,7 @@ import org.apache.poi.ss.usermodel.Sheet; public class XSSFFormulaEvaluator implements FormulaEvaluator { private WorkbookEvaluator _bookEvaluator; + private XSSFWorkbook _book; public XSSFFormulaEvaluator(XSSFWorkbook workbook) { this(workbook, null, null); @@ -59,9 +58,11 @@ public class XSSFFormulaEvaluator implements FormulaEvaluator { @Deprecated public XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier) { _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, null); + _book = workbook; } private XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) { _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder); + _book = workbook; } /** @@ -234,21 +235,22 @@ public class XSSFFormulaEvaluator implements FormulaEvaluator { * cells, and calling evaluateFormulaCell on each one. */ public static void evaluateAllFormulaCells(XSSFWorkbook wb) { - XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb); - for(int i=0; i rit = sheet.rowIterator(); rit.hasNext();) { - Row r = rit.next(); - - for (Iterator cit = r.cellIterator(); cit.hasNext();) { - XSSFCell c = (XSSFCell) cit.next(); - if (c.getCellType() == XSSFCell.CELL_TYPE_FORMULA) - evaluator.evaluateFormulaCell(c); - } - } - } + HSSFFormulaEvaluator.evaluateAllFormulaCells((Workbook)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 void evaluateAll() { + HSSFFormulaEvaluator.evaluateAllFormulaCells(_book); + } /** * Returns a CellValue wrapper around the supplied ValueEval instance.