Example usage for org.apache.poi.ss.formula.eval EvaluationException EvaluationException

List of usage examples for org.apache.poi.ss.formula.eval EvaluationException EvaluationException

Introduction

In this page you can find the example usage for org.apache.poi.ss.formula.eval EvaluationException EvaluationException.

Prototype

public EvaluationException(ErrorEval errorEval) 

Source Link

Usage

From source file:com.iesvirgendelcarmen.acceso.tema01.CalculateMortgage.java

License:Apache License

/**
 * Excel does not support infinities and NaNs, rather, it gives a #NUM! error in these cases
 *
 * @throws EvaluationException (#NUM!) if <tt>result</tt> is <tt>NaN</> or <tt>Infinity</tt>
 *///  w  ww  .ja  v a  2s  . co m
private void checkValue(double result) throws EvaluationException {
    if (Double.isNaN(result) || Double.isInfinite(result)) {
        throw new EvaluationException(ErrorEval.NUM_ERROR);
    }
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Resolve reference(-chains) until we have a normal value.
 *
 * @param field a ValueEval which can be a RefEval.
 * @return a ValueEval which is guaranteed not to be a RefEval
 * @throws EvaluationException If a multi-sheet reference was found along the way.
 *//* ww  w .  j ava2s  .c om*/
private static ValueEval solveReference(ValueEval field) throws EvaluationException {
    if (field instanceof RefEval) {
        RefEval refEval = (RefEval) field;
        if (refEval.getNumberOfSheets() > 1) {
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
        return solveReference(refEval.getInnerValueEval(refEval.getFirstSheetIndex()));
    } else {
        return field;
    }
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Returns the first column index that matches the given name. The name can either be
 * a string or an integer, when it's an integer, then the respective column
 * (1 based index) is returned./*from  w  w w  . j a  v  a 2s  .  c o m*/
 * @param nameValueEval
 * @param db
 * @return the first column index that matches the given name (or int)
 * @throws EvaluationException
 */
@SuppressWarnings("unused")
private static int getColumnForTag(ValueEval nameValueEval, TwoDEval db) throws EvaluationException {
    int resultColumn = -1;

    // Numbers as column indicator are allowed, check that.
    if (nameValueEval instanceof NumericValueEval) {
        double doubleResultColumn = ((NumericValueEval) nameValueEval).getNumberValue();
        resultColumn = (int) doubleResultColumn;
        // Floating comparisions are usually not possible, but should work for 0.0.
        if (doubleResultColumn - resultColumn != 0.0)
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        resultColumn -= 1; // Numbers are 1-based not 0-based.
    } else {
        resultColumn = getColumnForName(nameValueEval, db);
    }
    return resultColumn;
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Checks a row in a database against a condition database.
 *
 * @param db Database.// ww  w  .  j  ava  2s  .  c o m
 * @param row The row in the database to check.
 * @param cdb The condition database to use for checking.
 * @return Whether the row matches the conditions.
 * @throws EvaluationException If references could not be resolved or comparison
 * operators and operands didn't match.
 */
private static boolean fullfillsConditions(TwoDEval db, int row, TwoDEval cdb) throws EvaluationException {
    // Only one row must match to accept the input, so rows are ORed.
    // Each row is made up of cells where each cell is a condition,
    // all have to match, so they are ANDed.
    for (int conditionRow = 1; conditionRow < cdb.getHeight(); ++conditionRow) {
        boolean matches = true;
        for (int column = 0; column < cdb.getWidth(); ++column) { // columns are ANDed
            // Whether the condition column matches a database column, if not it's a
            // special column that accepts formulas.
            boolean columnCondition = true;
            ValueEval condition = null;
            try {
                // The condition to apply.
                condition = solveReference(cdb.getValue(conditionRow, column));
            } catch (java.lang.RuntimeException e) {
                // It might be a special formula, then it is ok if it fails.
                columnCondition = false;
            }
            // If the condition is empty it matches.
            if (condition instanceof BlankEval)
                continue;
            // The column in the DB to apply the condition to.
            ValueEval targetHeader = solveReference(cdb.getValue(0, column));
            targetHeader = solveReference(targetHeader);

            if (!(targetHeader instanceof StringValueEval)) {
                throw new EvaluationException(ErrorEval.VALUE_INVALID);
            }

            if (getColumnForName(targetHeader, db) == -1)
                // No column found, it's again a special column that accepts formulas.
                columnCondition = false;

            if (columnCondition == true) { // normal column condition
                // Should not throw, checked above.
                ValueEval value = db.getValue(row, getColumnForName(targetHeader, db));
                if (!testNormalCondition(value, condition)) {
                    matches = false;
                    break;
                }
            } else { // It's a special formula condition.
                if (getStringFromValueEval(condition).isEmpty()) {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                throw new NotImplementedException("D* function with formula conditions");
            }
        }
        if (matches == true) {
            return true;
        }
    }
    return false;
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Test whether a value matches a numeric condition.
 * @param valueEval Value to check.// w ww  .j  a  va  2s .  c  om
 * @param op Comparator to use.
 * @param condition Value to check against.
 * @return whether the condition holds.
 * @throws EvaluationException If it's impossible to turn the condition into a number.
 */
private static boolean testNumericCondition(ValueEval valueEval, operator op, String condition)
        throws EvaluationException {
    // Construct double from ValueEval.
    if (!(valueEval instanceof NumericValueEval))
        return false;
    double value = ((NumericValueEval) valueEval).getNumberValue();

    // Construct double from condition.
    double conditionValue = 0.0;
    try {
        int intValue = Integer.parseInt(condition);
        conditionValue = intValue;
    } catch (NumberFormatException e) { // It's not an int.
        try {
            conditionValue = Double.parseDouble(condition);
        } catch (NumberFormatException e2) { // It's not a double.
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
    }

    int result = NumberComparer.compare(value, conditionValue);
    switch (op) {
    case largerThan:
        return result > 0;
    case largerEqualThan:
        return result >= 0;
    case smallerThan:
        return result < 0;
    case smallerEqualThan:
        return result <= 0;
    case equal:
        return result == 0;
    }
    return false; // Can not be reached.
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Takes a ValueEval and tries to retrieve a String value from it.
 * It tries to resolve references if there are any.
 *
 * @param value ValueEval to retrieve the string from.
 * @return String corresponding to the given ValueEval.
 * @throws EvaluationException If it's not possible to retrieve a String value.
 *//*from  ww  w  . jav a2s  .c  om*/
private static String getStringFromValueEval(ValueEval value) throws EvaluationException {
    value = solveReference(value);
    if (!(value instanceof StringValueEval))
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    return ((StringValueEval) value).getStringValue();
}