Search in sources :

Example 11 with NumberEval

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

the class Rate method evaluate.

public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
    if (args.length < 3) {
        //First 3 parameters are mandatory
        return ErrorEval.VALUE_INVALID;
    }
    double periods, payment, present_val, future_val = 0, type = 0, estimate = 0.1, rate;
    try {
        ValueEval v1 = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex);
        ValueEval v2 = OperandResolver.getSingleValue(args[1], srcRowIndex, srcColumnIndex);
        ValueEval v3 = OperandResolver.getSingleValue(args[2], srcRowIndex, srcColumnIndex);
        ValueEval v4 = null;
        if (args.length >= 4)
            v4 = OperandResolver.getSingleValue(args[3], srcRowIndex, srcColumnIndex);
        ValueEval v5 = null;
        if (args.length >= 5)
            v5 = OperandResolver.getSingleValue(args[4], srcRowIndex, srcColumnIndex);
        ValueEval v6 = null;
        if (args.length >= 6)
            v6 = OperandResolver.getSingleValue(args[5], srcRowIndex, srcColumnIndex);
        periods = OperandResolver.coerceValueToDouble(v1);
        payment = OperandResolver.coerceValueToDouble(v2);
        present_val = OperandResolver.coerceValueToDouble(v3);
        if (args.length >= 4)
            future_val = OperandResolver.coerceValueToDouble(v4);
        if (args.length >= 5)
            type = OperandResolver.coerceValueToDouble(v5);
        if (args.length >= 6)
            estimate = OperandResolver.coerceValueToDouble(v6);
        rate = calculateRate(periods, payment, present_val, future_val, type, estimate);
        checkValue(rate);
    } catch (EvaluationException e) {
        LOG.log(POILogger.ERROR, "Can't evaluate rate function", e);
        return e.getErrorEval();
    }
    return new NumberEval(rate);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 12 with NumberEval

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

the class Sumif method eval.

private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg1, AreaEval aeRange, AreaEval aeSum) {
    // TODO - junit to prove last arg must be srcColumnIndex and not srcRowIndex
    I_MatchPredicate mp = Countif.createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex);
    // handle empty cells
    if (mp == null) {
        return NumberEval.ZERO;
    }
    double result = sumMatchingCells(aeRange, mp, aeSum);
    return new NumberEval(result);
}
Also used : I_MatchPredicate(org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 13 with NumberEval

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

Example 14 with NumberEval

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

the class TestSubtotal method confirmSubtotal.

private static void confirmSubtotal(int function, double expected) {
    ValueEval[] values = new ValueEval[TEST_VALUES0.length];
    for (int i = 0; i < TEST_VALUES0.length; i++) {
        values[i] = new NumberEval(TEST_VALUES0[i]);
    }
    AreaEval arg1 = EvalFactory.createAreaEval("C1:D5", values);
    ValueEval[] args = { new NumberEval(function), arg1 };
    ValueEval result = new Subtotal().evaluate(args, 0, 0);
    assertEquals(NumberEval.class, result.getClass());
    assertEquals(expected, ((NumberEval) result).getNumberValue(), 0.0);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 15 with NumberEval

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

the class TestSumif method testCriteriaArgRange.

/**
	 * test for bug observed near svn r882931
	 */
public void testCriteriaArgRange() {
    ValueEval[] arg0values = new ValueEval[] { _50, _60, _50, _50, _50, _30 };
    ValueEval[] arg1values = new ValueEval[] { _30, _40, _50, _60 };
    AreaEval arg0;
    AreaEval arg1;
    ValueEval ve;
    arg0 = EvalFactory.createAreaEval("A3:B5", arg0values);
    // single row range
    arg1 = EvalFactory.createAreaEval("A2:D2", arg1values);
    // invoking from cell C1
    ve = invokeSumif(0, 2, arg0, arg1);
    if (ve instanceof NumberEval) {
        NumberEval ne = (NumberEval) ve;
        if (ne.getNumberValue() == 30.0) {
            throw new AssertionFailedError("identified error in SUMIF - criteria arg not evaluated properly");
        }
    }
    confirmDouble(200, ve);
    arg0 = EvalFactory.createAreaEval("C1:D3", arg0values);
    // single column range
    arg1 = EvalFactory.createAreaEval("B1:B4", arg1values);
    // invoking from cell A4
    ve = invokeSumif(3, 0, arg0, arg1);
    confirmDouble(60, ve);
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) AssertionFailedError(junit.framework.AssertionFailedError) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Aggregations

NumberEval (org.apache.poi.ss.formula.eval.NumberEval)123 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)101 Test (org.junit.Test)34 StringEval (org.apache.poi.ss.formula.eval.StringEval)29 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)21 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)19 Calendar (java.util.Calendar)17 Date (java.util.Date)13 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)12 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)5 RefEval (org.apache.poi.ss.formula.eval.RefEval)4 AssertionFailedError (junit.framework.AssertionFailedError)3 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 SimpleDateFormat (java.text.SimpleDateFormat)1