You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/09/19 04:20:00 UTC
svn commit: r696898 [2/2] - in /poi/trunk/src: java/org/apache/poi/hssf/dev/
java/org/apache/poi/hssf/eventusermodel/
java/org/apache/poi/hssf/extractor/ java/org/apache/poi/hssf/model/
java/org/apache/poi/hssf/record/ java/org/apache/poi/hssf/record/f...
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java Thu Sep 18 19:19:58 2008
@@ -1,8 +1,32 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.NamePtg;
import org.apache.poi.hssf.record.formula.NameXPtg;
+/**
+ * Abstracts a workbook for the purpose of converting formula to text.<br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
public interface FormulaRenderingWorkbook {
String getSheetNameByExternSheet(int externSheetIndex);
Added: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java?rev=696898&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java (added)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java Thu Sep 18 19:19:58 2008
@@ -0,0 +1,40 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula;
+
+/**
+ * Enumeration of various formula types.<br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
+public final class FormulaType {
+ private FormulaType() {
+ // no instances of this class
+ }
+ public static final int CELL = 0;
+ public static final int SHARED = 1;
+ public static final int ARRAY =2;
+ public static final int CONDFORMAT = 3;
+ public static final int NAMEDRANGE = 4;
+ // this constant is currently very specific. The exact differences from general data
+ // validation formulas or conditional format formulas is not known yet
+ public static final int DATAVALIDATION_LIST = 5;
+
+}
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyAreaEval.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java&p1=poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyAreaEval.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyAreaEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java Thu Sep 18 19:19:58 2008
@@ -15,7 +15,7 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.usermodel;
+package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.AreaI;
import org.apache.poi.hssf.record.formula.AreaI.OffsetArea;
@@ -23,6 +23,9 @@
import org.apache.poi.hssf.record.formula.eval.AreaEvalBase;
import org.apache.poi.hssf.record.formula.eval.BlankEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.CellReference;
/**
@@ -32,9 +35,9 @@
final class LazyAreaEval extends AreaEvalBase {
private final HSSFSheet _sheet;
- private HSSFFormulaEvaluator _evaluator;
+ private final CellEvaluator _evaluator;
- public LazyAreaEval(AreaI ptg, HSSFSheet sheet, HSSFFormulaEvaluator evaluator) {
+ public LazyAreaEval(AreaI ptg, HSSFSheet sheet, CellEvaluator evaluator) {
super(ptg);
_sheet = sheet;
_evaluator = evaluator;
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyRefEval.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java&p1=poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyRefEval.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/LazyRefEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java Thu Sep 18 19:19:58 2008
@@ -15,7 +15,7 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.usermodel;
+package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.AreaI;
import org.apache.poi.hssf.record.formula.Ref3DPtg;
@@ -25,6 +25,9 @@
import org.apache.poi.hssf.record.formula.eval.BlankEval;
import org.apache.poi.hssf.record.formula.eval.RefEvalBase;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.CellReference;
/**
@@ -34,15 +37,15 @@
final class LazyRefEval extends RefEvalBase {
private final HSSFSheet _sheet;
- private final HSSFFormulaEvaluator _evaluator;
+ private final CellEvaluator _evaluator;
- public LazyRefEval(RefPtg ptg, HSSFSheet sheet, HSSFFormulaEvaluator evaluator) {
+ public LazyRefEval(RefPtg ptg, HSSFSheet sheet, CellEvaluator evaluator) {
super(ptg.getRow(), ptg.getColumn());
_sheet = sheet;
_evaluator = evaluator;
}
- public LazyRefEval(Ref3DPtg ptg, HSSFSheet sheet, HSSFFormulaEvaluator evaluator) {
+ public LazyRefEval(Ref3DPtg ptg, HSSFSheet sheet, CellEvaluator evaluator) {
super(ptg.getRow(), ptg.getColumn());
_sheet = sheet;
_evaluator = evaluator;
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/OperandClassTransformer.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/model/OperandClassTransformer.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/OperandClassTransformer.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/OperandClassTransformer.java&p1=poi/trunk/src/java/org/apache/poi/hssf/model/OperandClassTransformer.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/OperandClassTransformer.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/OperandClassTransformer.java Thu Sep 18 19:19:58 2008
@@ -15,7 +15,7 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.model;
+package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
import org.apache.poi.hssf.record.formula.ControlPtg;
@@ -63,10 +63,10 @@
public void transformFormula(ParseNode rootNode) {
byte rootNodeOperandClass;
switch (_formulaType) {
- case FormulaParser.FORMULA_TYPE_CELL:
+ case FormulaType.CELL:
rootNodeOperandClass = Ptg.CLASS_VALUE;
break;
- case FormulaParser.FORMULA_TYPE_DATAVALIDATION_LIST:
+ case FormulaType.DATAVALIDATION_LIST:
rootNodeOperandClass = Ptg.CLASS_REF;
break;
default:
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/OperandClassTransformer.java
------------------------------------------------------------------------------
svn:mergeinfo =
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/usermodel/OperationEvaluatorFactory.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java&p1=poi/trunk/src/java/org/apache/poi/hssf/usermodel/OperationEvaluatorFactory.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/OperationEvaluatorFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java Thu Sep 18 19:19:58 2008
@@ -15,7 +15,7 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.usermodel;
+package org.apache.poi.ss.formula;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
------------------------------------------------------------------------------
svn:mergeinfo =
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/ParseNode.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/model/ParseNode.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/ParseNode.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/ParseNode.java&p1=poi/trunk/src/java/org/apache/poi/hssf/model/ParseNode.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/ParseNode.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/ParseNode.java Thu Sep 18 19:19:58 2008
@@ -15,7 +15,7 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.model;
+package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.AttrPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/ParseNode.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java Thu Sep 18 19:19:58 2008
@@ -1,5 +1,30 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
package org.apache.poi.ss.formula;
+/**
+ * Should be implemented by any {@link Ptg} subclass that needs a workbook to render its formula.
+ * <br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
public interface WorkbookDependentFormula {
String toFormulaString(FormulaRenderingWorkbook book);
}
Copied: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java (from r696836, poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java?p2=poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java&p1=poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java&r1=696836&r2=696898&rev=696898&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFFormulaEvaluator.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java Thu Sep 18 19:19:58 2008
@@ -15,14 +15,10 @@
limitations under the License.
==================================================================== */
-package org.apache.poi.hssf.usermodel;
+package org.apache.poi.ss.formula;
-import java.util.Iterator;
import java.util.Stack;
-import org.apache.poi.hssf.model.FormulaParser;
-import org.apache.poi.hssf.model.Workbook;
-import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.formula.Area3DPtg;
import org.apache.poi.hssf.record.formula.AreaPtg;
import org.apache.poi.hssf.record.formula.BoolPtg;
@@ -54,650 +50,299 @@
import org.apache.poi.hssf.record.formula.eval.RefEval;
import org.apache.poi.hssf.record.formula.eval.StringEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.CellReference;
/**
* Evaluates formula cells.<p/>
- *
+ *
* For performance reasons, this class keeps a cache of all previously calculated intermediate
* cell values. Be sure to call {@link #clearCache()} if any workbook cells are changed between
- * calls to evaluate~ methods on this class.
- *
- * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
+ * calls to evaluate~ methods on this class.<br/>
+ *
+ * For POI internal use only
+ *
* @author Josh Micich
*/
-public class HSSFFormulaEvaluator {
+public class WorkbookEvaluator {
- /**
- * used to track the number of evaluations
- */
- private static final class Counter {
- public int value;
- public int depth;
- public Counter() {
- value = 0;
- }
- }
-
- private final HSSFWorkbook _workbook;
- private final EvaluationCache _cache;
-
- private Counter _evaluationCounter;
-
- /**
- * @deprecated (Sep 2008) HSSFSheet parameter is ignored
- */
- public HSSFFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook) {
- this(workbook);
- if (false) {
- sheet.toString(); // suppress unused parameter compiler warning
- }
- }
- public HSSFFormulaEvaluator(HSSFWorkbook workbook) {
- this(workbook, new EvaluationCache(), new Counter());
- }
-
- private HSSFFormulaEvaluator(HSSFWorkbook workbook, EvaluationCache cache, Counter evaluationCounter) {
- _workbook = workbook;
- _cache = cache;
- _evaluationCounter = evaluationCounter;
- }
-
- /**
- * for debug use. Used in toString methods
- */
- /* package */ String getSheetName(HSSFSheet sheet) {
- return _workbook.getSheetName(_workbook.getSheetIndex(sheet));
- }
- /**
- * for debug/test use
- */
- /* package */ int getEvaluationCount() {
- return _evaluationCounter.value;
- }
-
- private static boolean isDebugLogEnabled() {
- return false;
- }
- private static void logDebug(String s) {
- if (isDebugLogEnabled()) {
- System.out.println(s);
- }
- }
-
- /**
- * Does nothing
- * @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell
- */
- public void setCurrentRow(HSSFRow row) {
- // do nothing
- if (false) {
- row.getClass(); // suppress unused parameter compiler warning
- }
- }
-
- /**
- * Should be called whenever there are changes to input cells in the evaluated workbook.
- * Failure to call this method after changing cell values will cause incorrect behaviour
- * of the evaluate~ methods of this class
- */
- public void clearCache() {
- _cache.clear();
- }
-
- /**
- * Returns an underlying FormulaParser, for the specified
- * Formula String and HSSFWorkbook.
- * This will allow you to generate the Ptgs yourself, if
- * your needs are more complex than just having the
- * formula evaluated.
- */
- public static FormulaParser getUnderlyingParser(HSSFWorkbook workbook, String formula) {
- return new FormulaParser(formula, workbook);
- }
-
- /**
- * If cell contains a formula, the formula is evaluated and returned,
- * else the CellValue simply copies the appropriate cell value from
- * the cell and also its cell type. This method should be preferred over
- * evaluateInCell() when the call should not modify the contents of the
- * original cell.
- * @param cell
- */
- public CellValue evaluate(HSSFCell cell) {
- if (cell == null) {
- return null;
- }
-
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return CellValue.valueOf(cell.getBooleanCellValue());
- case HSSFCell.CELL_TYPE_ERROR:
- return CellValue.getError(cell.getErrorCellValue());
- case HSSFCell.CELL_TYPE_FORMULA:
- return evaluateFormulaCellValue(cell);
- case HSSFCell.CELL_TYPE_NUMERIC:
- return new CellValue(cell.getNumericCellValue());
- case HSSFCell.CELL_TYPE_STRING:
- return new CellValue(cell.getRichStringCellValue().getString());
- }
- throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
- }
-
-
- /**
- * 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>
- * int 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(HSSFCell)}
- * @param cell The cell to evaluate
- * @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however)
- */
- public int evaluateFormulaCell(HSSFCell cell) {
- if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
- return -1;
- }
- CellValue cv = evaluateFormulaCellValue(cell);
- // cell remains a formula cell, but the cached value is changed
- setCellValue(cell, cv);
- return cv.getCellType();
- }
-
- /**
- * 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 #evaluateFormulaCell(HSSFCell)}
- * @param cell
- */
- public HSSFCell evaluateInCell(HSSFCell cell) {
- if (cell == null) {
- return null;
- }
- if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
- CellValue cv = evaluateFormulaCellValue(cell);
- setCellType(cell, cv); // cell will no longer be a formula cell
- setCellValue(cell, cv);
- }
- return cell;
- }
- private static void setCellType(HSSFCell cell, CellValue cv) {
- int cellType = cv.getCellType();
- switch (cellType) {
- case HSSFCell.CELL_TYPE_BOOLEAN:
- case HSSFCell.CELL_TYPE_ERROR:
- case HSSFCell.CELL_TYPE_NUMERIC:
- case HSSFCell.CELL_TYPE_STRING:
- cell.setCellType(cellType);
- return;
- case HSSFCell.CELL_TYPE_BLANK:
- // never happens - blanks eventually get translated to zero
- case HSSFCell.CELL_TYPE_FORMULA:
- // this will never happen, we have already evaluated the formula
- }
- throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
- }
-
- private static void setCellValue(HSSFCell cell, CellValue cv) {
- int cellType = cv.getCellType();
- switch (cellType) {
- case HSSFCell.CELL_TYPE_BOOLEAN:
- cell.setCellValue(cv.getBooleanValue());
- break;
- case HSSFCell.CELL_TYPE_ERROR:
- cell.setCellErrorValue(cv.getErrorValue());
- break;
- case HSSFCell.CELL_TYPE_NUMERIC:
- cell.setCellValue(cv.getNumberValue());
- break;
- case HSSFCell.CELL_TYPE_STRING:
- cell.setCellValue(cv.getRichTextStringValue());
- break;
- case HSSFCell.CELL_TYPE_BLANK:
- // never happens - blanks eventually get translated to zero
- case HSSFCell.CELL_TYPE_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.
- * 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(HSSFWorkbook wb) {
- HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
- for(int i=0; i<wb.getNumberOfSheets(); i++) {
- HSSFSheet sheet = wb.getSheetAt(i);
-
- for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
- HSSFRow r = (HSSFRow)rit.next();
-
- for (Iterator cit = r.cellIterator(); cit.hasNext();) {
- HSSFCell c = (HSSFCell)cit.next();
- if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
- evaluator.evaluateFormulaCell(c);
- }
- }
- }
- }
-
- /**
- * Returns a CellValue wrapper around the supplied ValueEval instance.
- * @param eval
- */
- private CellValue evaluateFormulaCellValue(HSSFCell cell) {
- ValueEval eval = internalEvaluate(cell);
- if (eval instanceof NumberEval) {
- NumberEval ne = (NumberEval) eval;
- return new CellValue(ne.getNumberValue());
- }
- if (eval instanceof BoolEval) {
- BoolEval be = (BoolEval) eval;
- return CellValue.valueOf(be.getBooleanValue());
- }
- if (eval instanceof StringEval) {
- StringEval ne = (StringEval) eval;
- return new CellValue(ne.getStringValue());
- }
- if (eval instanceof ErrorEval) {
- return CellValue.getError(((ErrorEval)eval).getErrorCode());
- }
- throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
- }
-
- /**
- * Dev. Note: Internal evaluate must be passed only a formula cell
- * else a runtime exception will be thrown somewhere inside the method.
- * (Hence this is a private method.)
- * @return never <code>null</code>, never {@link BlankEval}
- */
- private ValueEval internalEvaluate(HSSFCell srcCell) {
- int srcRowNum = srcCell.getRowIndex();
- int srcColNum = srcCell.getCellNum();
-
- ValueEval result;
-
- int sheetIndex = _workbook.getSheetIndex(srcCell.getSheet());
- result = _cache.getValue(sheetIndex, srcRowNum, srcColNum);
- if (result != null) {
- return result;
- }
- _evaluationCounter.value++;
- _evaluationCounter.depth++;
-
- EvaluationCycleDetector tracker = EvaluationCycleDetectorManager.getTracker();
-
- if(!tracker.startEvaluate(_workbook, sheetIndex, srcRowNum, srcColNum)) {
- return ErrorEval.CIRCULAR_REF_ERROR;
- }
- try {
- result = evaluateCell(sheetIndex, srcRowNum, (short)srcColNum, srcCell.getCellFormula());
- } finally {
- tracker.endEvaluate(_workbook, sheetIndex, srcRowNum, srcColNum);
- _cache.setValue(sheetIndex, srcRowNum, srcColNum, result);
- _evaluationCounter.depth--;
- }
- if (isDebugLogEnabled()) {
- String sheetName = _workbook.getSheetName(sheetIndex);
- CellReference cr = new CellReference(srcRowNum, srcColNum);
- logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + result.toString());
- }
- return result;
- }
- private ValueEval evaluateCell(int sheetIndex, int srcRowNum, short srcColNum, String cellFormulaText) {
-
- Ptg[] ptgs = FormulaParser.parse(cellFormulaText, _workbook);
-
- Stack stack = new Stack();
- for (int i = 0, iSize = ptgs.length; i < iSize; i++) {
-
- // since we don't know how to handle these yet :(
- Ptg ptg = ptgs[i];
- if (ptg instanceof ControlPtg) {
- // skip Parentheses, Attr, etc
- continue;
- }
- if (ptg instanceof MemErrPtg) { continue; }
- if (ptg instanceof MissingArgPtg) {
- // TODO - might need to push BlankEval or MissingArgEval
- continue;
- }
- Eval opResult;
- if (ptg instanceof OperationPtg) {
- OperationPtg optg = (OperationPtg) ptg;
-
- if (optg instanceof UnionPtg) { continue; }
-
- OperationEval operation = OperationEvaluatorFactory.create(optg);
-
- int numops = operation.getNumberOfOperands();
- Eval[] ops = new Eval[numops];
-
- // storing the ops in reverse order since they are popping
- for (int j = numops - 1; j >= 0; j--) {
- Eval p = (Eval) stack.pop();
- ops[j] = p;
- }
-// logDebug("invoke " + operation + " (nAgs=" + numops + ")");
- opResult = invokeOperation(operation, ops, _workbook, sheetIndex, srcRowNum, srcColNum);
- } else {
- opResult = getEvalForPtg(ptg, sheetIndex);
- }
- if (opResult == null) {
- throw new RuntimeException("Evaluation result must not be null");
- }
-// logDebug("push " + opResult);
- stack.push(opResult);
- }
-
- ValueEval value = ((ValueEval) stack.pop());
- if (!stack.isEmpty()) {
- throw new IllegalStateException("evaluation stack not empty");
- }
- value = dereferenceValue(value, srcRowNum, srcColNum);
- if (value == BlankEval.INSTANCE) {
- // Note Excel behaviour here. A blank final final value is converted to zero.
- return NumberEval.ZERO;
- // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to
- // blank, the actual value is empty string. This can be verified with ISBLANK().
- }
- return value;
- }
-
- /**
- * Dereferences a single value from any AreaEval or RefEval evaluation result.
- * If the supplied evaluationResult is just a plain value, it is returned as-is.
- * @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt>,
- * <tt>BlankEval</tt> or <tt>ErrorEval</tt>. Never <code>null</code>.
- */
- private static ValueEval dereferenceValue(ValueEval evaluationResult, int srcRowNum, short srcColNum) {
- if (evaluationResult instanceof RefEval) {
- RefEval rv = (RefEval) evaluationResult;
- return rv.getInnerValueEval();
- }
- if (evaluationResult instanceof AreaEval) {
- AreaEval ae = (AreaEval) evaluationResult;
- if (ae.isRow()) {
- if(ae.isColumn()) {
- return ae.getRelativeValue(0, 0);
- }
- return ae.getValueAt(ae.getFirstRow(), srcColNum);
- }
- if (ae.isColumn()) {
- return ae.getValueAt(srcRowNum, ae.getFirstColumn());
- }
- return ErrorEval.VALUE_INVALID;
- }
- return evaluationResult;
- }
-
- private static Eval invokeOperation(OperationEval operation, Eval[] ops,
- HSSFWorkbook workbook, int sheetIndex, int srcRowNum, int srcColNum) {
-
- if(operation instanceof FunctionEval) {
- FunctionEval fe = (FunctionEval) operation;
- if(fe.isFreeRefFunction()) {
- return fe.getFreeRefFunction().evaluate(ops, workbook, sheetIndex, srcRowNum, srcColNum);
- }
- }
- return operation.evaluate(ops, srcRowNum, (short)srcColNum);
- }
-
- private HSSFSheet getOtherSheet(int externSheetIndex) {
- Workbook wb = _workbook.getWorkbook();
- return _workbook.getSheetAt(wb.getSheetIndexFromExternSheetIndex(externSheetIndex));
- }
-
- /**
- * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
- * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
- * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
- * passed here!
- */
- private Eval getEvalForPtg(Ptg ptg, int sheetIndex) {
- if (ptg instanceof NamePtg) {
- // named ranges, macro functions
- NamePtg namePtg = (NamePtg) ptg;
- int numberOfNames = _workbook.getNumberOfNames();
- int nameIndex = namePtg.getIndex();
- if(nameIndex < 0 || nameIndex >= numberOfNames) {
- throw new RuntimeException("Bad name index (" + nameIndex
- + "). Allowed range is (0.." + (numberOfNames-1) + ")");
- }
- NameRecord nameRecord = _workbook.getWorkbook().getNameRecord(nameIndex);
- if (nameRecord.isFunctionName()) {
- return new NameEval(nameRecord.getNameText());
- }
- if (nameRecord.hasFormula()) {
- return evaluateNameFormula(nameRecord.getNameDefinition(), sheetIndex);
- }
-
- throw new RuntimeException("Don't now how to evalate name '" + nameRecord.getNameText() + "'");
- }
- if (ptg instanceof NameXPtg) {
- NameXPtg nameXPtg = (NameXPtg) ptg;
- return new NameXEval(nameXPtg.getSheetRefIndex(), nameXPtg.getNameIndex());
- }
-
- if (ptg instanceof IntPtg) {
- return new NumberEval(((IntPtg)ptg).getValue());
- }
- if (ptg instanceof NumberPtg) {
- return new NumberEval(((NumberPtg)ptg).getValue());
- }
- if (ptg instanceof StringPtg) {
- return new StringEval(((StringPtg) ptg).getValue());
- }
- if (ptg instanceof BoolPtg) {
- return BoolEval.valueOf(((BoolPtg) ptg).getValue());
- }
- if (ptg instanceof ErrPtg) {
- return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
- }
- HSSFSheet sheet = _workbook.getSheetAt(sheetIndex);
- if (ptg instanceof RefPtg) {
- return new LazyRefEval(((RefPtg) ptg), sheet, this);
- }
- if (ptg instanceof AreaPtg) {
- return new LazyAreaEval(((AreaPtg) ptg), sheet, this);
- }
- if (ptg instanceof Ref3DPtg) {
- Ref3DPtg refPtg = (Ref3DPtg) ptg;
- HSSFSheet xsheet = getOtherSheet(refPtg.getExternSheetIndex());
- return new LazyRefEval(refPtg, xsheet, this);
- }
- if (ptg instanceof Area3DPtg) {
- Area3DPtg a3dp = (Area3DPtg) ptg;
- HSSFSheet xsheet = getOtherSheet(a3dp.getExternSheetIndex());
- return new LazyAreaEval(a3dp, xsheet, this);
- }
-
- if (ptg instanceof UnknownPtg) {
- // POI uses UnknownPtg when the encoded Ptg array seems to be corrupted.
- // This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
- // In any case, formulas are re-parsed before execution, so UnknownPtg should not get here
- throw new RuntimeException("UnknownPtg not allowed");
- }
-
- throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
- }
- private Eval evaluateNameFormula(Ptg[] ptgs, int sheetIndex) {
- if (ptgs.length > 1) {
- throw new RuntimeException("Complex name formulas not supported yet");
- }
- return getEvalForPtg(ptgs[0], sheetIndex);
- }
-
- /**
- * Given a cell, find its type and from that create an appropriate ValueEval
- * impl instance and return that. Since the cell could be an external
- * reference, we need the sheet that this belongs to.
- * Non existent cells are treated as empty.
- */
- /* package */ ValueEval getEvalForCell(HSSFCell cell) {
-
- if (cell == null) {
- return BlankEval.INSTANCE;
- }
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_NUMERIC:
- return new NumberEval(cell.getNumericCellValue());
- case HSSFCell.CELL_TYPE_STRING:
- return new StringEval(cell.getRichStringCellValue().getString());
- case HSSFCell.CELL_TYPE_FORMULA:
- return internalEvaluate(cell);
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return BoolEval.valueOf(cell.getBooleanCellValue());
- case HSSFCell.CELL_TYPE_BLANK:
- return BlankEval.INSTANCE;
- case HSSFCell.CELL_TYPE_ERROR:
- return ErrorEval.valueOf(cell.getErrorCellValue());
- }
- throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
- }
-
- /**
- * Mimics the 'data view' of a cell. This allows formula evaluator
- * to return a CellValue instead of precasting the value to String
- * or Number or boolean type.
- * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
- */
- public static final class CellValue {
- public static final CellValue TRUE = new CellValue(HSSFCell.CELL_TYPE_BOOLEAN, 0.0, true, null, 0);
- public static final CellValue FALSE= new CellValue(HSSFCell.CELL_TYPE_BOOLEAN, 0.0, false, null, 0);
-
- private final int _cellType;
- private final double _numberValue;
- private final boolean _booleanValue;
- private final String _textValue;
- private final int _errorCode;
-
- private CellValue(int cellType, double numberValue, boolean booleanValue,
- String textValue, int errorCode) {
- _cellType = cellType;
- _numberValue = numberValue;
- _booleanValue = booleanValue;
- _textValue = textValue;
- _errorCode = errorCode;
- }
-
-
- /* package*/ CellValue(double numberValue) {
- this(HSSFCell.CELL_TYPE_NUMERIC, numberValue, false, null, 0);
- }
- /* package*/ static CellValue valueOf(boolean booleanValue) {
- return booleanValue ? TRUE : FALSE;
- }
- /* package*/ CellValue(String stringValue) {
- this(HSSFCell.CELL_TYPE_STRING, 0.0, false, stringValue, 0);
- }
- /* package*/ static CellValue getError(int errorCode) {
- return new CellValue(HSSFCell.CELL_TYPE_ERROR, 0.0, false, null, errorCode);
- }
-
-
- /**
- * @return Returns the booleanValue.
- */
- public boolean getBooleanValue() {
- return _booleanValue;
- }
- /**
- * @return Returns the numberValue.
- */
- public double getNumberValue() {
- return _numberValue;
- }
- /**
- * @return Returns the stringValue.
- */
- public String getStringValue() {
- return _textValue;
- }
- /**
- * @return Returns the cellType.
- */
- public int getCellType() {
- return _cellType;
- }
- /**
- * @return Returns the errorValue.
- */
- public byte getErrorValue() {
- return (byte) _errorCode;
- }
- /**
- * @return Returns the richTextStringValue.
- * @deprecated (Sep 2008) Text formatting is lost during formula evaluation. Use {@link #getStringValue()}
- */
- public HSSFRichTextString getRichTextStringValue() {
- return new HSSFRichTextString(_textValue);
- }
- public String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(formatAsString());
- sb.append("]");
- return sb.toString();
- }
-
- public String formatAsString() {
- switch (_cellType) {
- case HSSFCell.CELL_TYPE_NUMERIC:
- return String.valueOf(_numberValue);
- case HSSFCell.CELL_TYPE_STRING:
- return '"' + _textValue + '"';
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return _booleanValue ? "TRUE" : "FALSE";
- case HSSFCell.CELL_TYPE_ERROR:
- return ErrorEval.getText(_errorCode);
- }
- return "<error unexpected cell type " + _cellType + ">";
- }
- }
-
- /**
- * debug method
- */
- void inspectPtgs(String formula) {
- Ptg[] ptgs = FormulaParser.parse(formula, _workbook);
- System.out.println("<ptg-group>");
- for (int i = 0, iSize = ptgs.length; i < iSize; i++) {
- System.out.println("<ptg>");
- System.out.println(ptgs[i]);
- if (ptgs[i] instanceof OperationPtg) {
- System.out.println("numoperands: " + ((OperationPtg) ptgs[i]).getNumberOfOperands());
- }
- System.out.println("</ptg>");
- }
- System.out.println("</ptg-group>");
- }
+ /**
+ * used to track the number of evaluations
+ */
+ private static final class Counter {
+ public int value;
+ public int depth;
+ public Counter() {
+ value = 0;
+ }
+ }
+
+ private final EvaluationWorkbook _workbook;
+ private final EvaluationCache _cache;
+
+ private Counter _evaluationCounter;
+
+ public WorkbookEvaluator(EvaluationWorkbook workbook) {
+ _workbook = workbook;
+ _cache = new EvaluationCache();
+ _evaluationCounter = new Counter();
+ }
+
+ /**
+ * for debug use. Used in toString methods
+ */
+ /* package */ String getSheetName(HSSFSheet sheet) {
+ return getSheetName(getSheetIndex(sheet));
+ }
+ private String getSheetName(int sheetIndex) {
+ return _workbook.getSheetName(sheetIndex);
+ }
+ /**
+ * for debug/test use
+ */
+ public int getEvaluationCount() {
+ return _evaluationCounter.value;
+ }
+
+ private static boolean isDebugLogEnabled() {
+ return false;
+ }
+ private static void logDebug(String s) {
+ if (isDebugLogEnabled()) {
+ System.out.println(s);
+ }
+ }
+
+ /**
+ * Should be called whenever there are changes to input cells in the evaluated workbook.
+ * Failure to call this method after changing cell values will cause incorrect behaviour
+ * of the evaluate~ methods of this class
+ */
+ public void clearAllCachedResultValues() {
+ _cache.clear();
+ }
+
+ public void clearCachedResultValue(HSSFSheet sheet, int rowIndex, int columnIndex) {
+ int sheetIndex = getSheetIndex(sheet);
+ _cache.clearValue(sheetIndex, rowIndex, columnIndex);
+
+ }
+ private int getSheetIndex(HSSFSheet sheet) {
+ // TODO cache sheet indexes too
+ return _workbook.getSheetIndex(sheet);
+ }
+
+ public ValueEval evaluate(HSSFCell srcCell) {
+ return internalEvaluate(srcCell, new EvaluationTracker(_cache));
+ }
+
+ /**
+ * Dev. Note: Internal evaluate must be passed only a formula cell
+ * else a runtime exception will be thrown somewhere inside the method.
+ * (Hence this is a private method.)
+ * @return never <code>null</code>, never {@link BlankEval}
+ */
+ /* package */ ValueEval internalEvaluate(HSSFCell srcCell, EvaluationTracker tracker) {
+ int srcRowNum = srcCell.getRowIndex();
+ int srcColNum = srcCell.getCellNum();
+
+ ValueEval result;
+
+ int sheetIndex = getSheetIndex(srcCell.getSheet());
+ result = tracker.startEvaluate(sheetIndex, srcRowNum, srcColNum);
+ if (result != null) {
+ return result;
+ }
+ _evaluationCounter.value++;
+ _evaluationCounter.depth++;
+
+ try {
+ Ptg[] ptgs = _workbook.getFormulaTokens(srcCell);
+ result = evaluateCell(sheetIndex, srcRowNum, (short)srcColNum, ptgs, tracker);
+ } finally {
+ tracker.endEvaluate(sheetIndex, srcRowNum, srcColNum, result);
+ _evaluationCounter.depth--;
+ }
+ if (isDebugLogEnabled()) {
+ String sheetName = getSheetName(sheetIndex);
+ CellReference cr = new CellReference(srcRowNum, srcColNum);
+ logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + result.toString());
+ }
+ return result;
+ }
+ private ValueEval evaluateCell(int sheetIndex, int srcRowNum, short srcColNum, Ptg[] ptgs, EvaluationTracker tracker) {
+
+ Stack stack = new Stack();
+ for (int i = 0, iSize = ptgs.length; i < iSize; i++) {
+
+ // since we don't know how to handle these yet :(
+ Ptg ptg = ptgs[i];
+ if (ptg instanceof ControlPtg) {
+ // skip Parentheses, Attr, etc
+ continue;
+ }
+ if (ptg instanceof MemErrPtg) { continue; }
+ if (ptg instanceof MissingArgPtg) {
+ // TODO - might need to push BlankEval or MissingArgEval
+ continue;
+ }
+ Eval opResult;
+ if (ptg instanceof OperationPtg) {
+ OperationPtg optg = (OperationPtg) ptg;
+
+ if (optg instanceof UnionPtg) { continue; }
+
+ OperationEval operation = OperationEvaluatorFactory.create(optg);
+
+ int numops = operation.getNumberOfOperands();
+ Eval[] ops = new Eval[numops];
+
+ // storing the ops in reverse order since they are popping
+ for (int j = numops - 1; j >= 0; j--) {
+ Eval p = (Eval) stack.pop();
+ ops[j] = p;
+ }
+// logDebug("invoke " + operation + " (nAgs=" + numops + ")");
+ opResult = invokeOperation(operation, ops, _workbook, sheetIndex, srcRowNum, srcColNum);
+ } else {
+ opResult = getEvalForPtg(ptg, sheetIndex, tracker);
+ }
+ if (opResult == null) {
+ throw new RuntimeException("Evaluation result must not be null");
+ }
+// logDebug("push " + opResult);
+ stack.push(opResult);
+ }
+
+ ValueEval value = ((ValueEval) stack.pop());
+ if (!stack.isEmpty()) {
+ throw new IllegalStateException("evaluation stack not empty");
+ }
+ value = dereferenceValue(value, srcRowNum, srcColNum);
+ if (value == BlankEval.INSTANCE) {
+ // Note Excel behaviour here. A blank final final value is converted to zero.
+ return NumberEval.ZERO;
+ // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to
+ // blank, the actual value is empty string. This can be verified with ISBLANK().
+ }
+ return value;
+ }
+
+ /**
+ * Dereferences a single value from any AreaEval or RefEval evaluation result.
+ * If the supplied evaluationResult is just a plain value, it is returned as-is.
+ * @return a <tt>NumberEval</tt>, <tt>StringEval</tt>, <tt>BoolEval</tt>,
+ * <tt>BlankEval</tt> or <tt>ErrorEval</tt>. Never <code>null</code>.
+ */
+ private static ValueEval dereferenceValue(ValueEval evaluationResult, int srcRowNum, short srcColNum) {
+ if (evaluationResult instanceof RefEval) {
+ RefEval rv = (RefEval) evaluationResult;
+ return rv.getInnerValueEval();
+ }
+ if (evaluationResult instanceof AreaEval) {
+ AreaEval ae = (AreaEval) evaluationResult;
+ if (ae.isRow()) {
+ if(ae.isColumn()) {
+ return ae.getRelativeValue(0, 0);
+ }
+ return ae.getValueAt(ae.getFirstRow(), srcColNum);
+ }
+ if (ae.isColumn()) {
+ return ae.getValueAt(srcRowNum, ae.getFirstColumn());
+ }
+ return ErrorEval.VALUE_INVALID;
+ }
+ return evaluationResult;
+ }
+
+ private static Eval invokeOperation(OperationEval operation, Eval[] ops,
+ EvaluationWorkbook workbook, int sheetIndex, int srcRowNum, int srcColNum) {
+
+ if(operation instanceof FunctionEval) {
+ FunctionEval fe = (FunctionEval) operation;
+ if(fe.isFreeRefFunction()) {
+ return fe.getFreeRefFunction().evaluate(ops, workbook, sheetIndex, srcRowNum, srcColNum);
+ }
+ }
+ return operation.evaluate(ops, srcRowNum, (short)srcColNum);
+ }
+
+ private HSSFSheet getOtherSheet(int externSheetIndex) {
+ return _workbook.getSheetByExternSheetIndex(externSheetIndex);
+ }
+
+ /**
+ * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
+ * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
+ * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
+ * passed here!
+ */
+ private Eval getEvalForPtg(Ptg ptg, int sheetIndex, EvaluationTracker tracker) {
+ if (ptg instanceof NamePtg) {
+ // named ranges, macro functions
+ NamePtg namePtg = (NamePtg) ptg;
+ EvaluationName nameRecord = _workbook.getName(namePtg);
+ if (nameRecord.isFunctionName()) {
+ return new NameEval(nameRecord.getNameText());
+ }
+ if (nameRecord.hasFormula()) {
+ return evaluateNameFormula(nameRecord.getNameDefinition(), sheetIndex, tracker);
+ }
+
+ throw new RuntimeException("Don't now how to evalate name '" + nameRecord.getNameText() + "'");
+ }
+ if (ptg instanceof NameXPtg) {
+ return new NameXEval(((NameXPtg) ptg));
+ }
+
+ if (ptg instanceof IntPtg) {
+ return new NumberEval(((IntPtg)ptg).getValue());
+ }
+ if (ptg instanceof NumberPtg) {
+ return new NumberEval(((NumberPtg)ptg).getValue());
+ }
+ if (ptg instanceof StringPtg) {
+ return new StringEval(((StringPtg) ptg).getValue());
+ }
+ if (ptg instanceof BoolPtg) {
+ return BoolEval.valueOf(((BoolPtg) ptg).getValue());
+ }
+ if (ptg instanceof ErrPtg) {
+ return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
+ }
+ CellEvaluator ce = new CellEvaluator(this, tracker);
+ HSSFSheet sheet = _workbook.getSheet(sheetIndex);
+ if (ptg instanceof RefPtg) {
+ return new LazyRefEval(((RefPtg) ptg), sheet, ce);
+ }
+ if (ptg instanceof AreaPtg) {
+ return new LazyAreaEval(((AreaPtg) ptg), sheet, ce);
+ }
+ if (ptg instanceof Ref3DPtg) {
+ Ref3DPtg refPtg = (Ref3DPtg) ptg;
+ HSSFSheet xsheet = getOtherSheet(refPtg.getExternSheetIndex());
+ return new LazyRefEval(refPtg, xsheet, ce);
+ }
+ if (ptg instanceof Area3DPtg) {
+ Area3DPtg a3dp = (Area3DPtg) ptg;
+ HSSFSheet xsheet = getOtherSheet(a3dp.getExternSheetIndex());
+ return new LazyAreaEval(a3dp, xsheet, ce);
+ }
+
+ if (ptg instanceof UnknownPtg) {
+ // POI uses UnknownPtg when the encoded Ptg array seems to be corrupted.
+ // This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
+ // In any case, formulas are re-parsed before execution, so UnknownPtg should not get here
+ throw new RuntimeException("UnknownPtg not allowed");
+ }
+
+ throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
+ }
+ private Eval evaluateNameFormula(Ptg[] ptgs, int sheetIndex, EvaluationTracker tracker) {
+ if (ptgs.length > 1) {
+ throw new RuntimeException("Complex name formulas not supported yet");
+ }
+ return getEvalForPtg(ptgs[0], sheetIndex, tracker);
+ }
}
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java
------------------------------------------------------------------------------
svn:mergeinfo =
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/eventusermodel/TestEventWorkbookBuilder.java Thu Sep 18 19:19:58 2008
@@ -26,7 +26,7 @@
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
-import org.apache.poi.hssf.model.FormulaParser;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;
@@ -101,7 +101,7 @@
// Check we can get the formula without breaking
for(int i=0; i<fRecs.length; i++) {
- FormulaParser.toFormulaString(stubHSSF, fRecs[i].getParsedExpression());
+ HSSFFormulaParser.toFormulaString(stubHSSF, fRecs[i].getParsedExpression());
}
// Peer into just one formula, and check that
@@ -123,19 +123,19 @@
fr = fRecs[0];
assertEquals(1, fr.getRow());
assertEquals(0, fr.getColumn());
- assertEquals("Sheet1!A1", FormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
+ assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A5 is to another sheet
fr = fRecs[3];
assertEquals(4, fr.getRow());
assertEquals(0, fr.getColumn());
- assertEquals("'S2'!A1", FormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
+ assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A7 is to another sheet, range
fr = fRecs[5];
assertEquals(6, fr.getRow());
assertEquals(0, fr.getColumn());
- assertEquals("SUM(Sh3!A1:A4)", FormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
+ assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Now, load via Usermodel and re-check
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java Thu Sep 18 19:19:58 2008
@@ -21,7 +21,6 @@
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.model.FormulaParser.FormulaParseException;
import org.apache.poi.hssf.record.constant.ErrorConstant;
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
import org.apache.poi.hssf.record.formula.AddPtg;
@@ -56,6 +55,7 @@
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.FormulaParserTestHelper;
/**
* Test the low level formula parser functionality. High level tests are to
@@ -67,10 +67,13 @@
* @return parsed token array already confirmed not <code>null</code>
*/
/* package */ static Ptg[] parseFormula(String formula) {
- Ptg[] result = FormulaParser.parse(formula, null);
+ Ptg[] result = HSSFFormulaParser.parse(formula, (HSSFWorkbook)null);
assertNotNull("Ptg array should not be null", result);
return result;
}
+ private static String toFormulaString(Ptg[] ptgs) {
+ return HSSFFormulaParser.toFormulaString((HSSFWorkbook)null, ptgs);
+ }
public void testSimpleFormula() {
Ptg[] ptgs = parseFormula("2+2");
@@ -133,7 +136,7 @@
HSSFWorkbook w = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(w);
- Ptg[] ptg = FormulaParser.parse("myFunc()", w);
+ Ptg[] ptg = HSSFFormulaParser.parse("myFunc()", w);
// myFunc() actually takes 1 parameter. Don't know if POI will ever be able to detect this problem
// the name gets encoded as the first arg
@@ -405,7 +408,7 @@
Ptg[] ptgs = {
new FuncPtg(10),
};
- assertEquals("NA()", FormulaParser.toFormulaString(book, ptgs));
+ assertEquals("NA()", HSSFFormulaParser.toFormulaString(book, ptgs));
}
public void testPercent() {
@@ -639,11 +642,11 @@
String formulaString;
Ptg[] ptgs;
ptgs = parseFormula("sum(5, 2, if(3>2, sum(A1:A2), 6))");
- formulaString = FormulaParser.toFormulaString(null, ptgs);
+ formulaString = toFormulaString(ptgs);
assertEquals("SUM(5,2,IF(3>2,SUM(A1:A2),6))", formulaString);
ptgs = parseFormula("if(1<2,sum(5, 2, if(3>2, sum(A1:A2), 6)),4)");
- formulaString = FormulaParser.toFormulaString(null, ptgs);
+ formulaString = toFormulaString(ptgs);
assertEquals("IF(1<2,SUM(5,2,IF(3>2,SUM(A1:A2),6)),4)", formulaString);
}
public void testParserErrors() {
@@ -665,12 +668,9 @@
try {
parseFormula(formula);
throw new AssertionFailedError("expected parse exception");
- } catch (FormulaParseException e) {
- // expected during successful test
- assertNotNull(e.getMessage());
} catch (RuntimeException e) {
- e.printStackTrace();
- fail("Wrong exception:" + e.getMessage());
+ // expected during successful test
+ FormulaParserTestHelper.confirmParseException(e);
}
}
@@ -697,7 +697,7 @@
Ptg[] ptgs = { spacePtg, new IntPtg(4), };
String formulaString;
try {
- formulaString = FormulaParser.toFormulaString(null, ptgs);
+ formulaString = toFormulaString(ptgs);
} catch (IllegalStateException e) {
if(e.getMessage().equalsIgnoreCase("too much stuff left on the stack")) {
throw new AssertionFailedError("Identified bug 44609");
@@ -709,7 +709,7 @@
assertEquals("4", formulaString);
ptgs = new Ptg[] { new IntPtg(3), spacePtg, new IntPtg(4), spacePtg, AddPtg.instance, };
- formulaString = FormulaParser.toFormulaString(null, ptgs);
+ formulaString = toFormulaString(ptgs);
assertEquals("3+4", formulaString);
}
@@ -725,7 +725,7 @@
DividePtg.instance,
};
try {
- FormulaParser.toFormulaString(null, ptgs);
+ toFormulaString(ptgs);
fail("Expected exception was not thrown");
} catch (IllegalStateException e) {
// expected during successful test
@@ -764,10 +764,10 @@
private static void confirmArgCountMsg(String formula, String expectedMessage) {
HSSFWorkbook book = new HSSFWorkbook();
try {
- FormulaParser.parse(formula, book);
+ HSSFFormulaParser.parse(formula, book);
throw new AssertionFailedError("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
- assertEquals(expectedMessage, e.getMessage());
+ } catch (RuntimeException e) {
+ FormulaParserTestHelper.confirmParseException(e, expectedMessage);
}
}
@@ -776,15 +776,17 @@
try {
parseFormula("round(3.14;2)");
throw new AssertionFailedError("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
- assertEquals("Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'", e.getMessage());
+ } catch (RuntimeException e) {
+ FormulaParserTestHelper.confirmParseException(e,
+ "Parse error near char 10 ';' in specified formula 'round(3.14;2)'. Expected ',' or ')'");
}
try {
parseFormula(" =2+2");
throw new AssertionFailedError("Didn't get parse exception as expected");
- } catch (FormulaParseException e) {
- assertEquals("The specified formula ' =2+2' starts with an equals sign which is not allowed.", e.getMessage());
+ } catch (RuntimeException e) {
+ FormulaParserTestHelper.confirmParseException(e,
+ "The specified formula ' =2+2' starts with an equals sign which is not allowed.");
}
}
@@ -819,7 +821,7 @@
Ptg[] ptgs;
try {
- ptgs = FormulaParser.parse("count(pfy1)", wb);
+ ptgs = HSSFFormulaParser.parse("count(pfy1)", wb);
} catch (IllegalArgumentException e) {
if (e.getMessage().equals("Specified colIx (1012) is out of range")) {
throw new AssertionFailedError("Identified bug 45354");
@@ -835,10 +837,9 @@
try {
cell.setCellFormula("count(pf1)");
throw new AssertionFailedError("Expected formula parse execption");
- } catch (FormulaParseException e) {
- if (!e.getMessage().equals("Specified named range 'pf1' does not exist in the current workbook.")) {
- throw e;
- }
+ } catch (RuntimeException e) {
+ FormulaParserTestHelper.confirmParseException(e,
+ "Specified named range 'pf1' does not exist in the current workbook.");
}
cell.setCellFormula("count(fp1)"); // plain cell ref, col is in range
}
@@ -850,14 +851,14 @@
HSSFWorkbook book = new HSSFWorkbook();
book.createSheet("Sheet1");
- ptgs = FormulaParser.parse("Sheet1!A10:A40000", book);
+ ptgs = HSSFFormulaParser.parse("Sheet1!A10:A40000", book);
aptg = (AreaI) ptgs[0];
if (aptg.getLastRow() == -25537) {
throw new AssertionFailedError("Identified bug 45358");
}
assertEquals(39999, aptg.getLastRow());
- ptgs = FormulaParser.parse("Sheet1!A10:A65536", book);
+ ptgs = HSSFFormulaParser.parse("Sheet1!A10:A65536", book);
aptg = (AreaI) ptgs[0];
assertEquals(65535, aptg.getLastRow());
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParserEval.java Thu Sep 18 19:19:58 2008
@@ -17,9 +17,9 @@
package org.apache.poi.hssf.model;
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
-import org.apache.poi.hssf.model.FormulaParser.FormulaParseException;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
import org.apache.poi.hssf.record.formula.NamePtg;
import org.apache.poi.hssf.record.formula.Ptg;
@@ -30,6 +30,7 @@
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue;
+import org.apache.poi.ss.formula.FormulaParserTestHelper;
/**
* Test the low level formula parser functionality,
@@ -51,21 +52,21 @@
name.setNameName("testName");
name.setReference("A1:A2");
- ptgs = FormulaParser.parse("SUM(testName)", workbook);
+ ptgs = HSSFFormulaParser.parse("SUM(testName)", workbook);
assertTrue("two tokens expected, got "+ptgs.length,ptgs.length == 2);
assertEquals(NamePtg.class, ptgs[0].getClass());
assertEquals(FuncVarPtg.class, ptgs[1].getClass());
// Now make it a single cell
name.setReference("C3");
- ptgs = FormulaParser.parse("SUM(testName)", workbook);
+ ptgs = HSSFFormulaParser.parse("SUM(testName)", workbook);
assertTrue("two tokens expected, got "+ptgs.length,ptgs.length == 2);
assertEquals(NamePtg.class, ptgs[0].getClass());
assertEquals(FuncVarPtg.class, ptgs[1].getClass());
// And make it non-contiguous
name.setReference("A1:A2,C3");
- ptgs = FormulaParser.parse("SUM(testName)", workbook);
+ ptgs = HSSFFormulaParser.parse("SUM(testName)", workbook);
assertTrue("two tokens expected, got "+ptgs.length,ptgs.length == 2);
assertEquals(NamePtg.class, ptgs[0].getClass());
assertEquals(FuncVarPtg.class, ptgs[1].getClass());
@@ -89,11 +90,12 @@
CellValue result;
try {
result = fe.evaluate(cell);
- } catch (FormulaParseException e) {
- if(e.getMessage().equals("Found reference to named range \"A\", but that named range wasn't defined!")) {
- fail("Identifed bug 44539");
+ } catch (RuntimeException e) {
+ FormulaParserTestHelper.confirmParseException(e);
+ if (!e.getMessage().equals("Found reference to named range \"A\", but that named range wasn't defined!")) {
+ throw new AssertionFailedError("Identifed bug 44539");
}
- throw new RuntimeException(e);
+ throw e;
}
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, result.getCellType());
assertEquals(42.0, result.getNumberValue(), 0.0);
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestOperandClassTransformer.java Thu Sep 18 19:19:58 2008
@@ -23,6 +23,7 @@
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Tests specific formula examples in <tt>OperandClassTransformer</tt>.
@@ -31,9 +32,15 @@
*/
public final class TestOperandClassTransformer extends TestCase {
+ private static Ptg[] parseFormula(String formula) {
+ Ptg[] result = HSSFFormulaParser.parse(formula, (HSSFWorkbook)null);
+ assertNotNull("Ptg array should not be null", result);
+ return result;
+ }
+
public void testMdeterm() {
String formula = "MDETERM(ABS(A1))";
- Ptg[] ptgs = FormulaParser.parse(formula, null);
+ Ptg[] ptgs = parseFormula(formula);
confirmTokenClass(ptgs, 0, Ptg.CLASS_ARRAY);
confirmFuncClass(ptgs, 1, "ABS", Ptg.CLASS_ARRAY);
@@ -51,7 +58,7 @@
*/
public void DISABLED_testIndexPi1() {
String formula = "INDEX(PI(),1)";
- Ptg[] ptgs = FormulaParser.parse(formula, null);
+ Ptg[] ptgs = parseFormula(formula);
confirmFuncClass(ptgs, 1, "PI", Ptg.CLASS_ARRAY); // fails as of POI 3.1
confirmFuncClass(ptgs, 2, "INDEX", Ptg.CLASS_VALUE);
@@ -63,7 +70,7 @@
*/
public void testDirectOperandOfValueOperator() {
String formula = "COUNT(A1*1)";
- Ptg[] ptgs = FormulaParser.parse(formula, null);
+ Ptg[] ptgs = parseFormula(formula);
if (ptgs[0].getPtgClass() == Ptg.CLASS_REF) {
throw new AssertionFailedError("Identified bug 45348");
}
@@ -78,13 +85,13 @@
public void testRtoV() {
String formula = "lookup(A1, A3:A52, B3:B52)";
- Ptg[] ptgs = FormulaParser.parse(formula, null);
+ Ptg[] ptgs = parseFormula(formula);
confirmTokenClass(ptgs, 0, Ptg.CLASS_VALUE);
}
public void testComplexIRR_bug45041() {
String formula = "(1+IRR(SUMIF(A:A,ROW(INDIRECT(MIN(A:A)&\":\"&MAX(A:A))),B:B),0))^365-1";
- Ptg[] ptgs = FormulaParser.parse(formula, null);
+ Ptg[] ptgs = parseFormula(formula);
FuncVarPtg rowFunc = (FuncVarPtg) ptgs[10];
FuncVarPtg sumifFunc = (FuncVarPtg) ptgs[12];
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestRVA.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestRVA.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestRVA.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestRVA.java Thu Sep 18 19:19:58 2008
@@ -82,7 +82,7 @@
private void confirmCell(HSSFCell formulaCell, String formula, HSSFWorkbook wb) {
Ptg[] excelPtgs = FormulaExtractor.getPtgs(formulaCell);
- Ptg[] poiPtgs = FormulaParser.parse(formula, wb);
+ Ptg[] poiPtgs = HSSFFormulaParser.parse(formula, wb);
int nExcelTokens = excelPtgs.length;
int nPoiTokens = poiPtgs.length;
if (nExcelTokens != nPoiTokens) {
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestAreaPtg.java Thu Sep 18 19:19:58 2008
@@ -20,7 +20,7 @@
import junit.framework.TestCase;
-import org.apache.poi.hssf.model.FormulaParser;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
@@ -87,7 +87,7 @@
private static String shiftAllColumnsBy1(String formula) {
int letUsShiftColumn1By1Column=1;
HSSFWorkbook wb = null;
- Ptg[] ptgs = FormulaParser.parse(formula, wb);
+ Ptg[] ptgs = HSSFFormulaParser.parse(formula, wb);
for(int i=0; i<ptgs.length; i++)
{
Ptg ptg = ptgs[i];
@@ -98,7 +98,7 @@
aptg.setLastColumn((short)(aptg.getLastColumn()+letUsShiftColumn1By1Column));
}
}
- String newFormula = FormulaParser.toFormulaString(wb, ptgs);
+ String newFormula = HSSFFormulaParser.toFormulaString(wb, ptgs);
return newFormula;
}
}
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java Thu Sep 18 19:19:58 2008
@@ -24,7 +24,7 @@
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.model.FormulaParser;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
@@ -52,7 +52,7 @@
public void testParse() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("externalFunctionExample.xls");
- Ptg[] ptgs = FormulaParser.parse("YEARFRAC(B1,C1)", wb);
+ Ptg[] ptgs = HSSFFormulaParser.parse("YEARFRAC(B1,C1)", wb);
assertEquals(4, ptgs.length);
assertEquals(NameXPtg.class, ptgs[0].getClass());
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestFuncVarPtg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestFuncVarPtg.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestFuncVarPtg.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestFuncVarPtg.java Thu Sep 18 19:19:58 2008
@@ -17,7 +17,7 @@
package org.apache.poi.hssf.record.formula;
-import org.apache.poi.hssf.model.FormulaParser;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import junit.framework.AssertionFailedError;
@@ -36,7 +36,7 @@
*/
public void testOperandClass() {
HSSFWorkbook book = new HSSFWorkbook();
- Ptg[] ptgs = FormulaParser.parse("sum(A1:A2)", book);
+ Ptg[] ptgs = HSSFFormulaParser.parse("sum(A1:A2)", book);
assertEquals(2, ptgs.length);
assertEquals(AreaPtg.class, ptgs[0].getClass());
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java Thu Sep 18 19:19:58 2008
@@ -20,7 +20,7 @@
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
-import org.apache.poi.hssf.model.FormulaParser;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
import org.apache.poi.hssf.record.formula.FuncPtg;
import org.apache.poi.hssf.record.formula.FuncVarPtg;
@@ -36,7 +36,7 @@
private static Ptg[] parse(String formula) {
HSSFWorkbook book = new HSSFWorkbook();
- return FormulaParser.parse(formula, book);
+ return HSSFFormulaParser.parse(formula, book);
}
private static void confirmFunc(String formula, int expPtgArraySize, boolean isVarArgFunc, int funcIx) {
Ptg[] ptgs = parse(formula);
@@ -58,7 +58,7 @@
// check that parsed Ptg array converts back to formula text OK
HSSFWorkbook book = new HSSFWorkbook();
- String reRenderedFormula = FormulaParser.toFormulaString(book, ptgs);
+ String reRenderedFormula = HSSFFormulaParser.toFormulaString(book, ptgs);
assertEquals(formula, reRenderedFormula);
}
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/TestDate.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/TestDate.java?rev=696898&r1=696897&r2=696898&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/TestDate.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/TestDate.java Thu Sep 18 19:19:58 2008
@@ -76,7 +76,7 @@
private void confirm(String formulaText, double expectedResult) {
cell11.setCellFormula(formulaText);
- evaluator.clearCache();
+ evaluator.clearAllCachedResultValues();
double actualValue = evaluator.evaluate(cell11).getNumberValue();
assertEquals(expectedResult, actualValue, 0);
}
Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/FormulaParserTestHelper.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/FormulaParserTestHelper.java?rev=696898&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/FormulaParserTestHelper.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/FormulaParserTestHelper.java Thu Sep 18 19:19:58 2008
@@ -0,0 +1,48 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula;
+
+import junit.framework.Assert;
+import junit.framework.AssertionFailedError;
+
+import org.apache.poi.ss.formula.FormulaParser;
+import org.apache.poi.ss.formula.FormulaParser.FormulaParseException;
+/**
+ * Avoids making {@link FormulaParser#FormulaParseException} public
+ *
+ * @author Josh Micich
+ */
+public class FormulaParserTestHelper {
+ public static void confirmParseException(RuntimeException e, String expectedMessage) {
+ checkType(e);
+ Assert.assertEquals(expectedMessage, e.getMessage());
+ }
+ public static void confirmParseException(RuntimeException e) {
+ checkType(e);
+ Assert.assertNotNull(e.getMessage());
+ }
+ private static void checkType(RuntimeException e) throws AssertionFailedError {
+ if (!(e instanceof FormulaParseException)) {
+ String failMsg = "Expected FormulaParseException, but got ("
+ + e.getClass().getName() + "):";
+ System.err.println(failMsg);
+ e.printStackTrace();
+ throw new AssertionFailedError(failMsg);
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org