From b9da0f93504193d0da217a2eeea5e5a2800141a0 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 25 May 2022 12:36:42 +0000 Subject: [PATCH] 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 --- .../poi/ss/formula/eval/FunctionEval.java | 2 +- .../poi/ss/formula/functions/DCount.java | 40 ++++++++++++ .../poi/ss/formula/functions/DStarRunner.java | 9 +-- .../poi/ss/formula/functions/TestDCount.java | 64 +++++++++++++++++++ .../poi/ss/formula/functions/TestDGet.java | 64 +++++++++++++++++++ 5 files changed, 174 insertions(+), 5 deletions(-) create mode 100644 poi/src/main/java/org/apache/poi/ss/formula/functions/DCount.java create mode 100644 poi/src/test/java/org/apache/poi/ss/formula/functions/TestDCount.java create mode 100644 poi/src/test/java/org/apache/poi/ss/formula/functions/TestDGet.java diff --git a/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java b/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java index b22ec1ef6a..a71c0b7901 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/eval/FunctionEval.java @@ -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); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/DCount.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/DCount.java new file mode 100644 index 0000000000..a8c2ca22e1 --- /dev/null +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DCount.java @@ -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); + } +} diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java index 97f20cebc5..369288d1fb 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java @@ -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 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, diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDCount.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDCount.java new file mode 100644 index 0000000000..f30d1564e3 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDCount.java @@ -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; + } +} diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDGet.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDGet.java new file mode 100644 index 0000000000..92ddef7f7c --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDGet.java @@ -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; + } +} -- 2.39.5