Search in sources :

Example 1 with ErrorEval

use of org.apache.poi.ss.formula.eval.ErrorEval 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 2 with ErrorEval

use of org.apache.poi.ss.formula.eval.ErrorEval 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 3 with ErrorEval

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

the class Sumproduct method throwFirstError.

private static void throwFirstError(TwoDEval areaEval) throws EvaluationException {
    int height = areaEval.getHeight();
    int width = areaEval.getWidth();
    for (int rrIx = 0; rrIx < height; rrIx++) {
        for (int rcIx = 0; rcIx < width; rcIx++) {
            ValueEval ve = areaEval.getValue(rrIx, rcIx);
            if (ve instanceof ErrorEval) {
                throw new EvaluationException((ErrorEval) ve);
            }
        }
    }
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 4 with ErrorEval

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

the class XYNumericFunction method evaluateInternal.

private double evaluateInternal(ValueVector x, ValueVector y, int size) throws EvaluationException {
    Accumulator acc = createAccumulator();
    // error handling is as if the x is fully evaluated before y
    ErrorEval firstXerr = null;
    ErrorEval firstYerr = null;
    boolean accumlatedSome = false;
    double result = 0.0;
    for (int i = 0; i < size; i++) {
        ValueEval vx = x.getItem(i);
        ValueEval vy = y.getItem(i);
        if (vx instanceof ErrorEval) {
            if (firstXerr == null) {
                firstXerr = (ErrorEval) vx;
                continue;
            }
        }
        if (vy instanceof ErrorEval) {
            if (firstYerr == null) {
                firstYerr = (ErrorEval) vy;
                continue;
            }
        }
        // only count pairs if both elements are numbers
        if (vx instanceof NumberEval && vy instanceof NumberEval) {
            accumlatedSome = true;
            NumberEval nx = (NumberEval) vx;
            NumberEval ny = (NumberEval) vy;
            result += acc.accumulate(nx.getNumberValue(), ny.getNumberValue());
        } else {
        // all other combinations of value types are silently ignored
        }
    }
    if (firstXerr != null) {
        throw new EvaluationException(firstXerr);
    }
    if (firstYerr != null) {
        throw new EvaluationException(firstYerr);
    }
    if (!accumlatedSome) {
        throw new EvaluationException(ErrorEval.DIV_ZERO);
    }
    return result;
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 5 with ErrorEval

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

the class Countif method createGeneralMatchPredicate.

/**
     * When the second argument is a string, many things are possible
     */
private static I_MatchPredicate createGeneralMatchPredicate(StringEval stringEval) {
    String value = stringEval.getStringValue();
    CmpOp operator = CmpOp.getOperator(value);
    value = value.substring(operator.getLength());
    Boolean booleanVal = parseBoolean(value);
    if (booleanVal != null) {
        return new BooleanMatcher(booleanVal.booleanValue(), operator);
    }
    Double doubleVal = OperandResolver.parseDouble(value);
    if (doubleVal != null) {
        return new NumberMatcher(doubleVal.doubleValue(), operator);
    }
    ErrorEval ee = parseError(value);
    if (ee != null) {
        return new ErrorMatcher(ee.getErrorCode(), operator);
    }
    //else - just a plain string with no interpretation.
    return new StringMatcher(value, operator);
}
Also used : ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval)

Aggregations

ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)17 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)13 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)8 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)5 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)5 StringEval (org.apache.poi.ss.formula.eval.StringEval)5 Test (org.junit.Test)5 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)2 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)2 EvaluationCell (org.apache.poi.ss.formula.EvaluationCell)1 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)1 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)1 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)1 RefEval (org.apache.poi.ss.formula.eval.RefEval)1 CellValue (org.apache.poi.ss.usermodel.CellValue)1