]> source.dussan.org Git - poi.git/commitdiff
As Java doesn't allow static methods on interfaces, allow HSSFFormulaEvaluator or...
authorNick Burch <nick@apache.org>
Mon, 9 May 2011 14:04:21 +0000 (14:04 +0000)
committerNick Burch <nick@apache.org>
Mon, 9 May 2011 14:04:21 +0000 (14:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1101033 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/spreadsheet/eval.xml
src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
src/java/org/apache/poi/ss/util/SheetUtil.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java

index a0c9896ca093da15d1000f40078c58fa6fe2d0ad..b19517e061d3ffe7bcac29d94f0c7adf8f49becd 100644 (file)
@@ -197,7 +197,6 @@ if (cell!=null) {
                        <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();
@@ -212,6 +211,11 @@ for(int sheetNum = 0; sheetNum &lt; wb.getNumberOfSheets(); sheetNum++) {
     }
 }
         </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>
                
index 0250619c3dc4d7a28d4692023841508a9f708675..86c85f02b372df86d94b0697648e5a9efc5aa0b9 100644 (file)
 
 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/>
@@ -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 <code>null</code>
@@ -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<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.
index a1393dc4852dba385196c71c4af4b23559bd981c..c33fbd49f05378cd99851149a2be371c079af35d 100644 (file)
@@ -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) {
index 3d1076fdf5501e207be444690cca17333b446b39..adbb685ffbd60a699b6050692f3eb32c3e830ba7 100644 (file)
@@ -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
index e114e0d8f3e6310a8835041fc4b7094f71a358e1..104d5c97b466afe834a0e9de5ee6011efeef37f1 100644 (file)
@@ -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();
index 6d2583b90d9e3d223121948450332fa074b7506c..3f8ce85a633944f985fac80b001d763b684253da 100644 (file)
 
 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/>
@@ -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<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.