Search in sources :

Example 1 with NotImplementedFunctionException

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);
}
Also used : FunctionNameEval(org.apache.poi.ss.formula.eval.FunctionNameEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException)

Example 2 with NotImplementedFunctionException

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();
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 3 with NotImplementedFunctionException

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);
}
Also used : HashMap(java.util.HashMap) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) Cell(org.apache.poi.ss.usermodel.Cell) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) HashSet(java.util.HashSet)

Aggregations

NotImplementedFunctionException (org.apache.poi.ss.formula.eval.NotImplementedFunctionException)3 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)2 Cell (org.apache.poi.ss.usermodel.Cell)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FunctionNameEval (org.apache.poi.ss.formula.eval.FunctionNameEval)1 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)1 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)1 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 CellReference (org.apache.poi.ss.util.CellReference)1 Test (org.junit.Test)1