Search in sources :

Example 1 with NumericValueEval

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

the class Match method evaluateLookupRange.

private static ValueVector evaluateLookupRange(ValueEval eval) throws EvaluationException {
    if (eval instanceof RefEval) {
        RefEval re = (RefEval) eval;
        if (re.getNumberOfSheets() == 1) {
            return new SingleValueVector(re.getInnerValueEval(re.getFirstSheetIndex()));
        } else {
            return LookupUtils.createVector(re);
        }
    }
    if (eval instanceof TwoDEval) {
        ValueVector result = LookupUtils.createVector((TwoDEval) eval);
        if (result == null) {
            throw new EvaluationException(ErrorEval.NA);
        }
        return result;
    }
    // Error handling for lookup_range arg is also unusual
    if (eval instanceof NumericValueEval) {
        throw new EvaluationException(ErrorEval.NA);
    }
    if (eval instanceof StringEval) {
        StringEval se = (StringEval) eval;
        Double d = OperandResolver.parseDouble(se.getStringValue());
        if (d == null) {
            // plain string
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        // else looks like a number
        throw new EvaluationException(ErrorEval.NA);
    }
    throw new RuntimeException("Unexpected eval type (" + eval + ")");
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 2 with NumericValueEval

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

the class Match method evaluateMatchTypeArg.

private static double evaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval match_type = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
    if (match_type instanceof ErrorEval) {
        throw new EvaluationException((ErrorEval) match_type);
    }
    if (match_type instanceof NumericValueEval) {
        NumericValueEval ne = (NumericValueEval) match_type;
        return ne.getNumberValue();
    }
    if (match_type instanceof StringEval) {
        StringEval se = (StringEval) match_type;
        Double d = OperandResolver.parseDouble(se.getStringValue());
        if (d == null) {
            // plain string
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        // if the string parses as a number, it is OK
        return d.doubleValue();
    }
    throw new RuntimeException("Unexpected match_type type (" + match_type.getClass().getName() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 3 with NumericValueEval

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

the class MultiOperandNumericFunction method collectValue.

private void collectValue(ValueEval ve, boolean isViaReference, DoubleList temp) throws EvaluationException {
    if (ve == null) {
        throw new IllegalArgumentException("ve must not be null");
    }
    if (ve instanceof BoolEval) {
        if (!isViaReference || _isReferenceBoolCounted) {
            BoolEval boolEval = (BoolEval) ve;
            temp.add(boolEval.getNumberValue());
        }
        return;
    }
    if (ve instanceof NumericValueEval) {
        NumericValueEval ne = (NumericValueEval) ve;
        temp.add(ne.getNumberValue());
        return;
    }
    if (ve instanceof StringValueEval) {
        if (isViaReference) {
            // ignore all ref strings
            return;
        }
        String s = ((StringValueEval) ve).getStringValue();
        Double d = OperandResolver.parseDouble(s);
        if (d == null) {
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        temp.add(d.doubleValue());
        return;
    }
    if (ve instanceof ErrorEval) {
        throw new EvaluationException((ErrorEval) ve);
    }
    if (ve == BlankEval.instance) {
        if (_isBlankCounted) {
            temp.add(0.0);
        }
        return;
    }
    throw new RuntimeException("Invalid ValueEval type passed for conversion: (" + ve.getClass() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) BoolEval(org.apache.poi.ss.formula.eval.BoolEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 4 with NumericValueEval

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

the class Sumproduct method evaluate.

public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
    int maxN = args.length;
    if (maxN < 1) {
        return ErrorEval.VALUE_INVALID;
    }
    ValueEval firstArg = args[0];
    try {
        if (firstArg instanceof NumericValueEval) {
            return evaluateSingleProduct(args);
        }
        if (firstArg instanceof RefEval) {
            return evaluateSingleProduct(args);
        }
        if (firstArg instanceof TwoDEval) {
            TwoDEval ae = (TwoDEval) firstArg;
            if (ae.isRow() && ae.isColumn()) {
                return evaluateSingleProduct(args);
            }
            return evaluateAreaSumProduct(args);
        }
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    throw new RuntimeException("Invalid arg type for SUMPRODUCT: (" + firstArg.getClass().getName() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 5 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)

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