Search in sources :

Example 16 with ValueEval

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

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

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

the class YearFrac method evaluateDateArg.

private static double evaluateDateArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol);
    if (ve instanceof StringEval) {
        String strVal = ((StringEval) ve).getStringValue();
        Double dVal = OperandResolver.parseDouble(strVal);
        if (dVal != null) {
            return dVal.doubleValue();
        }
        Calendar date = DateParser.parseDate(strVal);
        return DateUtil.getExcelDate(date, false);
    }
    return OperandResolver.coerceValueToDouble(ve);
}
Also used : Calendar(java.util.Calendar) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Example 19 with ValueEval

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

the class ArgumentsEvaluator method evaluateDateArg.

/**
     * Evaluate a generic {@link ValueEval} argument to a double value that represents a date in POI.
     * 
     * @param arg {@link ValueEval} an argument.
     * @param srcCellRow number cell row.
     * @param srcCellCol number cell column.
     * @return a double representing a date in POI.
     * @throws EvaluationException exception upon argument evaluation.
     */
public double evaluateDateArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol);
    if (ve instanceof StringEval) {
        String strVal = ((StringEval) ve).getStringValue();
        Double dVal = OperandResolver.parseDouble(strVal);
        if (dVal != null) {
            return dVal.doubleValue();
        }
        Calendar date = DateParser.parseDate(strVal);
        return DateUtil.getExcelDate(date, false);
    }
    return OperandResolver.coerceValueToDouble(ve);
}
Also used : Calendar(java.util.Calendar) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Example 20 with ValueEval

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

the class TestProper method checkProper.

private void checkProper(String input, String expected) {
    ValueEval strArg = new StringEval(input);
    final ValueEval ret = TextFunction.PROPER.evaluate(new ValueEval[] { strArg }, 0, 0);
    assertEquals(expected, ((StringEval) ret).getStringValue());
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Aggregations

ValueEval (org.apache.poi.ss.formula.eval.ValueEval)223 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)101 StringEval (org.apache.poi.ss.formula.eval.StringEval)48 Test (org.junit.Test)39 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)36 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)29 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)28 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)27 Calendar (java.util.Calendar)16 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)13 Date (java.util.Date)11 RefEval (org.apache.poi.ss.formula.eval.RefEval)9 TwoDEval (org.apache.poi.ss.formula.TwoDEval)7 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)4 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)4 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)4 ValueVector (org.apache.poi.ss.formula.functions.LookupUtils.ValueVector)4 ArrayList (java.util.ArrayList)3 AssertionFailedError (junit.framework.AssertionFailedError)3 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3