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

src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java
src/java/org/apache/poi/ss/formula/BaseFormulaEvaluator.java
src/java/org/apache/poi/ss/usermodel/FormulaEvaluator.java
src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java

index 7154d7450411fc7904e4d0c73bd30edae0dead8f..7eb1cfc5f09012b95eb4dcc40070d539057a042f 100644 (file)
@@ -33,9 +33,8 @@ 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;
 import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.util.Internal;
-import org.apache.poi.util.Removal;
 
 /**
  * Evaluates formula cells.<p/>
@@ -82,6 +81,11 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
     public static HSSFFormulaEvaluator create(HSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
         return new HSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
     }
+    
+    @Override
+    protected RichTextString createRichTextString(String str) {
+        return new HSSFRichTextString(str);
+    }
 
 
     /**
@@ -138,34 +142,6 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
         _bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
     }
 
-    /**
-     * If cell contains formula, it evaluates the formula, and saves the result of the formula. The
-     * cell remains as a formula cell. If the cell does not contain formula, rather than throwing an
-     * exception, this method returns {@link CellType#_NONE} and leaves the cell unchanged.
-     *
-     * Note that the type of the <em>formula result</em> is returned, so you know what kind of
-     * cached formula result is also stored with  the formula.
-     * <pre>
-     * CellType evaluatedCellType = evaluator.evaluateFormulaCell(cell);
-     * </pre>
-     * Be aware that your cell will hold both the formula, and the result. If you want the cell
-     * replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
-     * @param cell The cell to evaluate
-     * @return {@link CellType#_NONE} for non-formula cells, or the type of the <em>formula result</em>
-     * @since POI 3.15 beta 3
-     * @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
-     */
-    @Override
-    public CellType evaluateFormulaCellEnum(Cell cell) {
-        if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
-            return CellType._NONE;
-        }
-        CellValue cv = evaluateFormulaCellValue(cell);
-        // cell remains a formula cell, but the cached value is changed
-        setCellValue(cell, cv);
-        return cv.getCellTypeEnum();
-    }
-
     /**
      * If cell contains formula, it evaluates the formula, and
      *  puts the formula result back into the cell, in place
@@ -195,30 +171,6 @@ public class HSSFFormulaEvaluator extends BaseFormulaEvaluator {
         return result;
     }
 
-    private static void setCellValue(Cell cell, CellValue cv) {
-        CellType cellType = cv.getCellTypeEnum();
-        switch (cellType) {
-            case BOOLEAN:
-                cell.setCellValue(cv.getBooleanValue());
-                break;
-            case ERROR:
-                cell.setCellErrorValue(cv.getErrorValue());
-                break;
-            case NUMERIC:
-                cell.setCellValue(cv.getNumberValue());
-                break;
-            case STRING:
-                cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
-                break;
-            case BLANK:
-                // never happens - blanks eventually get translated to zero
-            case FORMULA:
-                // this will never happen, we have already evaluated the formula
-            default:
-                throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
-        }
-    }
-
     /**
      * Loops over all cells in all sheets of the supplied
      *  workbook.
index 7c459abb45c8273a9ed2e7b34a8b3bef71a4a9b2..c52c41f9b7d351c2a59910473f3152501d371c55 100644 (file)
@@ -19,10 +19,13 @@ 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;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -131,6 +134,38 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
     public int evaluateFormulaCell(Cell cell) {
         return evaluateFormulaCellEnum(cell).getCode();
     }
+    
+    /**
+     * If cell contains formula, it evaluates the formula,
+     *  and saves the result of the formula. The cell
+     *  remains as a formula cell.
+     * Else if cell does not contain formula, this method leaves
+     *  the cell unchanged.
+     * Note that the type of the formula result is returned,
+     *  so you know what kind of value is also stored with
+     *  the formula.
+     * <pre>
+     * CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(cell);
+     * </pre>
+     * Be aware that your cell will hold both the formula,
+     *  and the result. If you want the cell replaced with
+     *  the result of the formula, use {@link #evaluate(org.apache.poi.ss.usermodel.Cell)} }
+     * @param cell The cell to evaluate
+     * @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
+     *         If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
+     * @since POI 3.15 beta 3
+     * @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
+     */
+    @Override
+    public CellType evaluateFormulaCellEnum(Cell cell) {
+        if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
+            return CellType._NONE;
+        }
+        CellValue cv = evaluateFormulaCellValue(cell);
+        // cell remains a formula cell, but the cached value is changed
+        setCellValue(cell, cv);
+        return cv.getCellTypeEnum();
+    }
 
     protected static void setCellType(Cell cell, CellValue cv) {
         CellType cellType = cv.getCellTypeEnum();
@@ -151,6 +186,33 @@ public abstract class BaseFormulaEvaluator implements FormulaEvaluator, Workbook
                 throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
         }
     }
