use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.
the class ExcelAntWorkbookUtil method evaluateCell.
/**
* Uses a String in standard Excel format (SheetName!CellId) to locate a
* cell and evaluate it.
*
* @param cellName
* @param expectedValue
* @param precision
*/
public ExcelAntEvaluationResult evaluateCell(String cellName, double expectedValue, double precision) {
ExcelAntEvaluationResult evalResults = null;
Cell cell = getCell(cellName);
FormulaEvaluator evaluator = getEvaluator(excelFileName);
CellValue resultOfEval = evaluator.evaluate(cell);
if (resultOfEval.getErrorValue() == 0) {
// the evaluation did not encounter errors
double result = resultOfEval.getNumberValue();
double delta = Math.abs(result - expectedValue);
if (delta > precision) {
evalResults = new ExcelAntEvaluationResult(false, false, resultOfEval.getNumberValue(), "Results was out of range based on precision " + " of " + precision + ". Delta was actually " + delta, delta, cellName);
} else {
evalResults = new ExcelAntEvaluationResult(false, true, resultOfEval.getNumberValue(), "Evaluation passed without error within in range.", delta, cellName);
}
} else {
String errorMeaning = null;
try {
errorMeaning = FormulaError.forInt(resultOfEval.getErrorValue()).getString();
} catch (IllegalArgumentException iae) {
errorMeaning = "unknown error code: " + Byte.toString(resultOfEval.getErrorValue());
}
evalResults = new ExcelAntEvaluationResult(true, false, resultOfEval.getNumberValue(), "Evaluation failed due to an evaluation error of " + resultOfEval.getErrorValue() + " which is " + errorMeaning, 0, cellName);
}
return evalResults;
}
use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.
the class BaseFormulaEvaluator method evaluateInCell.
/**
* If cell contains formula, it evaluates the formula, and
* puts the formula result back into the cell, in place
* of the old formula.
* Else if cell does not contain formula, this method leaves
* the cell unchanged.
* Note that the same instance of HSSFCell is returned to
* allow chained calls like:
* <pre>
* int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
* </pre>
* Be aware that your cell value will be changed to hold the
* result of the formula. If you simply want the formula
* value computed for you, use {@link #evaluateFormulaCellEnum(Cell)}}
* @param cell
* @return the {@code cell} that was passed in, allowing for chained calls
*/
@Override
public Cell evaluateInCell(Cell cell) {
if (cell == null) {
return null;
}
Cell result = cell;
if (cell.getCellTypeEnum() == CellType.FORMULA) {
CellValue cv = evaluateFormulaCellValue(cell);
setCellValue(cell, cv);
// cell will no longer be a formula cell
setCellType(cell, cv);
}
return result;
}
use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.
the class TestProper method confirm.
private void confirm(String formulaText, String expectedResult) {
cell11.setCellFormula(formulaText);
evaluator.clearAllCachedResultValues();
CellValue cv = evaluator.evaluate(cell11);
if (cv.getCellTypeEnum() != CellType.STRING) {
throw new AssertionFailedError("Wrong result type: " + cv.formatAsString());
}
String actualValue = cv.getStringValue();
assertEquals(expectedResult, actualValue);
}
use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.
the class TestStructuredReferences method confirm.
private static void confirm(FormulaEvaluator fe, Cell cell, double expectedResult) {
fe.clearAllCachedResultValues();
CellValue cv = fe.evaluate(cell);
if (cv.getCellTypeEnum() != CellType.NUMERIC) {
fail("expected numeric cell type but got " + cv.formatAsString());
}
assertEquals(expectedResult, cv.getNumberValue(), 0.0);
}
use of org.apache.poi.ss.usermodel.CellValue in project poi by apache.
the class TestStructuredReferences method confirm.
private static void confirm(FormulaEvaluator fe, Cell cell, String expectedResult) {
fe.clearAllCachedResultValues();
CellValue cv = fe.evaluate(cell);
if (cv.getCellTypeEnum() != CellType.STRING) {
fail("expected String cell type but got " + cv.formatAsString());
}
assertEquals(expectedResult, cv.getStringValue());
}
Aggregations