Search in sources :

Example 1 with BlankEval

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

the class DStarRunner method fullfillsConditions.

/**
     * Checks a row in a database against a condition database.
     *
     * @param db Database.
     * @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(AreaEval db, int row, AreaEval 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.
    final int height = cdb.getHeight();
    for (int conditionRow = 1; conditionRow < height; ++conditionRow) {
        boolean matches = true;
        final int width = cdb.getWidth();
        for (int column = 0; column < width; ++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;
            // The condition to apply.
            condition = resolveReference(cdb, conditionRow, column);
            // If the condition is empty it matches.
            if (condition instanceof BlankEval)
                continue;
            // The column in the DB to apply the condition to.
            ValueEval targetHeader = resolveReference(cdb, 0, column);
            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 = resolveReference(db, row, getColumnForName(targetHeader, db));
                if (!testNormalCondition(value, condition)) {
                    matches = false;
                    break;
                }
            } else {
                // TODO: Check whether the condition cell contains a formula and return #VALUE! if it doesn't.
                if (OperandResolver.coerceValueToString(condition).isEmpty()) {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                throw new NotImplementedException("D* function with formula conditions");
            }
        }
        if (matches == true) {
            return true;
        }
    }
    return false;
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 2 with BlankEval

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

the class DStarRunner method getColumnForString.

/**
     * For a given database returns the column number for a column heading.
     *
     * @param db Database.
     * @param name Column heading.
     * @return Corresponding column number.
     * @throws EvaluationException If it's not possible to turn all headings into strings.
     */
private static int getColumnForString(AreaEval db, String name) throws EvaluationException {
    int resultColumn = -1;
    final int width = db.getWidth();
    for (int column = 0; column < width; ++column) {
        ValueEval columnNameValueEval = resolveReference(db, 0, column);
        if (columnNameValueEval instanceof BlankEval) {
            continue;
        }
        if (columnNameValueEval instanceof ErrorEval) {
            continue;
        }
        String columnName = OperandResolver.coerceValueToString(columnNameValueEval);
        if (name.equals(columnName)) {
            resultColumn = column;
            break;
        }
    }
    return resultColumn;
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval)

Example 3 with BlankEval

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

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

the class LookupUtils method resolveRangeLookupArg.

/**
	 * Resolves the last (optional) parameter (<b>range_lookup</b>) to the VLOOKUP and HLOOKUP functions.
	 * @param rangeLookupArg must not be <code>null</code>
	 */
public static boolean resolveRangeLookupArg(ValueEval rangeLookupArg, int srcCellRow, int srcCellCol) throws EvaluationException {
    ValueEval valEval = OperandResolver.getSingleValue(rangeLookupArg, srcCellRow, srcCellCol);
    if (valEval instanceof BlankEval) {
        // this does not get the default value
        return false;
    }
    if (valEval instanceof BoolEval) {
        // Happy day flow
        BoolEval boolEval = (BoolEval) valEval;
        return boolEval.getBooleanValue();
    }
    if (valEval instanceof StringEval) {
        String stringValue = ((StringEval) valEval).getStringValue();
        if (stringValue.length() < 1) {
            // Empty string is not the same as BlankEval.  It causes #VALUE! error
            throw EvaluationException.invalidValue();
        }
        // TODO move parseBoolean to OperandResolver
        Boolean b = Countif.parseBoolean(stringValue);
        if (b != null) {
            // string converted to boolean OK
            return b.booleanValue();
        }
        // Excel does not resolve it to a boolean.
        throw EvaluationException.invalidValue();
    // This is in contrast to the code below,, where NumberEvals values (for
    // example 0.01) *do* resolve to equivalent boolean values.
    }
    if (valEval instanceof NumericValueEval) {
        NumericValueEval nve = (NumericValueEval) valEval;
        // zero is FALSE, everything else is TRUE
        return 0.0 != nve.getNumberValue();
    }
    throw new RuntimeException("Unexpected eval type (" + valEval + ")");
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) BoolEval(org.apache.poi.ss.formula.eval.BoolEval) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Aggregations

BlankEval (org.apache.poi.ss.formula.eval.BlankEval)4 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)4 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)3 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)2 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)2 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)1 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)1 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)1 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)1 RefEval (org.apache.poi.ss.formula.eval.RefEval)1 StringEval (org.apache.poi.ss.formula.eval.StringEval)1