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 + ")");
}
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() + ")");
}
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);
}
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");
}
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));
}
Aggregations