Search in sources :

Example 1 with TwoDEval

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

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

use of org.apache.poi.ss.formula.TwoDEval 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 4 with TwoDEval

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

use of org.apache.poi.ss.formula.TwoDEval in project poi by apache.

the class Sumproduct method evaluateAreaSumProduct.

private static ValueEval evaluateAreaSumProduct(ValueEval[] evalArgs) throws EvaluationException {
    int maxN = evalArgs.length;
    TwoDEval[] args = new TwoDEval[maxN];
    try {
        System.arraycopy(evalArgs, 0, args, 0, maxN);
    } catch (ArrayStoreException e) {
        // one of the other args was not an AreaRef
        return ErrorEval.VALUE_INVALID;
    }
    TwoDEval firstArg = args[0];
    int height = firstArg.getHeight();
    // TODO - junit
    int width = firstArg.getWidth();
    // first check dimensions
    if (!areasAllSameSize(args, height, width)) {
        // but errors in individual cells take precedence
        for (int i = 1; i < args.length; i++) {
            throwFirstError(args[i]);
        }
        return ErrorEval.VALUE_INVALID;
    }
    double acc = 0;
    for (int rrIx = 0; rrIx < height; rrIx++) {
        for (int rcIx = 0; rcIx < width; rcIx++) {
            double term = 1D;
            for (int n = 0; n < maxN; n++) {
                double val = getProductTerm(args[n].getValue(rrIx, rcIx), false);
                term *= val;
            }
            acc += term;
        }
    }
    return new NumberEval(acc);
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Aggregations

TwoDEval (org.apache.poi.ss.formula.TwoDEval)10 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)7 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)7 RefEval (org.apache.poi.ss.formula.eval.RefEval)5 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)4 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)3 ThreeDEval (org.apache.poi.ss.formula.ThreeDEval)1 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)1 StringEval (org.apache.poi.ss.formula.eval.StringEval)1 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)1