]> source.dussan.org Git - poi.git/commitdiff
move HSSFFormulaEvaluator#evaluateInCell and BaseXSSFFormulaEvaluator#evaluateInCell...
authorJaven O'Neal <onealj@apache.org>
Wed, 14 Sep 2016 07:53:58 +0000 (07:53 +0000)
committerJaven O'Neal <onealj@apache.org>
Wed, 14 Sep 2016 07:53:58 +0000 (07:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760651 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
src/java/org/apache/poi/ss/formula/BaseFormulaEvaluator.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFFormulaEvaluator.java
src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFormulaEvaluator.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java

index 7eb1cfc5f09012b95eb4dcc40070d539057a042f..20c7ffa1e42de18ec1b33a13acdcd07d8eb8867d 100644 (file)
@@ -30,7 +30,6 @@ import org.apache.poi.ss.formula.eval.StringValueEval;
 import org.apache.poi.ss.formula.eval.ValueEval;
 import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.ss.usermodel.RichTextString;
@@ -141,34 +140,10 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
     public void notifySetFormula(Cell cell) {
         _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
     }
-
-    /**
-     * 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:
-     * <pre>
-     * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
-     * </pre>
-     * 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;
+        return (HSSFCell) super.evaluateInCell(cell);
     }
 
     /**
index c52c41f9b7d351c2a59910473f3152501d371c55..6cd19f5bf1bc9c6bb4538b72cc728eafb0553eef 100644 (file)
@@ -19,11 +19,9 @@ package org.apache.poi.ss.formula;
 
 import java.util.Map;
 
-import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.CellValue;
-import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Row;
@@ -111,6 +109,37 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
                 throw new IllegalStateException("Bad cell type (" + cell.getCellTypeEnum() + ")");
         }
     }
+    
+    /**
+     * 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:
+     * <pre>
+     * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
+     * </pre>
+     * 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)}}
+     * @param cell
+     * @return the {@code cell} that was passed in, allowing for chained calls
+     */
+    @Override
+    public Cell evaluateInCell(Cell cell) {
+        if (cell == null) {
+            return null;
+        }
+        Cell result = 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;
+    }
 
     protected abstract CellValue evaluateFormulaCellValue(Cell cell);
 
index ac72bc7f389c7bc67cb38f47140cabbd266d40bd..0ac776ffcafae87606e192433fee17792fa2b1ae 100644 (file)
@@ -72,24 +72,9 @@ public final class SXSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
         return new SXSSFEvaluationCell((SXSSFCell)cell);
     }
     
-    /**
-     * 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 SXSSFCell is returned to
-     * allow chained calls like:
-     * <pre>
-     * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
-     * </pre>
-     * 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(org.apache.poi.ss.usermodel.Cell)} }
-     */
+    @Override
     public SXSSFCell evaluateInCell(Cell cell) {
-        doEvaluateInCell(cell);
-        return (SXSSFCell)cell;
+        return (SXSSFCell) super.evaluateInCell(cell);
     }
     
     /**
index 3c72dfe790d2edc95eb376755435129292b3ba04..780126c3d59a5254877ed0c94e03159d2b863670 100644 (file)
@@ -52,22 +52,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
         _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
     }
 
-    /**
-     * 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.
-     */
-    protected void doEvaluateInCell(Cell cell) {
-        if (cell == null) return;
-        if (cell.getCellTypeEnum() == CellType.FORMULA) {
-            CellValue cv = evaluateFormulaCellValue(cell);
-            setCellType(cell, cv); // cell will no longer be a formula cell
-            setCellValue(cell, cv);
-        }
-    }
-
     /**
      * Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
      */
index fc456b7e5857a9933070838cdaa93afce535f19c..e63f47b46875c245bd295d5af492a59287c8387e 100644 (file)
@@ -23,6 +23,8 @@ import org.apache.poi.ss.formula.IStabilityClassifier;
 import org.apache.poi.ss.formula.WorkbookEvaluator;
 import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.CellValue;
 
 /**
  * Evaluates formula cells.<p/>
@@ -55,27 +57,6 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
         return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
     }
 
-    /**
-     * 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 XSSFCell is returned to
-     * allow chained calls like:
-     * <pre>
-     * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
-     * </pre>
-     * 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(org.apache.poi.ss.usermodel.Cell)} }
-     * @param cell
-     */
-    public XSSFCell evaluateInCell(Cell cell) {
-        doEvaluateInCell(cell);
-        return (XSSFCell)cell;
-    }
-
     /**
      * Loops over all cells in all sheets of the supplied
      *  workbook.
@@ -90,6 +71,12 @@ public final class XSSFFormulaEvaluator extends BaseXSSFFormulaEvaluator {
     public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
         BaseFormulaEvaluator.evaluateAllFormulaCells(wb);
     }
+    
+    @Override
+    public XSSFCell evaluateInCell(Cell cell) {
+        return (XSSFCell) super.evaluateInCell(cell);
+    }
+    
     /**
      * Loops over all cells in all sheets of the supplied
      *  workbook.
index 6dcaef960378b34bc871035f5bd2c1482ff2ebf0..84a4aa68006f7925955c4a6fb37dbbf0c002ad07 100644 (file)
@@ -682,4 +682,15 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
         value = evaluator.evaluate(cell);
         assertEquals(1, value.getNumberValue(), 0.001);
     }
+    
+    @Test
+    public void evaluateInCellReturnsSameDataType() throws IOException {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        wb.createSheet().createRow(0).createCell(0);
+        XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+        XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
+        XSSFCell same = evaluator.evaluateInCell(cell);
+        assertSame(cell, same);
+        wb.close();
+    }
 }
index 8a16d3a64470284204caa56a47aafcea9eb0426a..c4922a7c84e8bded9d014ae60a6deb990f931131 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.ss.usermodel;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
@@ -326,4 +327,15 @@ public abstract class BaseTestFormulaEvaluator {
         
         wb.close();
     }
+    
+    @Test
+    public void evaluateInCellReturnsSameCell() throws IOException {
+        Workbook wb = _testDataProvider.createWorkbook();
+        wb.createSheet().createRow(0).createCell(0);
+        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
+        Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
+        Cell same = evaluator.evaluateInCell(cell);
+        assertSame(cell, same);
+        wb.close();
+    }
 }
\ No newline at end of file