]> source.dussan.org Git - poi.git/commitdiff
bug# 45973: added factory method for FormulaEvaluator, also fixed unpaired tags in...
authorYegor Kozlov <yegor@apache.org>
Tue, 11 Nov 2008 11:43:20 +0000 (11:43 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 11 Nov 2008 11:43:20 +0000 (11:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713021 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/spreadsheet/eval.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/record/formula/functions/Countif.java
src/java/org/apache/poi/hssf/record/formula/functions/Errortype.java
src/java/org/apache/poi/hssf/record/formula/functions/Index.java
src/java/org/apache/poi/hssf/record/formula/functions/Sumproduct.java
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/java/org/apache/poi/hssf/usermodel/HSSFCreationHelper.java
src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CreationHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCreationHelper.java

index 42497beb12930426fc1980dbe0c77328b9c0d95b..4b1b5566d0c44d120c00e2f0db4f58325b7edffd 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.5-beta4" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">45973 - added CreationHelper.createFormulaEvaluator(), implemeted both for HSSF and XSSF</action>
            <action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
            <action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
            <action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
index 7f31017edd816225f9f6576611fe3fe40f301f2b..a0c9896ca093da15d1000f40078c58fa6fe2d0ad 100644 (file)
                                formulas in Excels sheets read-in, or created in POI. This document explains
                                how to use the API to evaluate your formulas. 
                        </p>
-                       <note>In versions of POI before 3.0.3, this code lived in the
-                               scratchpad area of the POI SVN repository. If using an such an older
-                               version of POI, ensure that you have the scratchpad jar or the 
-                               scratchpad build area in your classpath before experimenting with this 
+                       <note>.xlsx format is suported since POI 3.5, make sure yoy upgraded to that version before experimenting with this 
                                code. Users of all versions of POI may wish to make use of a recent 
                                SVN checkout, as new functions are currently being added fairly frequently.
                        </note>
@@ -47,7 +44,8 @@
                                It also provides implementations for approx. 100 built in 
                                functions in Excel. The framework however makes is easy to add 
                                implementation of new functions. See the <link href="eval-devguide.html"> Formula
-                               evaluation development guide</link> for details. </p>
+        evaluation development guide</link>  and <link href="../apidocs/org/apache/poi/hssf/record/formula/functions/package-summary.html">javadocs</link> 
+        for details. </p>
             <p> Both HSSFWorkbook and XSSFWorkbook are supported, so you can 
                 evaluate formulas on both .xls and .xlsx files.</p>
                        <p> Note that user-defined functions are not supported, and is not likely to done
                                without affecting the cell</p>
                                <source>
 FileInputStream fis = new FileInputStream("c:/temp/test.xls");
-Workbook wb = new HSSFWorkbook(fis);
+Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("c:/temp/test.xls")
 Sheet sheet = wb.getSheetAt(0);
-FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
+FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 
 // suppose your formula is in B3
 CellReference cellReference = new CellReference("B3"); 
 Row row = sheet.getRow(cellReference.getRow());
 Cell cell = row.getCell(cellReference.getCol()); 
 
-evaluator.setCurrentRow(row);
-FormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
+CellValue cellValue = evaluator.evaluate(cell);
 
 switch (cellValue.getCellType()) {
-       case Cell.CELL_TYPE_BOOLEAN:
-       System.out.println(cellValue.getBooleanValue());
-       break;
-       case Cell.CELL_TYPE_NUMERIC:
-       System.out.println(cellValue.getNumberValue());
-       break;
-       case Cell.CELL_TYPE_STRING:
-       System.out.println(cellValue.getStringValue());
-       break;
-       case Cell.CELL_TYPE_BLANK:
-       break;
-       case Cell.CELL_TYPE_ERROR:
-       break;
-
-       // CELL_TYPE_FORMULA will never happen
-       case Cell.CELL_TYPE_FORMULA: 
-       break;
+    case Cell.CELL_TYPE_BOOLEAN:
+        System.out.println(cellValue.getBooleanValue());
+        break;
+    case Cell.CELL_TYPE_NUMERIC:
+        System.out.println(cellValue.getNumberValue());
+        break;
+    case Cell.CELL_TYPE_STRING:
+        System.out.println(cellValue.getStringValue());
+        break;
+    case Cell.CELL_TYPE_BLANK:
+        break;
+    case Cell.CELL_TYPE_ERROR:
+        break;
+
+    // CELL_TYPE_FORMULA will never happen
+    case Cell.CELL_TYPE_FORMULA: 
+        break;
 }                              
-                               </source>
+        </source>
                                <p>Thus using the retrieved value (of type 
                                        FormulaEvaluator.CellValue - a nested class) returned 
                                        by FormulaEvaluator is similar to using a Cell object 
@@ -117,39 +114,38 @@ switch (cellValue.getCellType()) {
                                formula remains in the cell, just with a new value</p>
                                <p>The return of the function is the type of the
                                formula result, such as Cell.CELL_TYPE_BOOLEAN</p>
-                               <source>
+        <source>
 FileInputStream fis = new FileInputStream("/somepath/test.xls");
-Workbook wb = new HSSFWorkbook(fis);
+Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
 Sheet sheet = wb.getSheetAt(0);
-FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
+FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 
 // suppose your formula is in B3
 CellReference cellReference = new CellReference("B3"); 
 Row row = sheet.getRow(cellReference.getRow());
 Cell cell = row.getCell(cellReference.getCol()); 
-evaluator.setCurrentRow(row);
 
 if (cell!=null) {
-       switch (<strong>evaluator.evaluateFormulaCell</strong>(cell)) {
-               case Cell.CELL_TYPE_BOOLEAN:
-                   System.out.println(cell.getBooleanCellValue());
-                   break;
-               case Cell.CELL_TYPE_NUMERIC:
-                   System.out.println(cell.getNumberCellValue());
-                   break;
-               case Cell.CELL_TYPE_STRING:
-                   System.out.println(cell.getStringCellValue());
-                   break;
-               case Cell.CELL_TYPE_BLANK:
-                   break;
-               case Cell.CELL_TYPE_ERROR:
-                   System.out.println(cell.getErrorCellValue());
-                   break;
-               
-               // CELL_TYPE_FORMULA will never occur
-               case Cell.CELL_TYPE_FORMULA: 
-                   break;
-       }
+    switch (evaluator.evaluateFormulaCell(cell)) {
+        case Cell.CELL_TYPE_BOOLEAN:
+            System.out.println(cell.getBooleanCellValue());
+            break;
+        case Cell.CELL_TYPE_NUMERIC:
+            System.out.println(cell.getNumericCellValue());
+            break;
+        case Cell.CELL_TYPE_STRING:
+            System.out.println(cell.getStringCellValue());
+            break;
+        case Cell.CELL_TYPE_BLANK:
+            break;
+        case Cell.CELL_TYPE_ERROR:
+            System.out.println(cell.getErrorCellValue());
+            break;
+
+        // CELL_TYPE_FORMULA will never occur
+        case Cell.CELL_TYPE_FORMULA: 
+            break;
+    }
 }
                                </source>
                        </section>
@@ -163,64 +159,59 @@ if (cell!=null) {
                                in place of the old formula.</p>
                                <source>
 FileInputStream fis = new FileInputStream("/somepath/test.xls");
-Workbook wb = new HSSFWorkbook(fis);
+Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
 Sheet sheet = wb.getSheetAt(0);
-FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
+FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 
 // suppose your formula is in B3
-CellReference cellReference = new CellReference("B3"); 
+CellReference cellReference = new CellReference("B3");
 Row row = sheet.getRow(cellReference.getRow());
 Cell cell = row.getCell(cellReference.getCol()); 
-evaluator.setCurrentRow(row);
 
 if (cell!=null) {
-       switch (<strong>evaluator.evaluateInCell</strong>(cell).getCellType()) {
-               case Cell.CELL_TYPE_BOOLEAN:
-                   System.out.println(cell.getBooleanCellValue());
-                   break;
-               case Cell.CELL_TYPE_NUMERIC:
-                   System.out.println(cell.getNumberCellValue());
-                   break;
-               case Cell.CELL_TYPE_STRING:
-                   System.out.println(cell.getStringCellValue());
-                   break;
-               case Cell.CELL_TYPE_BLANK:
-                   break;
-               case Cell.CELL_TYPE_ERROR:
-                   System.out.println(cell.getErrorCellValue());
-                   break;
-               
-               // CELL_TYPE_FORMULA will never occur
-               case Cell.CELL_TYPE_FORMULA: 
-                   break;
-       }
+    switch (evaluator.<strong>evaluateInCell</strong>(cell).getCellType()) {
+        case Cell.CELL_TYPE_BOOLEAN:
+            System.out.println(cell.getBooleanCellValue());
+            break;
+        case Cell.CELL_TYPE_NUMERIC:
+            System.out.println(cell.getNumericCellValue());
+            break;
+        case Cell.CELL_TYPE_STRING:
+            System.out.println(cell.getStringCellValue());
+            break;
+        case Cell.CELL_TYPE_BLANK:
+            break;
+        case Cell.CELL_TYPE_ERROR:
+            System.out.println(cell.getErrorCellValue());
+            break;
+
+        // CELL_TYPE_FORMULA will never occur
+        case Cell.CELL_TYPE_FORMULA:
+            break;
+    }
 }
-                               </source>
+
+        </source>
                        </section>
 
                        <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);
+Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
+FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
 for(int sheetNum = 0; sheetNum &lt; wb.getNumberOfSheets(); sheetNum++) {
-       Sheet sheet = wb.getSheetAt(sheetNum);
-       FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
-
-       for(Iterator rit = sheet.rowIterator(); rit.hasNext();) {
-               Row r = (Row)rit.next();
-               evaluator.setCurrentRow(r);
-
-               for(Iterator cit = r.cellIterator(); cit.hasNext();) {
-                       Cell c = (Cell)cit.next();
-                       if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
-                               evaluator.evaluateFormulaCell(c);
-                       }
-               }
-       }
+    Sheet sheet = wb.getSheetAt(sheetNum);
+    for(Row r : sheet) {
+        for(Cell c : r) {
+            if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
+                evaluator.evaluateFormulaCell(c);
+            }
+        }
+    }
 }
-wb.write(new FileOutputStream("/somepath/changed.xls"));
-                               </source>
+        </source>
                        </section>
                </section>
                
index baec54a0454d92c4c6f2a478b2d2a57609a1fe83..00321db7457b4cc7370bd61c854e4eabe650212e 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.5-beta4" date="2008-??-??">
+           <action dev="POI-DEVELOPERS" type="add">45973 - added CreationHelper.createFormulaEvaluator(), implemeted both for HSSF and XSSF</action>
            <action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
                         <action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
            <action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
index 00eb86e94389ef92cf7e22f2e9d518a7e300116f..04aff3ba2734b035e915b26469f0999573505e0d 100644 (file)
@@ -31,14 +31,14 @@ import org.apache.poi.hssf.record.formula.eval.StringEval;
 import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
 
 /**
- * Implementation for the function COUNTIF<p/>
- * 
- * Syntax: COUNTIF ( range, criteria )
+ * Implementation for the function COUNTIF
+ * <p>
+ *  Syntax: COUNTIF ( range, criteria )
  *    <table border="0" cellpadding="1" cellspacing="0" summary="Parameter descriptions">
  *      <tr><th>range&nbsp;&nbsp;&nbsp;</th><td>is the range of cells to be counted based on the criteria</td></tr>
  *      <tr><th>criteria</th><td>is used to determine which cells to count</td></tr>
  *    </table>
- * <p/>
+ * </p>
  * 
  * @author Josh Micich
  */
index dd72eb5c7924fdf548628ff6892d209a16b709f9..abd32c94d075dceea0919e35ddda78a288e98fe7 100644 (file)
@@ -25,13 +25,13 @@ import org.apache.poi.hssf.record.formula.eval.OperandResolver;
 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
 
 /**
- * Implementation for the ERROR.TYPE() Excel function.<p/>
- * 
+ * Implementation for the ERROR.TYPE() Excel function.
+ * <p>
  * <b>Syntax:</b><br/>
- * <b>ERROR.TYPE</b>(<b>errorValue</b>)<p/>
- * 
+ * <b>ERROR.TYPE</b>(<b>errorValue</b>)</p>
+ * <p>
  * Returns a number corresponding to the error type of the supplied argument.<p/>
- * 
+ * <p>
  *    <table border="1" cellpadding="1" cellspacing="1" summary="Return values for ERROR.TYPE()">
  *      <tr><td>errorValue</td><td>Return Value</td></tr>
  *      <tr><td>#NULL!</td><td>1</td></tr>
@@ -46,7 +46,8 @@ import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
  * 
  * Note - the results of ERROR.TYPE() are different to the constants defined in 
  * <tt>HSSFErrorConstants</tt>.
- * 
+ * </p>
+ *
  * @author Josh Micich
  */
 public final class Errortype implements Function {
index 1c149dbdfce7a9c3c79cf8198b7f032498cb8bfe..1c343222561294981fc88348189fc5535dc36339 100644 (file)
@@ -26,7 +26,8 @@ import org.apache.poi.hssf.record.formula.eval.RefEval;
 import org.apache.poi.hssf.record.formula.eval.ValueEval;
 
 /**
- * Implementation for the Excel function INDEX<p/>
+ * Implementation for the Excel function INDEX
+ * <p>
  * 
  * Syntax : <br/>
  *  INDEX ( reference, row_num[, column_num [, area_num]])</br>
@@ -38,7 +39,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
  *      <tr><th>column_num</th><td>selects column within the array or area reference. default is 1</td></tr>
  *      <tr><th>area_num</th><td>used when reference is a union of areas</td></tr>
  *    </table>
- * <p/>
+ * </p>
  * 
  * @author Josh Micich
  */
index 1ed56e4718fb8966d0cc51f771220269d875c299..c437ba58945f63972952d3e8c02a2c2809c78eff 100644 (file)
@@ -31,7 +31,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
 
 
 /**
- * Implementation for the Excel function SUMPRODUCT<p/>
+ * Implementation for the Excel function SUMPRODUCT<p>
  * 
  * Syntax : <br/>
  *  SUMPRODUCT ( array1[, array2[, array3[, ...]]])
@@ -49,7 +49,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
  *                     A<b>n</b><sub>(<b>i</b>,<b>j</b>)</sub>&nbsp;
  *    )&nbsp;
  *  ) 
- * 
+ * </p>
  * @author Josh Micich
  */
 public final class Sumproduct implements Function {
index fd51b43857a3353ba5a1fe530472cc2648742243..82277661aec6b6c47ac42b4947219beed4edb658 100644 (file)
@@ -671,9 +671,7 @@ public class HSSFCell implements Cell {
      * get the value of the cell as a string - for numeric cells we throw an exception.
      * For blank cells we return an empty string.
      * For formulaCells that are not string Formulas, we return empty String
-     * @deprecated Use the HSSFRichTextString return
      */
-
     public String getStringCellValue()
     {
       HSSFRichTextString str = getRichStringCellValue();
index 6986ef94b5ff4ffe292e3f77ed82632a1ec43567..0eabcfa70669ed135484fadd466f68b4b6c1ca3f 100644 (file)
@@ -44,4 +44,14 @@ public class HSSFCreationHelper implements CreationHelper {
        public HSSFHyperlink createHyperlink(int type) {
                return new HSSFHyperlink(type);
        }
+
+    /**
+     * Creates a HSSFFormulaEvaluator, the object that evaluates formula cells.
+     *
+     * @return a HSSFFormulaEvaluator instance
+     */
+    public HSSFFormulaEvaluator createFormulaEvaluator(){
+        return new HSSFFormulaEvaluator(workbook);
+    }
+
 }
index 0ca8d5723db0cc5a2a2cc9c73f9d753546872ef7..845284c45622cc571c42c890ff1f1f1275eddbc6 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
 import org.apache.poi.ss.formula.WorkbookEvaluator;\r
 import org.apache.poi.ss.usermodel.Cell;\r
 import org.apache.poi.ss.usermodel.CellValue;\r
+import org.apache.poi.ss.usermodel.FormulaEvaluator;\r
 \r
 /**\r
  * Evaluates formula cells.<p/>\r
@@ -39,255 +40,268 @@ import org.apache.poi.ss.usermodel.CellValue;
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;\r
  * @author Josh Micich\r
  */\r
-public class HSSFFormulaEvaluator /* almost implements FormulaEvaluator */ {\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
-       /**\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
+     * @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
-       /**\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
+     * 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
-        * 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
+    /**\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
-        * 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
+     * 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
-               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
+     * 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
-       /**\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
+        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
-        * 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
-       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
+     * 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
-        * 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
+     * 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
-                       for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {\r
-                               HSSFRow r = (HSSFRow)rit.next();\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
-                               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
+     * 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
-       /**\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
+            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
+\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
index b848cd45303d3dcd3ca2986f055bfc93ccba8ef5..4e132dafbdff44f98d1439a4ae3c0a43c9e525c8 100644 (file)
@@ -183,6 +183,15 @@ public interface Cell {
 
     RichTextString getRichStringCellValue();
 
+    /**
+     * Get the value of the cell as a string - for numeric cells we throw an exception
+     * <p>
+     * For blank cells we return an empty string.
+     * For formulaCells that are not string Formulas, we return empty String
+     * </p>
+     */
+    String getStringCellValue();
+
     /**
      * set a boolean value for the cell
      *
index f0b119f99a471d8bcceeb27449ab82cbfdd81491..89dc88e4afc7c7548bed28ebfa7037b7dfd0556e 100644 (file)
@@ -44,4 +44,11 @@ public interface CreationHelper {
      * Creates a new Hyperlink, of the given type
      */
     Hyperlink createHyperlink(int type);
+
+    /**
+     * Creates FormulaEvaluator - an object that evaluates formula cells.
+     *
+     * @return a FormulaEvaluator instance
+     */
+    FormulaEvaluator createFormulaEvaluator();
 }
\ No newline at end of file
index 4c698cc01091c548003a8aa6341b31d07ceb9bd1..fa39817bd410c6af089382f9850edb15bc5691b5 100644 (file)
@@ -31,7 +31,7 @@ public class XSSFCreationHelper implements CreationHelper {
      * Creates a new XSSFRichTextString for you.
      */
        public XSSFRichTextString createRichTextString(String text) {
-        XSSFRichTextString rt =new XSSFRichTextString(text);
+        XSSFRichTextString rt = new XSSFRichTextString(text);
         rt.setStylesTableReference(workbook.getStylesSource());
         return rt;
        }
@@ -43,4 +43,14 @@ public class XSSFCreationHelper implements CreationHelper {
        public XSSFHyperlink createHyperlink(int type) {
                return new XSSFHyperlink(type);
        }
+
+    /**
+     * Creates a XSSFFormulaEvaluator, the object that evaluates formula cells.
+     *
+     * @return a XSSFFormulaEvaluator instance
+     */
+    public XSSFFormulaEvaluator createFormulaEvaluator(){
+        return new XSSFFormulaEvaluator(workbook);
+    }
+
 }