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