use of org.apache.poi.ss.formula.eval.NotImplementedFunctionException in project poi by apache.
the class UserDefinedFunction method evaluate.
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
int nIncomingArgs = args.length;
if (nIncomingArgs < 1) {
throw new RuntimeException("function name argument missing");
}
ValueEval nameArg = args[0];
String functionName;
if (nameArg instanceof FunctionNameEval) {
functionName = ((FunctionNameEval) nameArg).getFunctionName();
} else {
throw new RuntimeException("First argument should be a NameEval, but got (" + nameArg.getClass().getName() + ")");
}
FreeRefFunction targetFunc = ec.findUserDefinedFunction(functionName);
if (targetFunc == null) {
throw new NotImplementedFunctionException(functionName);
}
int nOutGoingArgs = nIncomingArgs - 1;
ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
return targetFunc.evaluate(outGoingArgs, ec);
}
use of org.apache.poi.ss.formula.eval.NotImplementedFunctionException 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.NotImplementedFunctionException 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