package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.ValueEval;
/**
private int count;
@Override
- public boolean processMatch(ValueEval eval) {
- count++;
+ public boolean processMatch(ValueEval eval, String field) {
+ if (field == null || eval instanceof NumericValueEval) {
+ count++;
+ }
return true;
}
public ValueEval getResult() {
return new NumberEval(count);
}
+
+ @Override
+ public boolean allowEmptyMatchField() {
+ return true;
+ }
}
private ValueEval result;
@Override
- public boolean processMatch(ValueEval eval) {
+ public boolean processMatch(ValueEval eval, String field) {
if(result == null) // First match, just set the value.
{
result = eval;
private ValueEval maximumValue;
@Override
- public boolean processMatch(ValueEval eval) {
+ public boolean processMatch(ValueEval eval, String field) {
if(eval instanceof NumericValueEval) {
if(maximumValue == null) { // First match, just set the value.
maximumValue = eval;
private ValueEval minimumValue;
@Override
- public boolean processMatch(ValueEval eval) {
+ public boolean processMatch(ValueEval eval, String field) {
if(eval instanceof NumericValueEval) {
if(minimumValue == null) { // First match, just set the value.
minimumValue = eval;
this.algoType = algorithm;
}
- public final ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
+ public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
if(args.length == 3) {
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2]);
}
AreaEval db = (AreaEval)database;
AreaEval cdb = (AreaEval)conditionDatabase;
- try {
- filterColumn = OperandResolver.getSingleValue(filterColumn, srcRowIndex, srcColumnIndex);
- } catch (EvaluationException e) {
- return e.getErrorEval();
- }
+ // Create an algorithm runner.
+ final IDStarAlgorithm algorithm = algoType.newInstance();
- int fc;
+ int fc = -1;
+ String field = null;
try {
+ filterColumn = OperandResolver.getSingleValue(filterColumn, srcRowIndex, srcColumnIndex);
fc = getColumnForName(filterColumn, db);
- }
- catch (EvaluationException e) {
- return ErrorEval.VALUE_INVALID;
- }
- if(fc == -1) { // column not found
- return ErrorEval.VALUE_INVALID;
+ if (filterColumn instanceof StringEval) {
+ field = ((StringEval)filterColumn).getStringValue();
+ }
+ if(fc == -1 && !algorithm.allowEmptyMatchField()) {
+ // column not found
+ return ErrorEval.VALUE_INVALID;
+ }
+ } catch (EvaluationException e) {
+ if (!algorithm.allowEmptyMatchField()) {
+ return e.getErrorEval();
+ }
+ } catch (Exception e) {
+ if (!algorithm.allowEmptyMatchField()) {
+ return ErrorEval.VALUE_INVALID;
+ }
}
- // Create an algorithm runner.
- IDStarAlgorithm algorithm = algoType.newInstance();
// Iterate over all DB entries.
final int height = db.getHeight();
if(matches) {
ValueEval currentValueEval = resolveReference(db, row, fc);
// Pass the match to the algorithm and conditionally abort the search.
- boolean shouldContinue = algorithm.processMatch(currentValueEval);
+ boolean shouldContinue = algorithm.processMatch(currentValueEval, field);
if(! shouldContinue) {
break;
}
private double totalValue = 0;
@Override
- public boolean processMatch(ValueEval eval) {
+ public boolean processMatch(ValueEval eval, String field) {
if(eval instanceof NumericValueEval) {
double currentValue = ((NumericValueEval)eval).getNumberValue();
totalValue += currentValue;
/**
* Process a match that is found during a run through a database.
* @param eval ValueEval of the cell in the matching row. References will already be resolved.
+ * @param field the field name (added in POI 5.2.3)
* @return Whether we should continue iterating through the database.
*/
- boolean processMatch(ValueEval eval);
+ boolean processMatch(ValueEval eval, String field);
+
/**
* Return a result ValueEval that will be the result of the calculation.
* This is always called at the end of a run through the database.
* @return a ValueEval
*/
ValueEval getResult();
+
+ /**
+ * Whether the field value (the 2nd param in DCOUNT, DGET, etc.) can evaluate to empty. It
+ * is allowed to evaluate to empty for DCOUNT.
+ * @return whether the field value can evaluate to empty
+ */
+ default boolean allowEmptyMatchField() {
+ return false;
+ }
}
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
- assertDouble(fe, cell, "DCOUNT(A5:E11, \"Age\", A1:A2)", 3);
+ assertDouble(fe, cell, "DCOUNT(A5:E11,,A1:A2)", 3);
+ assertDouble(fe, cell, "DCOUNT(A5:E11, \"Age\", A1:A2)", 2);
//next one returns 0 in error
//assertDouble(fe, cell, "DCOUNT(A5:E11, \"Age\", A1:F2)", 1);
}