From c505772859f74d3f4d24b5d0ab2223db12686a47 Mon Sep 17 00:00:00 2001
From: Josh Micich
Date: Tue, 13 Jan 2009 22:02:52 +0000
Subject: [PATCH] Bugzilla 46523 - added implementation for SUMIF
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@734252 13f79535-47bb-0310-9956-ffa450edef68
---
src/documentation/content/xdocs/changes.xml | 1 +
src/documentation/content/xdocs/status.xml | 1 +
.../hssf/record/formula/functions/Sumif.java | 140 +++++++++++++++---
.../poi/hssf/data/FormulaEvalTestData.xls | Bin 157184 -> 157184 bytes
4 files changed, 121 insertions(+), 21 deletions(-)
diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml
index 139b48f9a0..0502ddc4b1 100644
--- a/src/documentation/content/xdocs/changes.xml
+++ b/src/documentation/content/xdocs/changes.xml
@@ -37,6 +37,7 @@
+ 46523 - added implementation for SUMIF function
Support for reading HSSF column styles
Hook up POIXMLTextExtractor.getMetadataTextExtractor() to the already written POIXMLPropertiesTextExtractor
46472 - Avoid NPE in HPSFPropertiesExtractor when no properties exist
diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml
index 1af660ed72..aaab68a12f 100644
--- a/src/documentation/content/xdocs/status.xml
+++ b/src/documentation/content/xdocs/status.xml
@@ -34,6 +34,7 @@
+ 46523 - added implementation for SUMIF function
Support for reading HSSF column styles
Hook up POIXMLTextExtractor.getMetadataTextExtractor() to the already written POIXMLPropertiesTextExtractor
46472 - Avoid NPE in HPSFPropertiesExtractor when no properties exist
diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Sumif.java b/src/java/org/apache/poi/hssf/record/formula/functions/Sumif.java
index 11cb02315e..5255cda285 100644
--- a/src/java/org/apache/poi/hssf/record/formula/functions/Sumif.java
+++ b/src/java/org/apache/poi/hssf/record/formula/functions/Sumif.java
@@ -1,25 +1,123 @@
-/*
-* 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.
-*/
-/*
- * Created on May 15, 2005
- *
- */
+/* ====================================================================
+ 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.hssf.record.formula.functions;
-public class Sumif extends NotImplementedFunction {
+import org.apache.poi.hssf.record.formula.eval.AreaEval;
+import org.apache.poi.hssf.record.formula.eval.ErrorEval;
+import org.apache.poi.hssf.record.formula.eval.Eval;
+import org.apache.poi.hssf.record.formula.eval.EvaluationException;
+import org.apache.poi.hssf.record.formula.eval.NumberEval;
+import org.apache.poi.hssf.record.formula.eval.RefEval;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
+
+/**
+ * Implementation for the Excel function SUMIF
+ *
+ * Syntax :
+ * SUMIF ( range, criteria, sum_range )
+ *
+ * range | The range over which criteria is applied. Also used for addend values when the third parameter is not present |
+ * criteria | The value or expression used to filter rows from range |
+ * sum_range | Locates the top-left corner of the corresponding range of addends - values to be added (after being selected by the criteria) |
+ *
+ *
+ * @author Josh Micich
+ */
+public final class Sumif implements Function {
+
+ public Eval evaluate(Eval[] args, int srcRowIndex, short srcColumnIndex) {
+ if (args.length < 2) {
+ return ErrorEval.VALUE_INVALID;
+ }
+
+ AreaEval aeRange;
+ AreaEval aeSum;
+ try {
+ aeRange = convertRangeArg(args[0]);
+
+ switch (args.length) {
+ case 2:
+ aeSum = aeRange;
+ break;
+ case 3:
+ aeSum = createSumRange(args[2], aeRange);
+ break;
+ default:
+ return ErrorEval.VALUE_INVALID;
+ }
+ } catch (EvaluationException e) {
+ return e.getErrorEval();
+ }
+ I_MatchPredicate mp = Countif.createCriteriaPredicate(args[1], srcRowIndex, srcRowIndex);
+ double result = sumMatchingCells(aeRange, mp, aeSum);
+ return new NumberEval(result);
+ }
+
+ private static double sumMatchingCells(AreaEval aeRange, I_MatchPredicate mp, AreaEval aeSum) {
+ int height=aeRange.getHeight();
+ int width= aeRange.getWidth();
+
+ double result = 0.0;
+ for (int r=0; rMgh!$lv$ij-SDzXa_T#bZPV5Noxlubv|HVB$(DHEyMniw;z
z|EVR4eaNGdhUZ9#ZQ2a7Q`a%E-DX6=;)t!a&V*R)q!mnsg(fD1$oAYtqs{ns9`AeK
zIp;g~oO{n3JlzIQx1r9>#y~A8X1_RIZNhd|#q`zg_a}G#MA<0zFmA7d=q9VVn|)ko&MkZ1np7
zm;Ix0T$cWNdBj#5yYQ(}vvBHTRXUkC3Q9MZZ{Ae&!wUBcn>KIS4Uw?xTZ(t#O+4sr
zdSXjX%}@Z~#(x2heGYJb3}Askm=%U_Ee0Vh4q@;ygf=sRW}#wI5LVAZC`m*3TP8y3
z0)!7-2$PBtnpYyc_5#8Wsu4DBM!=U5{_Q~s>>_(D!k&W&`x_9_T1e+@gregJFP_49
zC_0NUa)qkAhLHL%gu|bZVF2NcAyj+f*#>X?)YM`ns1
zDZA{{bm{A@Jhdk(@-Fo}NcY`fFzEg2-0?&N<=s9FgWlr)#UiAs+)n2n%68^<7H>R}EA=)Mk+fCPe*WdQyBMt?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