Search in sources :

Example 6 with NumericValueEval

use of org.apache.poi.ss.formula.eval.NumericValueEval 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();
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval)

Example 7 with NumericValueEval

use of org.apache.poi.ss.formula.eval.NumericValueEval in project poi by apache.

the class TestSumif method confirmDouble.

private static void confirmDouble(double expected, ValueEval actualEval) {
    if (!(actualEval instanceof NumericValueEval)) {
        throw new AssertionFailedError("Expected numeric result");
    }
    NumericValueEval nve = (NumericValueEval) actualEval;
    assertEquals(expected, nve.getNumberValue(), 0);
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) AssertionFailedError(junit.framework.AssertionFailedError)

Example 8 with NumericValueEval

use of org.apache.poi.ss.formula.eval.NumericValueEval in project poi by apache.

the class TestSumproduct method confirmDouble.

private static void confirmDouble(double expected, ValueEval actualEval) {
    if (!(actualEval instanceof NumericValueEval)) {
        fail("Expected numeric result");
    }
    NumericValueEval nve = (NumericValueEval) actualEval;
    assertEquals(expected, nve.getNumberValue(), 0);
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval)

Example 9 with NumericValueEval

use of org.apache.poi.ss.formula.eval.NumericValueEval in project poi by apache.

the class LookupUtils method resolveRangeLookupArg.

/**
	 * Resolves the last (optional) parameter (<b>range_lookup</b>) to the VLOOKUP and HLOOKUP functions.
	 * @param rangeLookupArg must not be <code>null</code>
	 */
public static boolean resolveRangeLookupArg(ValueEval rangeLookupArg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval valEval = OperandResolver.getSingleValue(rangeLookupArg, srcCellRow, srcCellCol);
    if (valEval instanceof BlankEval) {
        // this does not get the default value
        return false;
    }
    if (valEval instanceof BoolEval) {
        // Happy day flow
        BoolEval boolEval = (BoolEval) valEval;
        return boolEval.getBooleanValue();
    }
    if (valEval instanceof StringEval) {
        String stringValue = ((StringEval) valEval).getStringValue();
        if (stringValue.length() < 1) {
            // Empty string is not the same as BlankEval.  It causes #VALUE! error
            throw EvaluationException.invalidValue();
        }
        // TODO move parseBoolean to OperandResolver
        Boolean b = Countif.parseBoolean(stringValue);
        if (b != null) {
            // string converted to boolean OK
            return b.booleanValue();
        }
        // Excel does not resolve it to a boolean.
        throw EvaluationException.invalidValue();
    // This is in contrast to the code below,, where NumberEvals values (for
    // example 0.01) *do* resolve to equivalent boolean values.
    }
    if (valEval instanceof NumericValueEval) {
        NumericValueEval nve = (NumericValueEval) valEval;
        // zero is FALSE, everything else is TRUE
        return 0.0 != nve.getNumberValue();
    }
    throw new RuntimeException("Unexpected eval type (" + valEval + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) BoolEval(org.apache.poi.ss.formula.eval.BoolEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Aggregations

NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)9 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)4 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)4 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)3 StringEval (org.apache.poi.ss.formula.eval.StringEval)3 TwoDEval (org.apache.poi.ss.formula.TwoDEval)2 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)2 RefEval (org.apache.poi.ss.formula.eval.RefEval)2 AssertionFailedError (junit.framework.AssertionFailedError)1 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)1 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)1 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)1 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)1