]> source.dussan.org Git - poi.git/commitdiff
DStar functions need to support field params provided as numbers as well as field...
authorPJ Fanning <fanningpj@apache.org>
Wed, 25 May 2022 15:54:29 +0000 (15:54 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 25 May 2022 15:54:29 +0000 (15:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1901250 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/formula/functions/DCount.java
poi/src/main/java/org/apache/poi/ss/formula/functions/DGet.java
poi/src/main/java/org/apache/poi/ss/formula/functions/DMax.java
poi/src/main/java/org/apache/poi/ss/formula/functions/DMin.java
poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java
poi/src/main/java/org/apache/poi/ss/formula/functions/DSum.java
poi/src/main/java/org/apache/poi/ss/formula/functions/IDStarAlgorithm.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestDCount.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestDGet.java

index c925652e7fba57d59e1aa2f21a024e750be720a0..a367eca81444268590f15cf0854b054d1bc35c9a 100644 (file)
@@ -29,8 +29,8 @@ public final class DCount implements IDStarAlgorithm {
     private int count;
 
     @Override
-    public boolean processMatch(ValueEval eval, String field) {
-        if (field == null || eval instanceof NumericValueEval) {
+    public boolean processMatch(ValueEval eval, int fieldNumber) {
+        if (fieldNumber < 0 || eval instanceof NumericValueEval) {
             count++;
         }
         return true;
index 606e13d7cfecff42d6030395d6aab5467386f75c..34c2900462c1212e9bcbb6369078a712eed4e4f4 100644 (file)
@@ -31,7 +31,7 @@ public final class DGet implements IDStarAlgorithm {
     private ValueEval result;
 
     @Override
-    public boolean processMatch(ValueEval eval, String field) {
+    public boolean processMatch(ValueEval eval, int fieldNumber) {
         if(result == null) // First match, just set the value.
         {
             result = eval;
index 0960e8c667f0040199e8514a3a201d200715607d..77b4624406e0e1c5d835e2143c62eb44ff9cbcb9 100644 (file)
@@ -33,7 +33,7 @@ public final class DMax implements IDStarAlgorithm {
     private ValueEval maximumValue;
 
     @Override
-    public boolean processMatch(ValueEval eval, String field) {
+    public boolean processMatch(ValueEval eval, int fieldNumber) {
         if(eval instanceof NumericValueEval) {
             if(maximumValue == null) { // First match, just set the value.
                 maximumValue = eval;
index 8919dc9426e92dbb2b8184aba85733f8934c6d5d..216d194ca589c05a9b8bb188acd93c494da6c8f1 100644 (file)
@@ -33,7 +33,7 @@ public final class DMin implements IDStarAlgorithm {
     private ValueEval minimumValue;
 
     @Override
-    public boolean processMatch(ValueEval eval, String field) {
+    public boolean processMatch(ValueEval eval, int fieldNumber) {
         if(eval instanceof NumericValueEval) {
             if(minimumValue == null) { // First match, just set the value.
                 minimumValue = eval;
index 2af4de18675b7e572a09276315b606932479a42f..5c014802aaef5cbd32a366e6523dacd9de94aff8 100644 (file)
@@ -104,12 +104,13 @@ public final class DStarRunner implements Function3Arg {
         final IDStarAlgorithm algorithm = algoType.newInstance();
 
         int fc = -1;
-        String field = null;
         try {
             filterColumn = OperandResolver.getSingleValue(filterColumn, srcRowIndex, srcColumnIndex);
-            fc = getColumnForName(filterColumn, db);
-            if (filterColumn instanceof StringEval) {
-                field = ((StringEval)filterColumn).getStringValue();
+            if (filterColumn instanceof NumericValueEval) {
+                //fc is zero based while Excel uses 1 based column numbering
+                fc = (int) Math.round(((NumericValueEval)filterColumn).getNumberValue()) - 1;
+            } else {
+                fc = getColumnForName(filterColumn, db);
             }
             if(fc == -1 && !algorithm.allowEmptyMatchField()) {
                 // column not found
@@ -140,7 +141,7 @@ public final class DStarRunner implements Function3Arg {
             if(matches) {
                 ValueEval currentValueEval = resolveReference(db, row, fc);
                 // Pass the match to the algorithm and conditionally abort the search.
-                boolean shouldContinue = algorithm.processMatch(currentValueEval, field);
+                boolean shouldContinue = algorithm.processMatch(currentValueEval, fc);
                 if(! shouldContinue) {
                     break;
                 }
index df60f7063a572e9ff787a75a82e1aaa6cdacede8..758b2481c3dcdb3895eb9e065cf14c13860b45c3 100644 (file)
@@ -33,7 +33,7 @@ public final class DSum implements IDStarAlgorithm {
     private double totalValue = 0;
 
     @Override
-    public boolean processMatch(ValueEval eval, String field) {
+    public boolean processMatch(ValueEval eval, int fieldNumber) {
         if(eval instanceof NumericValueEval) {
             double currentValue = ((NumericValueEval)eval).getNumberValue();
             totalValue += currentValue;
index e9fbd6c7be035d9372ac3343c3a951c73b4adf2e..dff5eb227f07029ad59458f22ecfb2ff5d6e66fd 100644 (file)
@@ -27,10 +27,10 @@ public interface IDStarAlgorithm {
     /**
      * 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)
+     * @param fieldNumber the field number (added in POI 5.2.3)
      * @return Whether we should continue iterating through the database.
      */
-    boolean processMatch(ValueEval eval, String field);
+    boolean processMatch(ValueEval eval, int fieldNumber);
 
     /**
      * Return a result ValueEval that will be the result of the calculation.
index dcd9b83bae321be844909dcf37165b26ab450a39..31888498ee3c72b8a6b2818493c9d94165ad2e6f 100644 (file)
@@ -42,6 +42,7 @@ public class TestDCount {
             assertDouble(fe, cell, "DCOUNT(A5:E11,,A1:A2)", 3);
             assertDouble(fe, cell, "DCOUNT(A5:E11, \"Age\", A1:A2)", 2);
             assertDouble(fe, cell, "DCOUNT(A5:E11, \"Age\", A1:F2)", 1);
+            assertDouble(fe, cell, "DCOUNT(A5:E11, 3, A1:F2)", 1);
         }
     }
 
index cfbf0b832a3ad3773c821938389c3cafc9b95c02..349f1b5e51ad0de57d7742cd7abae06429c56b9f 100644 (file)
@@ -43,6 +43,7 @@ public class TestDGet {
             HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
             assertError(fe, cell, "DGET(A5:E11, \"Yield\", A1:A3)", FormulaError.NUM);
             assertDouble(fe, cell, "DGET(A5:E11, \"Yield\", A1:F3)", 10);
+            assertDouble(fe, cell, "DGET(A5:E11, 4, A1:F3)", 10);
         }
     }
 
@@ -52,6 +53,7 @@ public class TestDGet {
             HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
             HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
             assertDouble(fe, cell, "DGET(A5:E11, \"Yield\", A1:F3)", 6);
+            assertDouble(fe, cell, "DGET(A5:E11, 4, A1:F3)", 6);
         }
     }