All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.ss.formula.functions.Sumif Maven / Gradle / Ivy

There is a newer version: 5.2.5
Show newest version
/* ====================================================================
   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.AreaEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.RefEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.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 extends Var2or3ArgFunction { public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { AreaEval aeRange; try { aeRange = convertRangeArg(arg0); } catch (EvaluationException e) { return e.getErrorEval(); } return eval(srcRowIndex, srcColumnIndex, arg1, aeRange, aeRange); } public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) { AreaEval aeRange; AreaEval aeSum; try { aeRange = convertRangeArg(arg0); aeSum = createSumRange(arg2, aeRange); } catch (EvaluationException e) { return e.getErrorEval(); } return eval(srcRowIndex, srcColumnIndex, arg1, aeRange, aeSum); } private static ValueEval eval(int srcRowIndex, int srcColumnIndex, ValueEval arg1, AreaEval aeRange, AreaEval aeSum) { // TODO - junit to prove last arg must be srcColumnIndex and not srcRowIndex I_MatchPredicate mp = Countif.createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex); // handle empty cells if(mp == null) { return NumberEval.ZERO; } 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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy