Search in sources :

Example 36 with EvaluationException

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

the class EOMonth method evaluate.

@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length != 2) {
        return ErrorEval.VALUE_INVALID;
    }
    try {
        double startDateAsNumber = NumericFunction.singleOperandEvaluate(args[0], ec.getRowIndex(), ec.getColumnIndex());
        int months = (int) NumericFunction.singleOperandEvaluate(args[1], ec.getRowIndex(), ec.getColumnIndex());
        // Excel treats date 0 as 1900-01-00; EOMONTH results in 1900-01-31
        if (startDateAsNumber >= 0.0 && startDateAsNumber < 1.0) {
            startDateAsNumber = 1.0;
        }
        Date startDate = DateUtil.getJavaDate(startDateAsNumber, false);
        Calendar cal = LocaleUtil.getLocaleCalendar();
        cal.setTime(startDate);
        cal.clear(Calendar.HOUR);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);
        cal.add(Calendar.MONTH, months + 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return new NumberEval(DateUtil.getExcelDate(cal.getTime()));
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : Calendar(java.util.Calendar) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) Date(java.util.Date) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 37 with EvaluationException

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

the class LinearRegressionFunction method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
    double result;
    try {
        ValueVector vvY = createValueVector(arg0);
        ValueVector vvX = createValueVector(arg1);
        int size = vvX.getSize();
        if (size == 0 || vvY.getSize() != size) {
            return ErrorEval.NA;
        }
        result = evaluateInternal(vvX, vvY, size);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    if (Double.isNaN(result) || Double.isInfinite(result)) {
        return ErrorEval.NUM_ERROR;
    }
    return new NumberEval(result);
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 38 with EvaluationException

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

the class LinearRegressionFunction method evaluateInternal.

private double evaluateInternal(ValueVector x, ValueVector y, int size) throws EvaluationException {
    // error handling is as if the x is fully evaluated before y
    ErrorEval firstXerr = null;
    ErrorEval firstYerr = null;
    boolean accumlatedSome = false;
    // first pass: read in data, compute xbar and ybar
    double sumx = 0.0, sumy = 0.0;
    for (int i = 0; i < size; i++) {
        ValueEval vx = x.getItem(i);
        ValueEval vy = y.getItem(i);
        if (vx instanceof ErrorEval) {
            if (firstXerr == null) {
                firstXerr = (ErrorEval) vx;
                continue;
            }
        }
        if (vy instanceof ErrorEval) {
            if (firstYerr == null) {
                firstYerr = (ErrorEval) vy;
                continue;
            }
        }
        // only count pairs if both elements are numbers
        if (vx instanceof NumberEval && vy instanceof NumberEval) {
            accumlatedSome = true;
            NumberEval nx = (NumberEval) vx;
            NumberEval ny = (NumberEval) vy;
            sumx += nx.getNumberValue();
            sumy += ny.getNumberValue();
        } else {
        // all other combinations of value types are silently ignored
        }
    }
    double xbar = sumx / size;
    double ybar = sumy / size;
    // second pass: compute summary statistics
    double xxbar = 0.0, xybar = 0.0;
    for (int i = 0; i < size; i++) {
        ValueEval vx = x.getItem(i);
        ValueEval vy = y.getItem(i);
        if (vx instanceof ErrorEval) {
            if (firstXerr == null) {
                firstXerr = (ErrorEval) vx;
                continue;
            }
        }
        if (vy instanceof ErrorEval) {
            if (firstYerr == null) {
                firstYerr = (ErrorEval) vy;
                continue;
            }
        }
        // only count pairs if both elements are numbers
        if (vx instanceof NumberEval && vy instanceof NumberEval) {
            NumberEval nx = (NumberEval) vx;
            NumberEval ny = (NumberEval) vy;
            xxbar += (nx.getNumberValue() - xbar) * (nx.getNumberValue() - xbar);
            xybar += (nx.getNumberValue() - xbar) * (ny.getNumberValue() - ybar);
        } else {
        // all other combinations of value types are silently ignored
        }
    }
    double beta1 = xybar / xxbar;
    double beta0 = ybar - beta1 * xbar;
    if (firstXerr != null) {
        throw new EvaluationException(firstXerr);
    }
    if (firstYerr != null) {
        throw new EvaluationException(firstYerr);
    }
    if (!accumlatedSome) {
        throw new EvaluationException(ErrorEval.DIV_ZERO);
    }
    if (function == FUNCTION.INTERCEPT) {
        return beta0;
    } else {
        return beta1;
    }
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 39 with EvaluationException

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

the class Match method eval.

private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, double match_type) {
    boolean matchExact = match_type == 0;
    // Note - Excel does not strictly require -1 and +1
    boolean findLargestLessThanOrEqual = match_type > 0;
    try {
        ValueEval lookupValue = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
        ValueVector lookupRange = evaluateLookupRange(arg1);
        int index = findIndexOfValue(lookupValue, lookupRange, matchExact, findLargestLessThanOrEqual);
        // +1 to convert to 1-based
        return new NumberEval(index + 1);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : ValueVector(org.apache.poi.ss.formula.functions.LookupUtils.ValueVector) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) 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 40 with EvaluationException

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

the class Match method findIndexOfValue.

/**
	 * @return zero based index
	 */
private static int findIndexOfValue(ValueEval lookupValue, ValueVector lookupRange, boolean matchExact, boolean findLargestLessThanOrEqual) throws EvaluationException {
    LookupValueComparer lookupComparer = createLookupComparer(lookupValue, matchExact);
    int size = lookupRange.getSize();
    if (matchExact) {
        for (int i = 0; i < size; i++) {
            if (lookupComparer.compareTo(lookupRange.getItem(i)).isEqual()) {
                return i;
            }
        }
        throw new EvaluationException(ErrorEval.NA);
    }
    if (findLargestLessThanOrEqual) {
        // Note - backward iteration
        for (int i = size - 1; i >= 0; i--) {
            CompareResult cmp = lookupComparer.compareTo(lookupRange.getItem(i));
            if (cmp.isTypeMismatch()) {
                continue;
            }
            if (!cmp.isLessThan()) {
                return i;
            }
        }
        throw new EvaluationException(ErrorEval.NA);
    }
    // TODO - is binary search used for (match_type==+1) ?
    for (int i = 0; i < size; i++) {
        CompareResult cmp = lookupComparer.compareTo(lookupRange.getItem(i));
        if (cmp.isEqual()) {
            return i;
        }
        if (cmp.isGreaterThan()) {
            if (i < 1) {
                throw new EvaluationException(ErrorEval.NA);
            }
            return i - 1;
        }
    }
    return size - 1;
}
Also used : LookupValueComparer(org.apache.poi.ss.formula.functions.LookupUtils.LookupValueComparer) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) CompareResult(org.apache.poi.ss.formula.functions.LookupUtils.CompareResult)

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