<anchor id="EvaluateAll"/>
<section><title>Re-calculating all formulas in a Workbook</title>
<source>
-
FileInputStream fis = new FileInputStream("/somepath/test.xls");
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
}
}
</source>
+
+ <p>Alternately, if you know which of HSSF or XSSF you're working
+ with, then you can call the static
+ <strong>evaluateAllFormulaCells</strong> method on the appropriate
+ HSSFFormulaEvaluator or XSSFFormulaEvaluator class.</p>
</section>
</section>
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.<p/>
public class HSSFFormulaEvaluator implements FormulaEvaluator {
private WorkbookEvaluator _bookEvaluator;
+ private HSSFWorkbook _book;
/**
* @deprecated (Sep 2008) HSSFSheet parameter is ignored
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 <code>null</code>
* cells, and calling evaluateFormulaCell on each one.
*/
public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
- HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
- for(int i=0; i<wb.getNumberOfSheets(); i++) {
- HSSFSheet sheet = wb.getSheetAt(i);
-
- for (Iterator<Row> 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<wb.getNumberOfSheets(); i++) {
+ Sheet sheet = wb.getSheetAt(i);
- for (Iterator<Cell> 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.
int sheetIndex = getSheetIndex(cell.getSheet());
_cache.notifyDeleteCell(_workbookIx, sheetIndex, cell);
}
-
+
private int getSheetIndex(EvaluationSheet sheet) {
Integer result = _sheetIndexesBySheet.get(sheet);
if (result == null) {
*/
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
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();
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.<p/>
public class XSSFFormulaEvaluator implements FormulaEvaluator {
private WorkbookEvaluator _bookEvaluator;
+ private XSSFWorkbook _book;
public XSSFFormulaEvaluator(XSSFWorkbook workbook) {
this(workbook, null, null);
@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;
}
/**
* cells, and calling evaluateFormulaCell on each one.
*/
public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
- XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb);
- for(int i=0; i<wb.getNumberOfSheets(); i++) {
- Sheet sheet = wb.getSheetAt(i);
-
- for (Iterator<Row> 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.