View | Details | Raw Unified | Return to bug 48044
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java (+1 lines)
Lines 203-208 Link Here
203
203
204
		retval[345] = new Sumif();
204
		retval[345] = new Sumif();
205
		retval[346] = new Countif();
205
		retval[346] = new Countif();
206
		retval[347] = new Countblank();
206
207
207
		retval[359] = new Hyperlink();
208
		retval[359] = new Hyperlink();
208
209
(-)src/java/org/apache/poi/hssf/record/formula/functions/Countblank.java (+73 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.hssf.record.formula.functions;
19
20
import org.apache.poi.hssf.record.formula.eval.AreaEval;
21
import org.apache.poi.hssf.record.formula.eval.BlankEval;
22
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
23
import org.apache.poi.hssf.record.formula.eval.NumberEval;
24
import org.apache.poi.hssf.record.formula.eval.RefEval;
25
import org.apache.poi.hssf.record.formula.eval.ValueEval;
26
import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
27
28
/**
29
 * Implementation for the function COUNTBLANK
30
 * <p>
31
 *  Syntax: COUNTBLANK ( range )
32
 *    <table border="0" cellpadding="1" cellspacing="0" summary="Parameter descriptions">
33
 *      <tr><th>range&nbsp;&nbsp;&nbsp;</th><td>is the range of cells to count blanks</td></tr>
34
 *    </table>
35
 * </p>
36
 *
37
 * @author Mads Mohr Christensen
38
 */
39
public final class Countblank implements Function {
40
41
	public ValueEval evaluate(ValueEval[] args, int srcRowIndex, short srcColumnIndex) {
42
		switch(args.length) {
43
		case 1:
44
			// expected
45
			break;
46
		default:
47
			// TODO - it doesn't seem to be possible to enter COUNTBLANK() into Excel with the wrong arg count
48
			// perhaps this should be an exception
49
			return ErrorEval.VALUE_INVALID;
50
		}
51
52
		double result;
53
		if (args[0] instanceof RefEval) {
54
			result = CountUtils.countMatchingCell((RefEval) args[0], predicate);
55
		} else if (args[0] instanceof AreaEval) {
56
			result = CountUtils.countMatchingCellsInArea((AreaEval) args[0], predicate);
57
		} else {
58
			throw new IllegalArgumentException("Bad range arg type (" + args[0].getClass().getName() + ")");
59
		}
60
		return new NumberEval(result);
61
	}
62
63
	private static final I_MatchPredicate predicate = new I_MatchPredicate() {
64
65
		public boolean matches(ValueEval valueEval) {
66
			if(valueEval == BlankEval.INSTANCE) {
67
				return true;
68
			}
69
			// Note - only BlankEval counts
70
			return false;
71
		}
72
	};
73
}
(-)src/testcases/org/apache/poi/hssf/record/formula/functions/TestCountFuncs.java (+66 lines)
Lines 45-51 Link Here
45
public final class TestCountFuncs extends TestCase {
45
public final class TestCountFuncs extends TestCase {
46
46
47
	private static final String NULL = null;
47
	private static final String NULL = null;
48
	
49
	public void testCountBlank() {
48
50
51
		AreaEval range;
52
		ValueEval[] values;
53
54
		values = new ValueEval[] {
55
				new NumberEval(0),
56
				new StringEval(""),	// note - does not match blank
57
				BoolEval.TRUE,
58
				BoolEval.FALSE,
59
				ErrorEval.DIV_ZERO,
60
				BlankEval.INSTANCE,
61
		};
62
		range = EvalFactory.createAreaEval("A1:B3", values);
63
		confirmCountBlank(1, range);
64
		
65
		values = new ValueEval[] {
66
				new NumberEval(0),
67
				new StringEval(""),	// note - does not match blank
68
				BlankEval.INSTANCE,
69
				BoolEval.FALSE,
70
				BoolEval.TRUE,
71
				BlankEval.INSTANCE,
72
		};
73
		range = EvalFactory.createAreaEval("A1:B3", values);
74
		confirmCountBlank(2, range);
75
	}
76
49
	public void testCountA() {
77
	public void testCountA() {
50
78
51
		ValueEval[] args;
79
		ValueEval[] args;
Lines 196-201 Link Here
196
		double result = NumericFunctionInvoker.invoke(new Countif(), args);
224
		double result = NumericFunctionInvoker.invoke(new Countif(), args);
197
		assertEquals(expected, result, 0);
225
		assertEquals(expected, result, 0);
198
	}
226
	}
227
	private static void confirmCountBlank(int expected, AreaEval range) {
228
		
229
		ValueEval[] args = { range };
230
		double result = NumericFunctionInvoker.invoke(new Countblank(), args);
231
		assertEquals(expected, result, 0);
232
	}
199
233
200
	private static I_MatchPredicate createCriteriaPredicate(ValueEval ev) {
234
	private static I_MatchPredicate createCriteriaPredicate(ValueEval ev) {
201
		return Countif.createCriteriaPredicate(ev, 0, 0);
235
		return Countif.createCriteriaPredicate(ev, 0, 0);
Lines 418-421 Link Here
418
			throw new AssertionFailedError(failureCount + " countif evaluations failed. See stderr for more details");
452
			throw new AssertionFailedError(failureCount + " countif evaluations failed. See stderr for more details");
419
		}
453
		}
420
	}
454
	}
455
	
456
	public void testCounBlankFromSpreadsheet() {
457
		final String FILE_NAME = "countblankExamples.xls";
458
		final int START_ROW_IX = 1;
459
		final int COL_IX_ACTUAL = 3;
460
		final int COL_IX_EXPECTED = 4;
461
462
		int failureCount = 0;
463
		HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook(FILE_NAME);
464
		HSSFSheet sheet = wb.getSheetAt(0);
465
		HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
466
		int maxRow = sheet.getLastRowNum();
467
		for (int rowIx=START_ROW_IX; rowIx<maxRow; rowIx++) {
468
			HSSFRow row = sheet.getRow(rowIx);
469
			if(row == null) {
470
				continue;
471
			}
472
			HSSFCell cell = row.getCell(COL_IX_ACTUAL);
473
			CellValue cv = fe.evaluate(cell);
474
			double actualValue = cv.getNumberValue();
475
			double expectedValue = row.getCell(COL_IX_EXPECTED).getNumericCellValue();
476
			if (actualValue != expectedValue) {
477
				System.err.println("Problem with test case on row " + (rowIx+1) + " "
478
						+ "Expected = (" + expectedValue + ") Actual=(" + actualValue + ") ");
479
				failureCount++;
480
			}
481
		}
482
483
		if (failureCount > 0) {
484
			throw new AssertionFailedError(failureCount + " countblank evaluations failed. See stderr for more details");
485
		}
486
	}
421
}
487
}

Return to bug 48044