Fix inconsistent indents/whitespace

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1693645 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-31 20:24:29 +00:00
parent af9f83fd08
commit 72909b1e6d
1 changed files with 221 additions and 221 deletions

View File

@ -44,255 +44,255 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator;
*/ */
public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider { public class XSSFFormulaEvaluator implements FormulaEvaluator, WorkbookEvaluatorProvider {
private WorkbookEvaluator _bookEvaluator; private WorkbookEvaluator _bookEvaluator;
private XSSFWorkbook _book; private XSSFWorkbook _book;
public XSSFFormulaEvaluator(XSSFWorkbook workbook) { public XSSFFormulaEvaluator(XSSFWorkbook workbook) {
this(workbook, null, null); this(workbook, null, null);
} }
/** /**
* @param stabilityClassifier used to optimise caching performance. Pass <code>null</code> * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
* for the (conservative) assumption that any cell may have its definition changed after * for the (conservative) assumption that any cell may have its definition changed after
* evaluation begins. * evaluation begins.
* @deprecated (Sep 2009) (reduce overloading) use {@link #create(XSSFWorkbook, org.apache.poi.ss.formula.IStabilityClassifier, org.apache.poi.ss.formula.udf.UDFFinder)} * @deprecated (Sep 2009) (reduce overloading) use {@link #create(XSSFWorkbook, org.apache.poi.ss.formula.IStabilityClassifier, org.apache.poi.ss.formula.udf.UDFFinder)}
*/ */
@Deprecated @Deprecated
public XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier) { public XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier) {
_bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, null); _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, null);
_book = workbook; _book = workbook;
} }
private XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) { private XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
_bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder); _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder);
_book = workbook; _book = workbook;
} }
/** /**
* @param stabilityClassifier used to optimise caching performance. Pass <code>null</code> * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
* for the (conservative) assumption that any cell may have its definition changed after * for the (conservative) assumption that any cell may have its definition changed after
* evaluation begins. * evaluation begins.
* @param udfFinder pass <code>null</code> for default (AnalysisToolPak only) * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
*/ */
public static XSSFFormulaEvaluator create(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) { public static XSSFFormulaEvaluator create(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder); return new XSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
} }
/** /**
* Should be called whenever there are major changes (e.g. moving sheets) to input cells * Should be called whenever there are major changes (e.g. moving sheets) to input cells
* in the evaluated workbook. * in the evaluated workbook.
* Failure to call this method after changing cell values will cause incorrect behaviour * Failure to call this method after changing cell values will cause incorrect behaviour
* of the evaluate~ methods of this class * of the evaluate~ methods of this class
*/ */
public void clearAllCachedResultValues() { public void clearAllCachedResultValues() {
_bookEvaluator.clearAllCachedResultValues(); _bookEvaluator.clearAllCachedResultValues();
} }
public void notifySetFormula(Cell cell) { public void notifySetFormula(Cell cell) {
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell)); _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
} }
public void notifyDeleteCell(Cell cell) { public void notifyDeleteCell(Cell cell) {
_bookEvaluator.notifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell)); _bookEvaluator.notifyDeleteCell(new XSSFEvaluationCell((XSSFCell)cell));
} }
public void notifyUpdateCell(Cell cell) { public void notifyUpdateCell(Cell cell) {
_bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell)); _bookEvaluator.notifyUpdateCell(new XSSFEvaluationCell((XSSFCell)cell));
} }
/** /**
* If cell contains a formula, the formula is evaluated and returned, * If cell contains a formula, the formula is evaluated and returned,
* else the CellValue simply copies the appropriate cell value from * else the CellValue simply copies the appropriate cell value from
* the cell and also its cell type. This method should be preferred over * the cell and also its cell type. This method should be preferred over
* evaluateInCell() when the call should not modify the contents of the * evaluateInCell() when the call should not modify the contents of the
* original cell. * original cell.
* @param cell * @param cell
*/ */
public CellValue evaluate(Cell cell) { public CellValue evaluate(Cell cell) {
if (cell == null) { if (cell == null) {
return null; return null;
} }
switch (cell.getCellType()) { switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_BOOLEAN: case XSSFCell.CELL_TYPE_BOOLEAN:
return CellValue.valueOf(cell.getBooleanCellValue()); return CellValue.valueOf(cell.getBooleanCellValue());
case XSSFCell.CELL_TYPE_ERROR: case XSSFCell.CELL_TYPE_ERROR:
return CellValue.getError(cell.getErrorCellValue()); return CellValue.getError(cell.getErrorCellValue());
case XSSFCell.CELL_TYPE_FORMULA: case XSSFCell.CELL_TYPE_FORMULA:
return evaluateFormulaCellValue(cell); return evaluateFormulaCellValue(cell);
case XSSFCell.CELL_TYPE_NUMERIC: case XSSFCell.CELL_TYPE_NUMERIC:
return new CellValue(cell.getNumericCellValue()); return new CellValue(cell.getNumericCellValue());
case XSSFCell.CELL_TYPE_STRING: case XSSFCell.CELL_TYPE_STRING:
return new CellValue(cell.getRichStringCellValue().getString()); return new CellValue(cell.getRichStringCellValue().getString());
case XSSFCell.CELL_TYPE_BLANK: case XSSFCell.CELL_TYPE_BLANK:
return null; return null;
} }
throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
} }
/** /**
* If cell contains formula, it evaluates the formula, * If cell contains formula, it evaluates the formula,
* and saves the result of the formula. The cell * and saves the result of the formula. The cell
* remains as a formula cell. * remains as a formula cell.
* Else if cell does not contain formula, this method leaves * Else if cell does not contain formula, this method leaves
* the cell unchanged. * the cell unchanged.
* Note that the type of the formula result is returned, * Note that the type of the formula result is returned,
* so you know what kind of value is also stored with * so you know what kind of value is also stored with
* the formula. * the formula.
* <pre> * <pre>
* int evaluatedCellType = evaluator.evaluateFormulaCell(cell); * int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
* </pre> * </pre>
* Be aware that your cell will hold both the formula, * Be aware that your cell will hold both the formula,
* and the result. If you want the cell replaced with * and the result. If you want the cell replaced with
* the result of the formula, use {@link #evaluate(org.apache.poi.ss.usermodel.Cell)} } * the result of the formula, use {@link #evaluate(org.apache.poi.ss.usermodel.Cell)} }
* @param cell The cell to evaluate * @param cell The cell to evaluate
* @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however) * @return The type of the formula result (the cell's type remains as HSSFCell.CELL_TYPE_FORMULA however)
*/ */
public int evaluateFormulaCell(Cell cell) { public int evaluateFormulaCell(Cell cell) {
if (cell == null || cell.getCellType() != XSSFCell.CELL_TYPE_FORMULA) { if (cell == null || cell.getCellType() != XSSFCell.CELL_TYPE_FORMULA) {
return -1; return -1;
} }
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
// cell remains a formula cell, but the cached value is changed // cell remains a formula cell, but the cached value is changed
setCellValue(cell, cv); setCellValue(cell, cv);
return cv.getCellType(); return cv.getCellType();
} }
/** /**
* If cell contains formula, it evaluates the formula, and * If cell contains formula, it evaluates the formula, and
* puts the formula result back into the cell, in place * puts the formula result back into the cell, in place
* of the old formula. * of the old formula.
* Else if cell does not contain formula, this method leaves * Else if cell does not contain formula, this method leaves
* the cell unchanged. * the cell unchanged.
* Note that the same instance of HSSFCell is returned to * Note that the same instance of HSSFCell is returned to
* allow chained calls like: * allow chained calls like:
* <pre> * <pre>
* int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType(); * int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
* </pre> * </pre>
* Be aware that your cell value will be changed to hold the * Be aware that your cell value will be changed to hold the
* result of the formula. If you simply want the formula * result of the formula. If you simply want the formula
* value computed for you, use {@link #evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)} } * value computed for you, use {@link #evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)} }
* @param cell * @param cell
*/ */
public XSSFCell evaluateInCell(Cell cell) { public XSSFCell evaluateInCell(Cell cell) {
if (cell == null) { if (cell == null) {
return null; return null;
} }
XSSFCell result = (XSSFCell) cell; XSSFCell result = (XSSFCell) cell;
if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) { if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
CellValue cv = evaluateFormulaCellValue(cell); CellValue cv = evaluateFormulaCellValue(cell);
setCellType(cell, cv); // cell will no longer be a formula cell setCellType(cell, cv); // cell will no longer be a formula cell
setCellValue(cell, cv); setCellValue(cell, cv);
} }
return result; return result;
} }
private static void setCellType(Cell cell, CellValue cv) { private static void setCellType(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); int cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case XSSFCell.CELL_TYPE_BOOLEAN: case XSSFCell.CELL_TYPE_BOOLEAN:
case XSSFCell.CELL_TYPE_ERROR: case XSSFCell.CELL_TYPE_ERROR:
case XSSFCell.CELL_TYPE_NUMERIC: case XSSFCell.CELL_TYPE_NUMERIC:
case XSSFCell.CELL_TYPE_STRING: case XSSFCell.CELL_TYPE_STRING:
cell.setCellType(cellType); cell.setCellType(cellType);
return; return;
case XSSFCell.CELL_TYPE_BLANK: case XSSFCell.CELL_TYPE_BLANK:
// never happens - blanks eventually get translated to zero // never happens - blanks eventually get translated to zero
case XSSFCell.CELL_TYPE_FORMULA: case XSSFCell.CELL_TYPE_FORMULA:
// this will never happen, we have already evaluated the formula // this will never happen, we have already evaluated the formula
} }
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
} }
private static void setCellValue(Cell cell, CellValue cv) { private static void setCellValue(Cell cell, CellValue cv) {
int cellType = cv.getCellType(); int cellType = cv.getCellType();
switch (cellType) { switch (cellType) {
case XSSFCell.CELL_TYPE_BOOLEAN: case XSSFCell.CELL_TYPE_BOOLEAN:
cell.setCellValue(cv.getBooleanValue()); cell.setCellValue(cv.getBooleanValue());
break; break;
case XSSFCell.CELL_TYPE_ERROR: case XSSFCell.CELL_TYPE_ERROR:
cell.setCellErrorValue(cv.getErrorValue()); cell.setCellErrorValue(cv.getErrorValue());
break; break;
case XSSFCell.CELL_TYPE_NUMERIC: case XSSFCell.CELL_TYPE_NUMERIC:
cell.setCellValue(cv.getNumberValue()); cell.setCellValue(cv.getNumberValue());
break; break;
case XSSFCell.CELL_TYPE_STRING: case XSSFCell.CELL_TYPE_STRING:
cell.setCellValue(new XSSFRichTextString(cv.getStringValue())); cell.setCellValue(new XSSFRichTextString(cv.getStringValue()));
break; break;
case XSSFCell.CELL_TYPE_BLANK: case XSSFCell.CELL_TYPE_BLANK:
// never happens - blanks eventually get translated to zero // never happens - blanks eventually get translated to zero
case XSSFCell.CELL_TYPE_FORMULA: case XSSFCell.CELL_TYPE_FORMULA:
// this will never happen, we have already evaluated the formula // this will never happen, we have already evaluated the formula
default: default:
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")"); throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
} }
} }
/** /**
* Loops over all cells in all sheets of the supplied * Loops over all cells in all sheets of the supplied
* workbook. * workbook.
* For cells that contain formulas, their formulas are * For cells that contain formulas, their formulas are
* evaluated, and the results are saved. These cells * evaluated, and the results are saved. These cells
* remain as formula cells. * remain as formula cells.
* For cells that do not contain formulas, no changes * For cells that do not contain formulas, no changes
* are made. * are made.
* This is a helpful wrapper around looping over all * This is a helpful wrapper around looping over all
* cells, and calling evaluateFormulaCell on each one. * cells, and calling evaluateFormulaCell on each one.
*/ */
public static void evaluateAllFormulaCells(XSSFWorkbook wb) { public static void evaluateAllFormulaCells(XSSFWorkbook wb) {
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
} }
/** /**
* Loops over all cells in all sheets of the supplied * Loops over all cells in all sheets of the supplied
* workbook. * workbook.
* For cells that contain formulas, their formulas are * For cells that contain formulas, their formulas are
* evaluated, and the results are saved. These cells * evaluated, and the results are saved. These cells
* remain as formula cells. * remain as formula cells.
* For cells that do not contain formulas, no changes * For cells that do not contain formulas, no changes
* are made. * are made.
* This is a helpful wrapper around looping over all * This is a helpful wrapper around looping over all
* cells, and calling evaluateFormulaCell on each one. * cells, and calling evaluateFormulaCell on each one.
*/ */
public void evaluateAll() { public void evaluateAll() {
HSSFFormulaEvaluator.evaluateAllFormulaCells(_book); HSSFFormulaEvaluator.evaluateAllFormulaCells(_book);
} }
/** /**
* Turns a XSSFCell into a XSSFEvaluationCell * Turns a XSSFCell into a XSSFEvaluationCell
*/ */
protected EvaluationCell toEvaluationCell(Cell cell) { protected EvaluationCell toEvaluationCell(Cell cell) {
if (!(cell instanceof XSSFCell)){ if (!(cell instanceof XSSFCell)){
throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." + throw new IllegalArgumentException("Unexpected type of cell: " + cell.getClass() + "." +
" Only XSSFCells can be evaluated."); " Only XSSFCells can be evaluated.");
} }
return new XSSFEvaluationCell((XSSFCell)cell); return new XSSFEvaluationCell((XSSFCell)cell);
} }
/**
* Returns a CellValue wrapper around the supplied ValueEval instance.
*/
private CellValue evaluateFormulaCellValue(Cell cell) {
EvaluationCell evalCell = toEvaluationCell(cell);
ValueEval eval = _bookEvaluator.evaluate(evalCell);
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() + ")");
}
/**
* Returns a CellValue wrapper around the supplied ValueEval instance.
*/
private CellValue evaluateFormulaCellValue(Cell cell) {
EvaluationCell evalCell = toEvaluationCell(cell);
ValueEval eval = _bookEvaluator.evaluate(evalCell);
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() + ")");
}
public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> evaluators) { public void setupReferencedWorkbooks(Map<String, FormulaEvaluator> evaluators) {
CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators); CollaboratingWorkbooksEnvironment.setupFormulaEvaluator(evaluators);
} }
public WorkbookEvaluator _getWorkbookEvaluator() { public WorkbookEvaluator _getWorkbookEvaluator() {
return _bookEvaluator; return _bookEvaluator;
} }