+    
+    protected abstract RichTextString createRichTextString(String str);
+    
+    protected void setCellValue(Cell cell, CellValue cv) {
+        CellType cellType = cv.getCellTypeEnum();
+        switch (cellType) {
+            case BOOLEAN:
+                cell.setCellValue(cv.getBooleanValue());
+                break;
+            case ERROR:
+                cell.setCellErrorValue(cv.getErrorValue());
+                break;
+            case NUMERIC:
+                cell.setCellValue(cv.getNumberValue());
+                break;
+            case STRING:
+                cell.setCellValue(createRichTextString(cv.getStringValue()));
+                break;
+            case BLANK:
+                // never happens - blanks eventually get translated to zero
+            case FORMULA:
+                // this will never happen, we have already evaluated the formula
+            default:
+                throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
+        }
+    }
+    
 
     /**
      * Loops over all cells in all sheets of the supplied
index be75ff9d559ef0cae2fc5f21a05bc2fb751eb595..357bd073a6619fd673f3f49429f42f9f2ab71e9c 100644 (file)
@@ -101,6 +101,7 @@ public interface FormulaEvaluator {
      *      or one of {@link CellType#NUMERIC}, {@link CellType#STRING},
      *      {@link CellType#BOOLEAN}, {@link CellType#ERROR}
      *      Note: the cell's type remains as CellType.FORMULA however.
+     * @deprecated 3.15. Will return a {@link CellType} enum in the future
      */
     int evaluateFormulaCell(Cell cell);
     
index e5bd1a27ca80422ffd30544d73e4e3b71fdce89c..3c72dfe790d2edc95eb376755435129292b3ba04 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.eval.ValueEval;
 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.RichTextString;
 
 /**
  * Internal POI use only - parent of XSSF and SXSSF formula evaluators
@@ -36,6 +37,10 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
     protected BaseXSSFFormulaEvaluator(WorkbookEvaluator bookEvaluator) {
         super(bookEvaluator);
     }
+    @Override
+    protected RichTextString createRichTextString(String str) {
+        return new XSSFRichTextString(str);
+    }
 
     public void notifySetFormula(Cell cell) {
         _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
@@ -47,37 +52,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
         _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
     }
 
-    /**
-     * If cell contains formula, it evaluates the formula,
-     *  and saves the result of the formula. The cell
-     *  remains as a formula cell.
-     * Else if cell does not contain formula, this method leaves
-     *  the cell unchanged.
-     * Note that the type of the formula result is returned,
-     *  so you know what kind of value is also stored with
-     *  the formula.
-     * <pre>
-     * CellType evaluatedCellType = evaluator.evaluateFormulaCellEnum(cell);
-     * </pre>
-     * Be aware that your cell will hold both the formula,
-     *  and the result. If you want the cell replaced with
-     *  the result of the formula, use {@link #evaluate(org.apache.poi.ss.usermodel.Cell)} }
-     * @param cell The cell to evaluate
-     * @return The type of the formula result (the cell's type remains as CellType.FORMULA however)
-     *         If cell is not a formula cell, returns {@link CellType#_NONE} rather than throwing an exception.
-     * @since POI 3.15 beta 3
-     * @deprecated POI 3.15 beta 3. Will be deleted when we make the CellType enum transition. See bug 59791.
-     */
-    public CellType evaluateFormulaCellEnum(Cell cell) {
-        if (cell == null || cell.getCellTypeEnum() != CellType.FORMULA) {
-            return CellType._NONE;
-        }
-        CellValue cv = evaluateFormulaCellValue(cell);
-        // cell remains a formula cell, but the cached value is changed
-        setCellValue(cell, cv);
-        return cv.getCellTypeEnum();
-    }
-
     /**
      * If cell contains formula, it evaluates the formula, and
      *  puts the formula result back into the cell, in place
@@ -94,30 +68,6 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
         }
     }
 
-    private static void setCellValue(Cell cell, CellValue cv) {
-        CellType cellType = cv.getCellTypeEnum();
-        switch (cellType) {
-            case BOOLEAN:
-                cell.setCellValue(cv.getBooleanValue());
-                break;
-            case ERROR:
-                cell.setCellErrorValue(cv.getErrorValue());
-                break;
-            case NUMERIC:
-                cell.setCellValue(cv.getNumberValue());
-                break;
-            case STRING:
-                cell.setCellValue(new XSSFRichTextString(cv.getStringValue()));
-                break;
-            case BLANK:
-                // never happens - blanks eventually get translated to zero
-            case FORMULA:
-                // this will never happen, we have already evaluated the formula
-            default:
-                throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
-        }
-    }
-
     /**
      * Turns a XSSFCell / SXSSFCell into a XSSFEvaluationCell
      */