Search in sources :

Example 1 with FunctionNameEval

use of org.apache.poi.ss.formula.eval.FunctionNameEval 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)

Aggregations

FunctionNameEval (org.apache.poi.ss.formula.eval.FunctionNameEval)1 NotImplementedFunctionException (org.apache.poi.ss.formula.eval.NotImplementedFunctionException)1 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)1 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)1