Search in sources :

Example 21 with EvaluationException

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

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

the class Sumif method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
    AreaEval aeRange;
    AreaEval aeSum;
    try {
        aeRange = convertRangeArg(arg0);
        aeSum = createSumRange(arg2, aeRange);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    return eval(srcRowIndex, srcColumnIndex, arg1, aeRange, aeSum);
}
Also used : EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Example 23 with EvaluationException

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

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

the class Indirect method evaluate.

public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length < 1) {
        return ErrorEval.VALUE_INVALID;
    }
    boolean isA1style;
    String text;
    try {
        ValueEval ve = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex());
        text = OperandResolver.coerceValueToString(ve);
        switch(args.length) {
            case 1:
                isA1style = true;
                break;
            case 2:
                isA1style = evaluateBooleanArg(args[1], ec);
                break;
            default:
                return ErrorEval.VALUE_INVALID;
        }
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    return evaluateIndirect(ec, text, isA1style);
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 25 with EvaluationException

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

the class Index method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
    TwoDEval reference = convertFirstArg(arg0);
    int columnIx = 0;
    try {
        int rowIx = resolveIndexArg(arg1, srcRowIndex, srcColumnIndex);
        if (!reference.isColumn()) {
            if (!reference.isRow()) {
                // Note - the type of error changes if the pRowArg is negative
                return ErrorEval.REF_INVALID;
            }
            // When the two-arg version of INDEX() has been invoked and the reference
            // is a single column ref, the row arg seems to get used as the column index
            columnIx = rowIx;
            rowIx = 0;
        }
        return getValueFromArea(reference, rowIx, columnIx);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : TwoDEval(org.apache.poi.ss.formula.TwoDEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Aggregations

EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)50 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)36 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)19 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 StringEval (org.apache.poi.ss.formula.eval.StringEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)7 Calendar (java.util.Calendar)6 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 RefEval (org.apache.poi.ss.formula.eval.RefEval)5 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)3 Date (java.util.Date)2 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)2 BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 AssertionFailedError (junit.framework.AssertionFailedError)1