/* ==================================================================== 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.usermodel; import java.util.Iterator; import java.util.Stack; import org.apache.poi.hssf.model.FormulaParser; import org.apache.poi.hssf.record.formula.Area3DPtg; import org.apache.poi.hssf.record.formula.AreaPtg; import org.apache.poi.hssf.record.formula.BoolPtg; import org.apache.poi.hssf.record.formula.ControlPtg; import org.apache.poi.hssf.record.formula.ErrPtg; import org.apache.poi.hssf.record.formula.IntPtg; import org.apache.poi.hssf.record.formula.MemErrPtg; import org.apache.poi.hssf.record.formula.MissingArgPtg; import org.apache.poi.hssf.record.formula.NamePtg; import org.apache.poi.hssf.record.formula.NameXPtg; import org.apache.poi.hssf.record.formula.NumberPtg; import org.apache.poi.hssf.record.formula.OperationPtg; import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.hssf.record.formula.Ref3DPtg; import org.apache.poi.hssf.record.formula.RefPtg; import org.apache.poi.hssf.record.formula.StringPtg; import org.apache.poi.hssf.record.formula.UnionPtg; import org.apache.poi.hssf.record.formula.UnknownPtg; import org.apache.poi.hssf.record.formula.eval.AreaEval; import org.apache.poi.hssf.record.formula.eval.BlankEval; import org.apache.poi.hssf.record.formula.eval.BoolEval; import org.apache.poi.hssf.record.formula.eval.ErrorEval; import org.apache.poi.hssf.record.formula.eval.Eval; import org.apache.poi.hssf.record.formula.eval.FunctionEval; import org.apache.poi.hssf.record.formula.eval.LazyAreaEval; import org.apache.poi.hssf.record.formula.eval.LazyRefEval; import org.apache.poi.hssf.record.formula.eval.NameEval; import org.apache.poi.hssf.record.formula.eval.NameXEval; import org.apache.poi.hssf.record.formula.eval.NumberEval; import org.apache.poi.hssf.record.formula.eval.OperationEval; 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; /** * @author Amol S. Deshmukh < amolweb at ya hoo dot com > * */ public class FormulaEvaluator { protected Sheet _sheet; protected Workbook _workbook; public FormulaEvaluator(Sheet sheet, Workbook workbook) { _sheet = sheet; _workbook = workbook; } /** * Does nothing * @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell */ public void setCurrentRow(Row row) { // do nothing } /** * 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(Cell cell) { CellValue retval = null; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BLANK: retval = new CellValue(Cell.CELL_TYPE_BLANK, _workbook.getCreationHelper()); break; case Cell.CELL_TYPE_BOOLEAN: retval = new CellValue(Cell.CELL_TYPE_BOOLEAN, _workbook.getCreationHelper()); retval.setBooleanValue(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: retval = new CellValue(Cell.CELL_TYPE_ERROR, _workbook.getCreationHelper()); retval.setErrorValue(cell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: retval = getCellValueForEval(internalEvaluate(cell, _sheet, _workbook), _workbook.getCreationHelper()); break; case Cell.CELL_TYPE_NUMERIC: retval = new CellValue(Cell.CELL_TYPE_NUMERIC, _workbook.getCreationHelper()); retval.setNumberValue(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: retval = new CellValue(Cell.CELL_TYPE_STRING, _workbook.getCreationHelper()); retval.setRichTextStringValue(cell.getRichStringCellValue()); break; } } return retval; } /** * 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. *
* int evaluatedCellType = evaluator.evaluateFormulaCell(cell); ** 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(Cell)} * @param cell The cell to evaluate * @return The type of the formula result (the cell's type remains as Cell.CELL_TYPE_FORMULA however) */ public int evaluateFormulaCell(Cell cell) { if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA: CellValue cv = getCellValueForEval(internalEvaluate(cell, _sheet, _workbook), _workbook.getCreationHelper()); switch (cv.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cell.setCellValue(cv.getBooleanValue()); break; case Cell.CELL_TYPE_ERROR: cell.setCellValue(cv.getErrorValue()); break; case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(cv.getNumberValue()); break; case Cell.CELL_TYPE_STRING: cell.setCellValue(cv.getRichTextStringValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_FORMULA: // this will never happen, we have already evaluated the formula break; } return cv.getCellType(); } } return -1; } /** * 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 Cell is returned to * allow chained calls like: *
* int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType(); ** 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(Cell)} * @param cell */ public Cell evaluateInCell(Cell cell) { if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_FORMULA: CellValue cv = getCellValueForEval(internalEvaluate(cell, _sheet, _workbook), _workbook.getCreationHelper()); switch (cv.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellValue(cv.getBooleanValue()); break; case Cell.CELL_TYPE_ERROR: cell.setCellErrorValue(cv.getErrorValue()); break; case Cell.CELL_TYPE_NUMERIC: cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(cv.getNumberValue()); break; case Cell.CELL_TYPE_STRING: cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(cv.getRichTextStringValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_FORMULA: // this will never happen, we have already evaluated the formula break; } } } return cell; } /** * Loops over all cells in all sheets of the supplied * workbook. * For cells that contain formulas, their formulas are * evaluated, and the results are saved. These cells * remain as formula cells. * For cells that do not contain formulas, no changes * are made. * This is a helpful wrapper around looping over all * cells, and calling evaluateFormulaCell on each one. */ public static void evaluateAllFormulaCells(Workbook wb) { for(int i=0; i
null
.
*/
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, int srcRowNum, short srcColNum,
Workbook workbook, Sheet sheet) {
if(operation instanceof FunctionEval) {
FunctionEval fe = (FunctionEval) operation;
if(fe.isFreeRefFunction()) {
return fe.getFreeRefFunction().evaluate(ops, srcRowNum, srcColNum, workbook, sheet);
}
}
return operation.evaluate(ops, srcRowNum, srcColNum);
}
/**
* returns an appropriate Eval impl instance for the Ptg. The Ptg must be
* one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
* StringPtg, BoolPtg