use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.
the class DStarRunner method fullfillsConditions.
/**
* Checks a row in a database against a condition database.
*
* @param db Database.
* @param row The row in the database to check.
* @param cdb The condition database to use for checking.
* @return Whether the row matches the conditions.
* @throws EvaluationException If references could not be resolved or comparison
* operators and operands didn't match.
*/
private static boolean fullfillsConditions(AreaEval db, int row, AreaEval cdb) throws EvaluationException {
// Only one row must match to accept the input, so rows are ORed.
// Each row is made up of cells where each cell is a condition,
// all have to match, so they are ANDed.
final int height = cdb.getHeight();
for (int conditionRow = 1; conditionRow < height; ++conditionRow) {
boolean matches = true;
final int width = cdb.getWidth();
for (int column = 0; column < width; ++column) {
// columns are ANDed
// Whether the condition column matches a database column, if not it's a
// special column that accepts formulas.
boolean columnCondition = true;
ValueEval condition = null;
// The condition to apply.
condition = resolveReference(cdb, conditionRow, column);
// If the condition is empty it matches.
if (condition instanceof BlankEval)
continue;
// The column in the DB to apply the condition to.
ValueEval targetHeader = resolveReference(cdb, 0, column);
if (!(targetHeader instanceof StringValueEval)) {
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
if (getColumnForName(targetHeader, db) == -1)
// No column found, it's again a special column that accepts formulas.
columnCondition = false;
if (columnCondition == true) {
// normal column condition
// Should not throw, checked above.
ValueEval value = resolveReference(db, row, getColumnForName(targetHeader, db));
if (!testNormalCondition(value, condition)) {
matches = false;
break;
}
} else {
// TODO: Check whether the condition cell contains a formula and return #VALUE! if it doesn't.
if (OperandResolver.coerceValueToString(condition).isEmpty()) {
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
throw new NotImplementedException("D* function with formula conditions");
}
}
if (matches == true) {
return true;
}
}
return false;
}
use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.
the class BaseTestExternalFunctions method testExternalFunctions.
@Test
public void testExternalFunctions() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sh = wb.createSheet();
Cell cell1 = sh.createRow(0).createCell(0);
// functions from the Excel Analysis Toolpack
cell1.setCellFormula("ISODD(1)+ISEVEN(2)");
assertEquals("ISODD(1)+ISEVEN(2)", cell1.getCellFormula());
Cell cell2 = sh.createRow(1).createCell(0);
// unregistered functions are parseable and renderable, but may not be evaluateable
cell2.setCellFormula("MYFUNC(\"B1\")");
try {
evaluator.evaluate(cell2);
fail("Expected NotImplementedFunctionException/NotImplementedException");
} catch (final NotImplementedException e) {
assertTrue(e.getCause() instanceof NotImplementedFunctionException);
// Alternatively, a future implementation of evaluate could return #NAME? error to align behavior with Excel
// assertEquals(ErrorEval.NAME_INVALID, ErrorEval.valueOf(evaluator.evaluate(cell2).getErrorValue()));
}
wb.addToolPack(customToolpack);
cell2.setCellFormula("MYFUNC(\"B1\")");
assertEquals("MYFUNC(\"B1\")", cell2.getCellFormula());
Cell cell3 = sh.createRow(2).createCell(0);
//where A2 is defined above
cell3.setCellFormula("MYFUNC2(\"C1\")&\"-\"&A2");
assertEquals("MYFUNC2(\"C1\")&\"-\"&A2", cell3.getCellFormula());
assertEquals(2.0, evaluator.evaluate(cell1).getNumberValue(), 0);
assertEquals("B1abc", evaluator.evaluate(cell2).getStringValue());
assertEquals("C1abc2-B1abc", evaluator.evaluate(cell3).getStringValue());
wb.close();
}
use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.
the class TestFunctionRegistry method testRegisterInRuntime.
public void testRegisterInRuntime() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cellA = row.createCell(0);
cellA.setCellFormula("FISHER(A5)");
CellValue cv;
try {
cv = fe.evaluate(cellA);
fail("expectecd exception");
} catch (NotImplementedException e) {
}
FunctionEval.registerFunction("FISHER", new Function() {
@Override
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
return ErrorEval.NA;
}
});
cv = fe.evaluate(cellA);
assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
HSSFCell cellB = row.createCell(1);
cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
try {
cv = fe.evaluate(cellB);
fail("expectecd exception");
} catch (NotImplementedException e) {
}
AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", new FreeRefFunction() {
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
return ErrorEval.NUM_ERROR;
}
});
cv = fe.evaluate(cellB);
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
}
use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.
the class NumericFunctionInvoker method invokeInternal.
/**
* Formats nicer error messages for the junit output
*/
private static double invokeInternal(Function target, ValueEval[] args, int srcCellRow, int srcCellCol) throws NumericEvalEx {
ValueEval evalResult;
try {
evalResult = target.evaluate(args, srcCellRow, (short) srcCellCol);
} catch (NotImplementedException e) {
throw new NumericEvalEx("Not implemented:" + e.getMessage());
}
if (evalResult == null) {
throw new NumericEvalEx("Result object was null");
}
if (evalResult instanceof ErrorEval) {
ErrorEval ee = (ErrorEval) evalResult;
throw new NumericEvalEx(formatErrorMessage(ee));
}
if (!(evalResult instanceof NumericValueEval)) {
throw new NumericEvalEx("Result object type (" + evalResult.getClass().getName() + ") is invalid. Expected implementor of (" + NumericValueEval.class.getName() + ")");
}
NumericValueEval result = (NumericValueEval) evalResult;
return result.getNumberValue();
}
use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.
the class CheckFunctionsSupported method getEvaluationProblems.
public FormulaEvaluationProblems getEvaluationProblems(Sheet sheet) {
Set<String> unsupportedFunctions = new HashSet<String>();
Map<CellReference, Exception> unevaluatableCells = new HashMap<CellReference, Exception>();
for (Row r : sheet) {
for (Cell c : r) {
try {
evaluator.evaluate(c);
} catch (Exception e) {
if (e instanceof NotImplementedException && e.getCause() != null) {
// Has been wrapped with cell details, but we know those
e = (Exception) e.getCause();
}
if (e instanceof NotImplementedFunctionException) {
NotImplementedFunctionException nie = (NotImplementedFunctionException) e;
unsupportedFunctions.add(nie.getFunctionName());
}
unevaluatableCells.put(new CellReference(c), e);
}
}
}
return new FormulaEvaluationProblems(unsupportedFunctions, unevaluatableCells);
}
Aggregations