Search in sources :

Example 16 with EvaluationException

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

the class Match method evaluateMatchTypeArg.

private static double evaluateMatchTypeArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval match_type = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
    if (match_type instanceof ErrorEval) {
        throw new EvaluationException((ErrorEval) match_type);
    }
    if (match_type instanceof NumericValueEval) {
        NumericValueEval ne = (NumericValueEval) match_type;
        return ne.getNumberValue();
    }
    if (match_type instanceof StringEval) {
        StringEval se = (StringEval) match_type;
        Double d = OperandResolver.parseDouble(se.getStringValue());
        if (d == null) {
            // plain string
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        // if the string parses as a number, it is OK
        return d.doubleValue();
    }
    throw new RuntimeException("Unexpected match_type type (" + match_type.getClass().getName() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 17 with EvaluationException

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

the class MultiOperandNumericFunction method collectValue.

private void collectValue(ValueEval ve, boolean isViaReference, DoubleList temp) throws EvaluationException {
    if (ve == null) {
        throw new IllegalArgumentException("ve must not be null");
    }
    if (ve instanceof BoolEval) {
        if (!isViaReference || _isReferenceBoolCounted) {
            BoolEval boolEval = (BoolEval) ve;
            temp.add(boolEval.getNumberValue());
        }
        return;
    }
    if (ve instanceof NumericValueEval) {
        NumericValueEval ne = (NumericValueEval) ve;
        temp.add(ne.getNumberValue());
        return;
    }
    if (ve instanceof StringValueEval) {
        if (isViaReference) {
            // ignore all ref strings
            return;
        }
        String s = ((StringValueEval) ve).getStringValue();
        Double d = OperandResolver.parseDouble(s);
        if (d == null) {
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        temp.add(d.doubleValue());
        return;
    }
    if (ve instanceof ErrorEval) {
        throw new EvaluationException((ErrorEval) ve);
    }
    if (ve == BlankEval.instance) {
        if (_isBlankCounted) {
            temp.add(0.0);
        }
        return;
    }
    throw new RuntimeException("Invalid ValueEval type passed for conversion: (" + ve.getClass() + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) BoolEval(org.apache.poi.ss.formula.eval.BoolEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 18 with EvaluationException

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

the class Sumproduct method throwFirstError.

private static void throwFirstError(TwoDEval areaEval) throws EvaluationException {
    int height = areaEval.getHeight();
    int width = areaEval.getWidth();
    for (int rrIx = 0; rrIx < height; rrIx++) {
        for (int rcIx = 0; rcIx < width; rcIx++) {
            ValueEval ve = areaEval.getValue(rrIx, rcIx);
            if (ve instanceof ErrorEval) {
                throw new EvaluationException((ErrorEval) ve);
            }
        }
    }
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException)

Example 19 with EvaluationException

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

the class WeekdayFunc method evaluate.

/* for Var1or2ArgFunction:
	@Override
	public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
	}

	@Override
	public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
	}
	*/
/**
	 * Perform WEEKDAY(date, returnOption) function.
	 * Note: Parameter texts are from German EXCEL-2010 help.
	 * Parameters in args[]:
	 *  args[0] serialDate
	 * EXCEL-date value
	 * Standardmaessig ist der 1. Januar 1900 die fortlaufende Zahl 1 und
	 * der 1. Januar 2008 die fortlaufende Zahl 39.448, da dieser Tag nach 39.448 Tagen
	 * auf den 01.01.1900 folgt.
	 * @return Option (optional)
	 * Bestimmt den Rueckgabewert:
		1	oder nicht angegeben Zahl 1 (Sonntag) bis 7 (Samstag). Verhaelt sich wie fruehere Microsoft Excel-Versionen.
		2	Zahl 1 (Montag) bis 7 (Sonntag).
		3	Zahl 0 (Montag) bis 6 (Sonntag).
		11	Die Zahlen 1 (Montag) bis 7 (Sonntag)
		12	Die Zahlen 1 (Dienstag) bis 7 (Montag)
		13	Die Zahlen 1 (Mittwoch) bis 7 (Dienstag)
		14	Die Zahlen 1 (Donnerstag) bis 7 (Mittwoch)
		15	Die Zahlen 1 (Freitag) bis 7 (Donnerstag)
		16	Die Zahlen 1 (Samstag) bis 7 (Freitag)
		17	Die Zahlen 1 (Sonntag) bis 7 (Samstag)
	 */
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
    try {
        if (args.length < 1 || args.length > 2) {
            return ErrorEval.VALUE_INVALID;
        }
        // extract first parameter
        ValueEval serialDateVE = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex);
        double serialDate = OperandResolver.coerceValueToDouble(serialDateVE);
        if (!DateUtil.isValidExcelDate(serialDate)) {
            // EXCEL uses this and no VALUE_ERROR
            return ErrorEval.NUM_ERROR;
        }
        // (XXX 1904-windowing not respected)
        Calendar date = DateUtil.getJavaCalendar(serialDate, false);
        // => sunday = 1, monday = 2, ..., saturday = 7
        int weekday = date.get(Calendar.DAY_OF_WEEK);
        // extract second parameter
        // default value
        int returnOption = 1;
        if (args.length == 2) {
            ValueEval ve = OperandResolver.getSingleValue(args[1], srcRowIndex, srcColumnIndex);
            if (ve == MissingArgEval.instance || ve == BlankEval.instance) {
                // EXCEL uses this and no VALUE_ERROR
                return ErrorEval.NUM_ERROR;
            }
            returnOption = OperandResolver.coerceValueToInt(ve);
            if (returnOption == 2) {
                // both mean the same
                returnOption = 11;
            }
        }
        // if
        // perform calculation
        double result;
        if (returnOption == 1) {
            result = weekday;
        // value 2 is handled above (as value 11)
        } else if (returnOption == 3) {
            result = (weekday + 6 - 1) % 7;
        } else if (returnOption >= 11 && returnOption <= 17) {
            // rotate in the value range 1 to 7
            result = (weekday + 6 - (returnOption - 10)) % 7 + 1;
        } else {
            // EXCEL uses this and no VALUE_ERROR
            return ErrorEval.NUM_ERROR;
        }
        return new NumberEval(result);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
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)

Example 20 with EvaluationException

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

the class XYNumericFunction method evaluateInternal.

private double evaluateInternal(ValueVector x, ValueVector y, int size) throws EvaluationException {
    Accumulator acc = createAccumulator();
    // error handling is as if the x is fully evaluated before y
    ErrorEval firstXerr = null;
    ErrorEval firstYerr = null;
    boolean accumlatedSome = false;
    double result = 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;
            result += acc.accumulate(nx.getNumberValue(), ny.getNumberValue());
        } else {
        // all other combinations of value types are silently ignored
        }
    }
    if (firstXerr != null) {
        throw new EvaluationException(firstXerr);
    }
    if (firstYerr != null) {
        throw new EvaluationException(firstYerr);
    }
    if (!accumlatedSome) {
        throw new EvaluationException(ErrorEval.DIV_ZERO);
    }
    return result;
}
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)

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