Search in sources :

Example 41 with EvaluationException

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

the class IPMT method eval.

@Override
public double eval(ValueEval[] args, int srcCellRow, int srcCellCol) throws EvaluationException {
    if (args.length != 4)
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    double result;
    ValueEval v1 = OperandResolver.getSingleValue(args[0], srcCellRow, srcCellCol);
    ValueEval v2 = OperandResolver.getSingleValue(args[1], srcCellRow, srcCellCol);
    ValueEval v3 = OperandResolver.getSingleValue(args[2], srcCellRow, srcCellCol);
    ValueEval v4 = OperandResolver.getSingleValue(args[3], srcCellRow, srcCellCol);
    double interestRate = OperandResolver.coerceValueToDouble(v1);
    int period = OperandResolver.coerceValueToInt(v2);
    int numberPayments = OperandResolver.coerceValueToInt(v3);
    double PV = OperandResolver.coerceValueToDouble(v4);
    result = Finance.ipmt(interestRate, period, numberPayments, PV);
    checkValue(result);
    return result;
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 42 with EvaluationException

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

the class Index method resolveIndexArg.

/**
	 * @param arg a 1-based index.
	 * @return the resolved 1-based index. Zero if the arg was missing or blank
	 * @throws EvaluationException if the arg is an error value evaluates to a negative numeric value
	 */
private static int resolveIndexArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval ev = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
    if (ev == MissingArgEval.instance) {
        return 0;
    }
    if (ev == BlankEval.instance) {
        return 0;
    }
    int result = OperandResolver.coerceValueToInt(ev);
    if (result < 0) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    return result;
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 43 with EvaluationException

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

the class Value method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
    ValueEval veText;
    try {
        veText = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    String strText = OperandResolver.coerceValueToString(veText);
    Double result = convertTextToNumber(strText);
    if (result == null) {
        return ErrorEval.VALUE_INVALID;
    }
    return new NumberEval(result.doubleValue());
}
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 44 with EvaluationException

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

the class Vlookup method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval lookup_value, ValueEval table_array, ValueEval col_index, ValueEval range_lookup) {
    try {
        // Evaluation order:
        // lookup_value , table_array, range_lookup, find lookup value, col_index, fetch result
        ValueEval lookupValue = OperandResolver.getSingleValue(lookup_value, srcRowIndex, srcColumnIndex);
        TwoDEval tableArray = LookupUtils.resolveTableArrayArg(table_array);
        boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(range_lookup, srcRowIndex, srcColumnIndex);
        int rowIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createColumnVector(tableArray, 0), isRangeLookup);
        int colIndex = LookupUtils.resolveRowOrColIndexArg(col_index, srcRowIndex, srcColumnIndex);
        ValueVector resultCol = createResultColumnVector(tableArray, colIndex);
        return resultCol.getItem(rowIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) TwoDEval(org.apache.poi.ss.formula.TwoDEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 45 with EvaluationException

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

the class WeekNum method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE) {
    double serialNum;
    try {
        serialNum = NumericFunction.singleOperandEvaluate(serialNumVE, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
    Calendar serialNumCalendar = LocaleUtil.getLocaleCalendar();
    serialNumCalendar.setTime(DateUtil.getJavaDate(serialNum, false));
    int returnType;
    try {
        ValueEval ve = OperandResolver.getSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex);
        returnType = OperandResolver.coerceValueToInt(ve);
    } catch (EvaluationException e) {
        return ErrorEval.NUM_ERROR;
    }
    if (returnType != 1 && returnType != 2) {
        return ErrorEval.NUM_ERROR;
    }
    return new NumberEval(this.getWeekNo(serialNumCalendar, returnType));
}
Also used : Calendar(java.util.Calendar) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

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