Search in sources :

Example 1 with StringValueEval

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

use of org.apache.poi.ss.formula.eval.StringValueEval 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)

Aggregations

EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)2 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)2 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)2 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)1 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 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)1