Search in sources :

Example 11 with EvaluationException

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

the class DateParser method makeDate.

/**
     * @param month 1-based
     */
private static Calendar makeDate(int year, int month, int day) throws EvaluationException {
    if (month < 1 || month > 12) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    Calendar cal = LocaleUtil.getLocaleCalendar(year, month - 1, 1, 0, 0, 0);
    if (day < 1 || day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    cal.set(Calendar.DAY_OF_MONTH, day);
    return cal;
}
Also used : Calendar(java.util.Calendar) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 12 with EvaluationException

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

the class NetworkdaysFunction method evaluate.

/**
     * Evaluate for NETWORKDAYS. Given two dates and a optional date or interval of holidays, determines how many working days are there
     * between those dates.
     * 
     * @return {@link ValueEval} for the number of days between two dates.
     */
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    // NOSONAR
    if (args.length < 2 || args.length > 3) {
        return ErrorEval.VALUE_INVALID;
    }
    int srcCellRow = ec.getRowIndex();
    int srcCellCol = ec.getColumnIndex();
    double start, end;
    double[] holidays;
    try {
        start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol);
        end = this.evaluator.evaluateDateArg(args[1], srcCellRow, srcCellCol);
        if (start > end) {
            return ErrorEval.NAME_INVALID;
        }
        ValueEval holidaysCell = args.length == 3 ? args[2] : null;
        holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol);
        return new NumberEval(WorkdayCalculator.instance.calculateWorkdays(start, end, holidays));
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
}
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 13 with EvaluationException

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

the class WorkdayFunction method evaluate.

/**
     * Evaluate for WORKDAY. Given a date, a number of days and a optional date or interval of holidays, determines which date it is past
     * number of parametrized workdays.
     * 
     * @return {@link ValueEval} with date as its value.
     */
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length < 2 || args.length > 3) {
        return ErrorEval.VALUE_INVALID;
    }
    int srcCellRow = ec.getRowIndex();
    int srcCellCol = ec.getColumnIndex();
    double start;
    int days;
    double[] holidays;
    try {
        start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol);
        days = (int) Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol));
        ValueEval holidaysCell = args.length == 3 ? args[2] : null;
        holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol);
        return new NumberEval(DateUtil.getExcelDate(WorkdayCalculator.instance.calculateWorkdays(start, days, holidays)));
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
}
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 14 with EvaluationException

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

the class LookupUtils method resolveRowOrColIndexArg.

/**
	 * Processes the third argument to VLOOKUP, or HLOOKUP (<b>col_index_num</b>
	 * or <b>row_index_num</b> respectively).<br>
	 * Sample behaviour:
	 *    <table border="0" cellpadding="1" cellspacing="2" summary="Sample behaviour">
	 *      <tr><th>Input&nbsp;&nbsp;&nbsp;Return</th><th>Value&nbsp;&nbsp;</th><th>Thrown Error</th></tr>
	 *      <tr><td>5</td><td>4</td><td>&nbsp;</td></tr>
	 *      <tr><td>2.9</td><td>2</td><td>&nbsp;</td></tr>
	 *      <tr><td>"5"</td><td>4</td><td>&nbsp;</td></tr>
	 *      <tr><td>"2.18e1"</td><td>21</td><td>&nbsp;</td></tr>
	 *      <tr><td>"-$2"</td><td>-3</td><td>*</td></tr>
	 *      <tr><td>FALSE</td><td>-1</td><td>*</td></tr>
	 *      <tr><td>TRUE</td><td>0</td><td>&nbsp;</td></tr>
	 *      <tr><td>"TRUE"</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>"abc"</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>""</td><td>&nbsp;</td><td>#REF!</td></tr>
	 *      <tr><td>&lt;blank&gt;</td><td>&nbsp;</td><td>#VALUE!</td></tr>
	 *    </table><br/>
	 *
	 * Note - out of range errors (result index too high) are handled by the caller.
	 * @return column or row index as a zero-based value, never negative.
	 * @throws EvaluationException when the specified arg cannot be coerced to a non-negative integer
	 */
public static int resolveRowOrColIndexArg(ValueEval rowColIndexArg, int srcCellRow, int srcCellCol) throws EvaluationException {
    if (rowColIndexArg == null) {
        throw new IllegalArgumentException("argument must not be null");
    }
    ValueEval veRowColIndexArg;
    try {
        veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short) srcCellCol);
    } catch (EvaluationException e) {
        // All errors get translated to #REF!
        throw EvaluationException.invalidRef();
    }
    int oneBasedIndex;
    if (veRowColIndexArg instanceof StringEval) {
        StringEval se = (StringEval) veRowColIndexArg;
        String strVal = se.getStringValue();
        Double dVal = OperandResolver.parseDouble(strVal);
        if (dVal == null) {
            // String does not resolve to a number. Raise #REF! error.
            throw EvaluationException.invalidRef();
        // This includes text booleans "TRUE" and "FALSE".  They are not valid.
        }
    // else - numeric value parses OK
    }
    // actual BoolEval values get interpreted as FALSE->0 and TRUE->1
    oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
    if (oneBasedIndex < 1) {
        // note this is asymmetric with the errors when the index is too large (#REF!)
        throw EvaluationException.invalidValue();
    }
    // convert to zero based
    return oneBasedIndex - 1;
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 15 with EvaluationException

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

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