Search in sources :

Example 1 with BoolEval

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

the class EvaluationConditionalFormatRule method checkValue.

/**
     * @param cell the cell to check for
     * @param region for adjusting relative formulas
     * @return if the value of the cell is valid or not for the formatting rule
     */
private boolean checkValue(Cell cell, CellRangeAddress region) {
    if (cell == null || DataValidationEvaluator.isType(cell, CellType.BLANK) || DataValidationEvaluator.isType(cell, CellType.ERROR) || (DataValidationEvaluator.isType(cell, CellType.STRING) && (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty()))) {
        return false;
    }
    ValueEval eval = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ConditionalFormattingEvaluator.getRef(cell), region));
    String f2 = rule.getFormula2();
    ValueEval eval2 = null;
    if (f2 != null && f2.length() > 0) {
        eval2 = unwrapEval(workbookEvaluator.evaluate(f2, ConditionalFormattingEvaluator.getRef(cell), region));
    }
    // we assume the cell has been evaluated, and the current formula value stored
    if (DataValidationEvaluator.isType(cell, CellType.BOOLEAN)) {
        if (eval instanceof BoolEval && (eval2 == null || eval2 instanceof BoolEval)) {
            return operator.isValid(cell.getBooleanCellValue(), ((BoolEval) eval).getBooleanValue(), eval2 == null ? null : ((BoolEval) eval2).getBooleanValue());
        }
        // wrong types
        return false;
    }
    if (DataValidationEvaluator.isType(cell, CellType.NUMERIC)) {
        if (eval instanceof NumberEval && (eval2 == null || eval2 instanceof NumberEval)) {
            return operator.isValid(cell.getNumericCellValue(), ((NumberEval) eval).getNumberValue(), eval2 == null ? null : ((NumberEval) eval2).getNumberValue());
        }
        // wrong types
        return false;
    }
    if (DataValidationEvaluator.isType(cell, CellType.STRING)) {
        if (eval instanceof StringEval && (eval2 == null || eval2 instanceof StringEval)) {
            return operator.isValid(cell.getStringCellValue(), ((StringEval) eval).getStringValue(), eval2 == null ? null : ((StringEval) eval2).getStringValue());
        }
        // wrong types
        return false;
    }
    // should not get here, but in case...
    return false;
}
Also used : BoolEval(org.apache.poi.ss.formula.eval.BoolEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 2 with BoolEval

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

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

Example 4 with BoolEval

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

the class BaseXSSFFormulaEvaluator method evaluateFormulaCellValue.

/**
     * Returns a CellValue wrapper around the supplied ValueEval instance.
     */
protected CellValue evaluateFormulaCellValue(Cell cell) {
    EvaluationCell evalCell = toEvaluationCell(cell);
    ValueEval eval = _bookEvaluator.evaluate(evalCell);
    if (eval instanceof NumberEval) {
        NumberEval ne = (NumberEval) eval;
        return new CellValue(ne.getNumberValue());
    }
    if (eval instanceof BoolEval) {
        BoolEval be = (BoolEval) eval;
        return CellValue.valueOf(be.getBooleanValue());
    }
    if (eval instanceof StringEval) {
        StringEval ne = (StringEval) eval;
        return new CellValue(ne.getStringValue());
    }
    if (eval instanceof ErrorEval) {
        return CellValue.getError(((ErrorEval) eval).getErrorCode());
    }
    throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
}
Also used : BoolEval(org.apache.poi.ss.formula.eval.BoolEval) EvaluationCell(org.apache.poi.ss.formula.EvaluationCell) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) CellValue(org.apache.poi.ss.usermodel.CellValue) StringEval(org.apache.poi.ss.formula.eval.StringEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Aggregations

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