Search in sources :

Example 1 with RefEval

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

the class BooleanFunction method calculate.

private boolean calculate(ValueEval[] args) throws EvaluationException {
    boolean result = getInitialResultValue();
    boolean atleastOneNonBlank = false;
    /*
		 * Note: no short-circuit boolean loop exit because any ErrorEvals will override the result
		 */
    for (final ValueEval arg : args) {
        Boolean tempVe;
        if (arg instanceof TwoDEval) {
            TwoDEval ae = (TwoDEval) arg;
            int height = ae.getHeight();
            int width = ae.getWidth();
            for (int rrIx = 0; rrIx < height; rrIx++) {
                for (int rcIx = 0; rcIx < width; rcIx++) {
                    ValueEval ve = ae.getValue(rrIx, rcIx);
                    tempVe = OperandResolver.coerceValueToBoolean(ve, true);
                    if (tempVe != null) {
                        result = partialEvaluate(result, tempVe.booleanValue());
                        atleastOneNonBlank = true;
                    }
                }
            }
            continue;
        }
        if (arg instanceof RefEval) {
            RefEval re = (RefEval) arg;
            final int firstSheetIndex = re.getFirstSheetIndex();
            final int lastSheetIndex = re.getLastSheetIndex();
            for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) {
                ValueEval ve = re.getInnerValueEval(sIx);
                tempVe = OperandResolver.coerceValueToBoolean(ve, true);
                if (tempVe != null) {
                    result = partialEvaluate(result, tempVe.booleanValue());
                    atleastOneNonBlank = true;
                }
            }
            continue;
        }
        if (arg == MissingArgEval.instance) {
            // you can leave out parameters, they are simply ignored
            tempVe = null;
        } else {
            tempVe = OperandResolver.coerceValueToBoolean(arg, false);
        }
        if (tempVe != null) {
            result = partialEvaluate(result, tempVe.booleanValue());
            atleastOneNonBlank = true;
        }
    }
    if (!atleastOneNonBlank) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    return result;
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 2 with RefEval

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

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

the class Mode method collectValues.

private static void collectValues(ValueEval arg, List<Double> temp) throws EvaluationException {
    if (arg instanceof TwoDEval) {
        TwoDEval ae = (TwoDEval) arg;
        int width = ae.getWidth();
        int height = ae.getHeight();
        for (int rrIx = 0; rrIx < height; rrIx++) {
            for (int rcIx = 0; rcIx < width; rcIx++) {
                ValueEval ve1 = ae.getValue(rrIx, rcIx);
                collectValue(ve1, temp, false);
            }
        }
        return;
    }
    if (arg instanceof RefEval) {
        RefEval re = (RefEval) arg;
        final int firstSheetIndex = re.getFirstSheetIndex();
        final int lastSheetIndex = re.getLastSheetIndex();
        for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) {
            collectValue(re.getInnerValueEval(sIx), temp, true);
        }
        return;
    }
    collectValue(arg, temp, true);
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval)

Example 4 with RefEval

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

the class MultiOperandNumericFunction method collectValues.

/**
	 * Collects values from a single argument
	 */
private void collectValues(ValueEval operand, DoubleList temp) throws EvaluationException {
    if (operand instanceof ThreeDEval) {
        ThreeDEval ae = (ThreeDEval) operand;
        for (int sIx = ae.getFirstSheetIndex(); sIx <= ae.getLastSheetIndex(); sIx++) {
            int width = ae.getWidth();
            int height = ae.getHeight();
            for (int rrIx = 0; rrIx < height; rrIx++) {
                for (int rcIx = 0; rcIx < width; rcIx++) {
                    ValueEval ve = ae.getValue(sIx, rrIx, rcIx);
                    if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
                        continue;
                    collectValue(ve, true, temp);
                }
            }
        }
        return;
    }
    if (operand instanceof TwoDEval) {
        TwoDEval ae = (TwoDEval) operand;
        int width = ae.getWidth();
        int height = ae.getHeight();
        for (int rrIx = 0; rrIx < height; rrIx++) {
            for (int rcIx = 0; rcIx < width; rcIx++) {
                ValueEval ve = ae.getValue(rrIx, rcIx);
                if (!isSubtotalCounted() && ae.isSubTotal(rrIx, rcIx))
                    continue;
                collectValue(ve, true, temp);
            }
        }
        return;
    }
    if (operand instanceof RefEval) {
        RefEval re = (RefEval) operand;
        for (int sIx = re.getFirstSheetIndex(); sIx <= re.getLastSheetIndex(); sIx++) {
            collectValue(re.getInnerValueEval(sIx), true, temp);
        }
        return;
    }
    collectValue(operand, false, temp);
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) ThreeDEval(org.apache.poi.ss.formula.ThreeDEval) RefEval(org.apache.poi.ss.formula.eval.RefEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 5 with RefEval

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

Aggregations

RefEval (org.apache.poi.ss.formula.eval.RefEval)12 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)5 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)5 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)5 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)4 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)3 StringEval (org.apache.poi.ss.formula.eval.StringEval)3 ThreeDEval (org.apache.poi.ss.formula.ThreeDEval)1 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)1 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)1 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)1 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)1