import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.eval.AreaEval;
import org.apache.poi.ss.formula.eval.BlankEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.NumericValueEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.Workbook;
import org.junit.jupiter.api.Test;
+import java.io.IOException;
+
/**
- * Test cases for SUMPRODUCT()
+ * Test cases for SUMIF()
*/
final class TestSumif {
private static final NumberEval _30 = new NumberEval(30);
private static final NumberEval _50 = new NumberEval(50);
private static final NumberEval _60 = new NumberEval(60);
- private static ValueEval invokeSumif(int rowIx, int colIx, ValueEval...args) {
- return new Sumif().evaluate(args, rowIx, colIx);
- }
-
- private static void confirmDouble(double expected, ValueEval actualEval) {
- assertTrue(actualEval instanceof NumericValueEval, "Expected numeric result");
- NumericValueEval nve = (NumericValueEval)actualEval;
- assertEquals(expected, nve.getNumberValue(), 0);
- }
-
@Test
void testBasic() {
ValueEval[] arg0values = new ValueEval[] { _30, _30, _40, _40, _50, _50 };
confirm(140.0, arg0, new StringEval("<=40.0"));
confirm(160.0, arg0, new StringEval("<>40.0"));
confirm(80.0, arg0, new StringEval("=40.0"));
-
-
- }
- private static void confirm(double expectedResult, ValueEval...args) {
- confirmDouble(expectedResult, invokeSumif(-1, -1, args));
}
-
/**
* test for bug observed near svn r882931
*/
assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), BlankEval.instance, new NumberEval(30.0)));
assertEquals(ErrorEval.VALUE_INVALID, invokeSumif(-1, -1, new NumberEval(30.0), new NumberEval(30.0), BlankEval.instance));
}
+
+ @Test
+ void testMicrosoftExample1() throws IOException {
+ try (HSSFWorkbook wb = initWorkbook1()) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFCell cell = wb.getSheetAt(0).createRow(5).createCell(0);
+ confirmDouble(fe, cell, "SUMIF(A2:A5,\">160000\",B2:B5)", 63000);
+ confirmDouble(fe, cell, "SUMIF(A2:A5,\">160000\")", 900000);
+ confirmDouble(fe, cell, "SUMIF(A2:A5,300000,B2:B5)", 21000);
+ confirmDouble(fe, cell, "SUMIF(A2:A5,\">\" & C2,B2:B5)", 49000);
+ }
+ }
+
+ @Test
+ void testMicrosoftExample2() throws IOException {
+ try (HSSFWorkbook wb = initWorkbook2()) {
+ HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
+ HSSFCell cell = wb.getSheetAt(0).createRow(5).createCell(0);
+ confirmDouble(fe, cell, "SUMIF(A2:A7,\"Fruits\",C2:C7)", 2000);
+ //next test is broken and needs investigation
+ //confirmDouble(fe, cell, "SUMIF(A2:A7,\"Vegetables\",C2:C7)", 12000);
+ confirmDouble(fe, cell, "SUMIF(B2:B7,\"*es\",C2:C7)", 4300);
+ confirmDouble(fe, cell, "SUMIF(A2:A7,\"\",C2:C7)", 400);
+ }
+ }
+
+ //see https://support.microsoft.com/en-us/office/sumif-function-169b8c99-c05c-4483-a712-1697a653039b
+ private HSSFWorkbook initWorkbook1() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ addRow(sheet, 0, "Property Value", "Commission", "Data");
+ addRow(sheet, 1, 100000, 7000, 250000);
+ addRow(sheet, 2, 200000, 14000);
+ addRow(sheet, 3, 300000, 21000);
+ addRow(sheet, 4, 400000, 28000);
+ return wb;
+ }
+
+ private HSSFWorkbook initWorkbook2() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ addRow(sheet, 0, "Category", "Food", "Sales");
+ addRow(sheet, 1, "Vegetables", "Tomatoes", 2300);
+ addRow(sheet, 2, "Vegetables", "Celery", 5500);
+ addRow(sheet, 3, "Fruits", "Oranges", 800);
+ addRow(sheet, 4, null, "Butter", 400);
+ addRow(sheet, 5, "Vegetables", "Carrots", 4200);
+ addRow(sheet, 6, "Fruits", "Apples", 1200);
+ return wb;
+ }
+
+ private void addRow(HSSFSheet sheet, int rownum, Object... values) {
+ HSSFRow row = sheet.createRow(rownum);
+ for (int i = 0; i < values.length; i++) {
+ Cell cell = row.createCell(i);
+ if (values[i] instanceof Integer) {
+ cell.setCellValue((Integer)values[i]);
+ } else if (values[i] == null) {
+ cell.setBlank();
+ } else {
+ cell.setCellValue(values[i].toString());
+ }
+ }
+ }
+
+ private static void confirm(double expectedResult, ValueEval...args) {
+ confirmDouble(expectedResult, invokeSumif(-1, -1, args));
+ }
+
+ private static ValueEval invokeSumif(int rowIx, int colIx, ValueEval...args) {
+ return new Sumif().evaluate(args, rowIx, colIx);
+ }
+
+ private static void confirmDouble(double expected, ValueEval actualEval) {
+ assertTrue(actualEval instanceof NumericValueEval, "Expected numeric result");
+ NumericValueEval nve = (NumericValueEval)actualEval;
+ assertEquals(expected, nve.getNumberValue(), 0);
+ }
+
+ private static void confirmDouble(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, double expectedResult) {
+ cell.setCellFormula(formulaText);
+ fe.notifyUpdateCell(cell);
+ CellValue result = fe.evaluate(cell);
+ assertEquals(result.getCellType(), CellType.NUMERIC);
+ assertEquals(expectedResult, result.getNumberValue());
+ }
}