From: Josh Micich Date: Tue, 13 Jan 2009 22:02:52 +0000 (+0000) Subject: Bugzilla 46523 - added implementation for SUMIF X-Git-Tag: REL_3_5_BETA5~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c505772859f74d3f4d24b5d0ab2223db12686a47;p=poi.git Bugzilla 46523 - added implementation for SUMIF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@734252 13f79535-47bb-0310-9956-ffa450edef68 --- 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 )
+ * + * + * + * + *
rangeThe range over which criteria is applied. Also used for addend values when the third parameter is not present
criteriaThe value or expression used to filter rows from range
sum_rangeLocates 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; r