add initial version of DCount and broken test

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901237 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-05-25 12:36:42 +00:00
parent 3f414c760c
commit b9da0f9350
5 changed files with 174 additions and 5 deletions

View File

@ -105,7 +105,7 @@ public final class FunctionEval {
retval[37] = BooleanFunction.OR;
retval[38] = BooleanFunction.NOT;
retval[39] = NumericFunction.MOD;
// 40: DCOUNT
retval[40] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DCOUNT);
retval[41] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DSUM);
// 42: DAVERAGE
retval[43] = new DStarRunner(DStarRunner.DStarAlgorithmEnum.DMIN);

View File

@ -0,0 +1,40 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
/**
* Implementation of the DCount function:
* Counts the value of a column in an area with given conditions.
*/
public final class DCount implements IDStarAlgorithm {
private int count;
@Override
public boolean processMatch(ValueEval eval) {
count++;
return true;
}
@Override
public ValueEval getResult() {
return new NumberEval(count);
}
}

View File

@ -17,7 +17,6 @@
package org.apache.poi.ss.formula.functions;
import java.util.Locale;
import java.util.function.Supplier;
import org.apache.poi.ss.formula.eval.AreaEval;
@ -55,6 +54,8 @@ public final class DStarRunner implements Function3Arg {
DMAX(DMax::new),
/** @see DSum */
DSUM(DSum::new),
/** @see DCount */
DCOUNT(DCount::new),
;
private final Supplier<IDStarAlgorithm> implSupplier;
@ -122,7 +123,7 @@ public final class DStarRunner implements Function3Arg {
for(int row = 1; row < height; ++row) {
boolean matches;
try {
matches = fullfillsConditions(db, row, cdb);
matches = fulfillsConditions(db, row, cdb);
}
catch (EvaluationException e) {
return ErrorEval.VALUE_INVALID;
@ -181,7 +182,7 @@ public final class DStarRunner implements Function3Arg {
* @param name Column heading.
* @return Corresponding column number.
*/
private static int getColumnForString(AreaEval db,String name) {
private static int getColumnForString(AreaEval db, String name) {
int resultColumn = -1;
final int width = db.getWidth();
for(int column = 0; column < width; ++column) {
@ -211,7 +212,7 @@ public final class DStarRunner implements Function3Arg {
* @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)
private static boolean fulfillsConditions(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,

View File

@ -0,0 +1,64 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula.functions;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.apache.poi.ss.util.Utils.addRow;
import static org.apache.poi.ss.util.Utils.assertDouble;
/**
* Testcase for function DCOUNT()
*/
public class TestDCount {
//https://support.microsoft.com/en-us/office/dcount-function-c1fc7b93-fb0d-4d8d-97db-8d5f076eaeb1
@Disabled
@Test
void testMicrosoftExample1() throws IOException {
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:F2)", 1);
}
}
private HSSFWorkbook initWorkbook1() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
addRow(sheet, 0, "Tree", "Height", "Age", "Yield", "Profit", "Height");
addRow(sheet, 1, "=\"=Apple\"", ">10", null, null, null, "<16");
addRow(sheet, 2, "=\"=Pear\"");
addRow(sheet, 3);
addRow(sheet, 4, "Tree", "Height", "Age", "Yield", "Profit");
addRow(sheet, 5, "Apple", 18, 20, 14, 105);
addRow(sheet, 6, "Pear", 12, 12, 10, 96);
addRow(sheet, 7, "Cherry", 13, 14, 9, 105);
addRow(sheet, 8, "Apple", 14, "N/A", 10, 75);
addRow(sheet, 9, "Pear", 9, 8, 8, 77);
addRow(sheet, 10, "Apple", 12, 11, 6, 45);
return wb;
}
}

View File

@ -0,0 +1,64 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula.functions;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.apache.poi.ss.util.Utils.addRow;
import static org.apache.poi.ss.util.Utils.assertDouble;
/**
* Testcase for function DGET()
*/
public class TestDGet {
//https://support.microsoft.com/en-us/office/dget-function-455568bf-4eef-45f7-90f0-ec250d00892e
@Disabled
@Test
void testMicrosoftExample1() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertDouble(fe, cell, "DGET(A5:E11, \"Yield\", A1:F3)", 10);
}
}
private HSSFWorkbook initWorkbook1() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
addRow(sheet, 0, "Tree", "Height", "Age", "Yield", "Profit", "Height");
addRow(sheet, 1, "=\"=Apple\"", ">10", null, null, null, "<16");
addRow(sheet, 2, "=\"=Pear\"", ">12");
addRow(sheet, 3);
addRow(sheet, 4, "Tree", "Height", "Age", "Yield", "Profit");
addRow(sheet, 5, "Apple", 18, 20, 14, 105);
addRow(sheet, 6, "Pear", 12, 12, 10, 96);
addRow(sheet, 7, "Cherry", 13, 14, 9, 105);
addRow(sheet, 8, "Apple", 14, "N/A", 10, 75);
addRow(sheet, 9, "Pear", 9, 8, 8, 77);
addRow(sheet, 10, "Apple", 12, 11, 6, 45);
return wb;
}
}