Search in sources :

Example 31 with EvaluationException

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

the class DateFunc method evaluate.

/**
	 * Note - works with Java Calendar months, not Excel months
	 */
private static double evaluate(int year, int month, int pDay) throws EvaluationException {
    // We don't support negative years yet
    if (year < 0) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    // Negative months are fairly easy
    while (month < 0) {
        year--;
        month += 12;
    }
    // Special case for the non-existant 1900 leap year
    if (year == 1900 && month == Calendar.FEBRUARY && pDay == 29) {
        return 60.0;
    }
    // If they give a date in 1900 in Jan/Feb, with the days
    //  putting it past the leap year, adjust
    int day = pDay;
    if (year == 1900) {
        if ((month == Calendar.JANUARY && day >= 60) || (month == Calendar.FEBRUARY && day >= 30)) {
            day--;
        }
    }
    // Turn this into a Java date
    Calendar c = LocaleUtil.getLocaleCalendar(year, month, day);
    //  the 29th of Feb 1900
    if (pDay < 0 && c.get(Calendar.YEAR) == 1900 && month > Calendar.FEBRUARY && c.get(Calendar.MONTH) < Calendar.MARCH) {
        c.add(Calendar.DATE, 1);
    }
    // TODO Identify if we're doing 1900 or 1904 date windowing
    boolean use1904windowing = false;
    // Have this Java date turned back into an Excel one
    return DateUtil.getExcelDate(c.getTime(), use1904windowing);
}
Also used : Calendar(java.util.Calendar) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 32 with EvaluationException

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

the class Dec2Bin method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval numberVE, ValueEval placesVE) {
    ValueEval veText1;
    try {
        veText1 = OperandResolver.getSingleValue(numberVE, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    String strText1 = OperandResolver.coerceValueToString(veText1);
    Double number = OperandResolver.parseDouble(strText1);
    //If this number argument is non numeric, this function returns the #VALUE! error value.
    if (number == null) {
        return ErrorEval.VALUE_INVALID;
    }
    //If number < -512 or if number > 512, this function returns the #NUM! error value.
    if (number.longValue() < MIN_VALUE || number.longValue() > MAX_VALUE) {
        return ErrorEval.NUM_ERROR;
    }
    int placesNumber;
    if (number < 0 || placesVE == null) {
        placesNumber = DEFAULT_PLACES_VALUE;
    } else {
        ValueEval placesValueEval;
        try {
            placesValueEval = OperandResolver.getSingleValue(placesVE, srcRowIndex, srcColumnIndex);
        } catch (EvaluationException e) {
            return e.getErrorEval();
        }
        String placesStr = OperandResolver.coerceValueToString(placesValueEval);
        Double placesNumberDouble = OperandResolver.parseDouble(placesStr);
        //non numeric value
        if (placesNumberDouble == null) {
            return ErrorEval.VALUE_INVALID;
        }
        //If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.
        placesNumber = placesNumberDouble.intValue();
        if (placesNumber < 0 || placesNumber == 0) {
            return ErrorEval.NUM_ERROR;
        }
    }
    String binary = Integer.toBinaryString(number.intValue());
    if (binary.length() > DEFAULT_PLACES_VALUE) {
        binary = binary.substring(binary.length() - DEFAULT_PLACES_VALUE, binary.length());
    }
    //If DEC2BIN requires more than places characters, it returns the #NUM! error value.
    if (binary.length() > placesNumber) {
        return ErrorEval.NUM_ERROR;
    }
    return new StringEval(binary);
}
Also used : 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 33 with EvaluationException

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

the class EDate method evaluate.

public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length != 2) {
        return ErrorEval.VALUE_INVALID;
    }
    try {
        double startDateAsNumber = getValue(args[0]);
        int offsetInMonthAsNumber = (int) getValue(args[1]);
        Date startDate = DateUtil.getJavaDate(startDateAsNumber);
        Calendar calendar = LocaleUtil.getLocaleCalendar();
        calendar.setTime(startDate);
        calendar.add(Calendar.MONTH, offsetInMonthAsNumber);
        return new NumberEval(DateUtil.getExcelDate(calendar.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 34 with EvaluationException

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

the class EDate method getValue.

private double getValue(ValueEval arg) throws EvaluationException {
    if (arg instanceof NumberEval) {
        return ((NumberEval) arg).getNumberValue();
    }
    if (arg instanceof BlankEval) {
        return 0;
    }
    if (arg instanceof RefEval) {
        RefEval refEval = (RefEval) arg;
        if (refEval.getNumberOfSheets() > 1) {
            // Multi-Sheet references are not supported
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        ValueEval innerValueEval = refEval.getInnerValueEval(refEval.getFirstSheetIndex());
        if (innerValueEval instanceof NumberEval) {
            return ((NumberEval) innerValueEval).getNumberValue();
        }
        if (innerValueEval instanceof BlankEval) {
            return 0;
        }
    }
    throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
Also used : RefEval(org.apache.poi.ss.formula.eval.RefEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) 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 35 with EvaluationException

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

the class Days360 method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
    try {
        double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex);
        double d1 = NumericFunction.singleOperandEvaluate(arg1, srcRowIndex, srcColumnIndex);
        ValueEval ve = OperandResolver.getSingleValue(arg2, srcRowIndex, srcColumnIndex);
        Boolean method = OperandResolver.coerceValueToBoolean(ve, false);
        return new NumberEval(evaluate(d0, d1, method != null && method.booleanValue()));
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
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)

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