Search in sources :

Example 41 with StringEval

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

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

Example 43 with StringEval

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

the class TestCountFuncs method testCountifAreaCriteria.

/**
	 * the criteria arg is mostly handled by {@link OperandResolver#getSingleValue(org.apache.poi.ss.formula.eval.ValueEval, int, int)}}
	 */
public void testCountifAreaCriteria() {
    // anything but column A
    int srcColIx = 2;
    ValueEval v0 = new NumberEval(2.0);
    ValueEval v1 = new StringEval("abc");
    ValueEval v2 = ErrorEval.DIV_ZERO;
    AreaEval ev = EvalFactory.createAreaEval("A10:A12", new ValueEval[] { v0, v1, v2 });
    I_MatchPredicate mp;
    mp = Countif.createCriteriaPredicate(ev, 9, srcColIx);
    confirmPredicate(true, mp, srcColIx);
    confirmPredicate(false, mp, "abc");
    confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
    mp = Countif.createCriteriaPredicate(ev, 10, srcColIx);
    confirmPredicate(false, mp, srcColIx);
    confirmPredicate(true, mp, "abc");
    confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
    mp = Countif.createCriteriaPredicate(ev, 11, srcColIx);
    confirmPredicate(false, mp, srcColIx);
    confirmPredicate(false, mp, "abc");
    confirmPredicate(true, mp, ErrorEval.DIV_ZERO);
    confirmPredicate(false, mp, ErrorEval.VALUE_INVALID);
    // tricky: indexing outside of A10:A12
    // even this #VALUE! error gets used by COUNTIF as valid criteria
    mp = Countif.createCriteriaPredicate(ev, 12, srcColIx);
    confirmPredicate(false, mp, srcColIx);
    confirmPredicate(false, mp, "abc");
    confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
    confirmPredicate(true, mp, ErrorEval.VALUE_INVALID);
}
Also used : I_MatchPredicate(org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 44 with StringEval

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

the class TestCountFuncs method testNotQuiteWildCards.

public void testNotQuiteWildCards() {
    I_MatchPredicate mp;
    // make sure special reg-ex chars are treated like normal chars
    mp = createCriteriaPredicate(new StringEval("a.b"));
    confirmPredicate(false, mp, "aab");
    confirmPredicate(true, mp, "a.b");
    mp = createCriteriaPredicate(new StringEval("a~b"));
    confirmPredicate(false, mp, "ab");
    confirmPredicate(false, mp, "axb");
    confirmPredicate(false, mp, "a~~b");
    confirmPredicate(true, mp, "a~b");
    mp = createCriteriaPredicate(new StringEval(">a*b"));
    confirmPredicate(false, mp, "a(b");
    confirmPredicate(true, mp, "aab");
    confirmPredicate(false, mp, "a*a");
    confirmPredicate(true, mp, "a*c");
}
Also used : I_MatchPredicate(org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Example 45 with StringEval

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

the class TestCountFuncs method confirmPredicate.

private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, String value) {
    ValueEval ev = value == null ? BlankEval.instance : new StringEval(value);
    assertEquals(expectedResult, matchPredicate.matches(ev));
}
Also used : ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval)

Aggregations

StringEval (org.apache.poi.ss.formula.eval.StringEval)58 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)48 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)29 Test (org.junit.Test)18 AreaEval (org.apache.poi.ss.formula.eval.AreaEval)13 Calendar (java.util.Calendar)10 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)10 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)9 Date (java.util.Date)8 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)7 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)5 BoolEval (org.apache.poi.ss.formula.eval.BoolEval)3 RefEval (org.apache.poi.ss.formula.eval.RefEval)3 BigDecimal (java.math.BigDecimal)1 DateFormatSymbols (java.text.DateFormatSymbols)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 NumberFormat (java.text.NumberFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1