-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patch averageif #330
Patch averageif #330
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* ==================================================================== | ||
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.OperationEvaluationContext; | ||
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.ValueEval; | ||
import org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate; | ||
|
||
/** | ||
* Handler for singular AverageIf which has different operand handling than | ||
* the generic AverageIfs version. | ||
*/ | ||
public class AverageIf | ||
extends Baseifs | ||
{ | ||
public static final FreeRefFunction instance = new Averageifs(); | ||
|
||
|
||
@Override | ||
public ValueEval evaluate( ValueEval[] _args, OperationEvaluationContext _ec ) | ||
{ | ||
if( _args.length < 2 ) | ||
{ | ||
return ErrorEval.VALUE_INVALID; | ||
} | ||
|
||
try | ||
{ | ||
AreaEval sumRange = null; | ||
|
||
if( _args.length == 3 ) | ||
{ | ||
sumRange = convertRangeArg( _args[2] ); | ||
} | ||
|
||
// collect pairs of ranges and criteria | ||
AreaEval ae = convertRangeArg( _args[0] ); | ||
I_MatchPredicate mp = Countif.createCriteriaPredicate( _args[ 1 ], _ec.getRowIndex(), _ec.getColumnIndex()); | ||
|
||
if( mp instanceof Countif.ErrorMatcher ) | ||
{ | ||
throw new EvaluationException(ErrorEval.valueOf((( Countif.ErrorMatcher) mp).getValue())); | ||
} | ||
|
||
return aggregateMatchingCells( createAggregator(), sumRange, ae, mp ); | ||
} | ||
catch (EvaluationException e) | ||
{ | ||
return e.getErrorEval(); | ||
} | ||
} | ||
|
||
protected ValueEval aggregateMatchingCells( Aggregator aggregator, AreaEval sumRange, AreaEval range, I_MatchPredicate mp ) | ||
throws EvaluationException | ||
{ | ||
int height = range.getHeight(); | ||
int width = range.getWidth(); | ||
|
||
for (int r = 0; r < height; r++) { | ||
for (int c = 0; c < width; c++) { | ||
|
||
ValueEval _ve = range.getRelativeValue(r, c);; | ||
|
||
// Bugs 60858 and 56420 show predicate can be null | ||
if( sumRange != null ) | ||
{ | ||
_ve = sumRange.getRelativeValue(r, c); | ||
} | ||
|
||
if ( mp != null && mp.matches( _ve ) ) | ||
{ // aggregate only if all of the corresponding criteria specified are true for that cell. | ||
if ( _ve instanceof ErrorEval) | ||
{ | ||
throw new EvaluationException((ErrorEval) _ve ); | ||
} | ||
aggregator.addValue( _ve ); | ||
} | ||
} | ||
} | ||
return aggregator.getResult(); | ||
} | ||
|
||
@Override | ||
protected boolean hasInitialRange() | ||
{ | ||
return false; | ||
} | ||
|
||
|
||
@Override | ||
protected Aggregator createAggregator() | ||
{ | ||
return new Aggregator() { | ||
Double sum = 0.0; | ||
Integer count = 0; | ||
|
||
@Override | ||
public void addValue(ValueEval value) { | ||
|
||
if(!(value instanceof NumberEval)) return; | ||
|
||
double d = ((NumberEval) value).getNumberValue(); | ||
sum += d; | ||
count++; | ||
|
||
} | ||
|
||
@Override | ||
public ValueEval getResult() { | ||
return count == 0 ? ErrorEval.DIV_ZERO : new NumberEval(sum / count); | ||
} | ||
}; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* ==================================================================== | ||
* 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 static org.apache.poi.ss.util.Utils.*; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.apache.poi.hssf.HSSFTestDataSamples; | ||
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.apache.poi.ss.formula.OperationEvaluationContext; | ||
import org.apache.poi.ss.formula.eval.ErrorEval; | ||
import org.apache.poi.ss.formula.eval.NumberEval; | ||
import org.apache.poi.ss.formula.eval.NumericValueEval; | ||
import org.apache.poi.ss.formula.eval.StringEval; | ||
import org.apache.poi.ss.formula.eval.ValueEval; | ||
import org.apache.poi.ss.usermodel.FormulaError; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Test cases for AVERAGEIFS() | ||
*/ | ||
final class TestAverageIf { | ||
|
||
private static final OperationEvaluationContext EC = new OperationEvaluationContext(null, null, 0, 1, 0, null); | ||
|
||
private static ValueEval invokeAverageif(ValueEval[] args) { | ||
return new AverageIf().evaluate(args, EC); | ||
} | ||
|
||
private static void confirmDouble(double expected, ValueEval actualEval) { | ||
assertTrue(actualEval instanceof NumericValueEval, "Expected numeric result"); | ||
NumericValueEval nve = (NumericValueEval)actualEval; | ||
assertEquals(expected, nve.getNumberValue(), 0); | ||
} | ||
|
||
private static void confirm(double expectedResult, ValueEval[] args) { | ||
confirmDouble(expectedResult, invokeAverageif(args)); | ||
} | ||
|
||
private static void confirmError(ErrorEval errorEval, ValueEval[] args) { | ||
ValueEval actualEval = invokeAverageif(args); | ||
assertEquals(errorEval, actualEval); | ||
} | ||
|
||
/** | ||
* Example 1 from | ||
* https://support.microsoft.com/en-us/office/maxifs-function-dfd611e6-da2c-488a-919b-9b6376b28883 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this link be https://support.microsoft.com/en-us/office/averageif-function-faec8e2e-0dec-4308-af69-f5576d8ac642 ? the test data does not match either of the examples on that doc page though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. the danger of cut/paste coding (i stole the UT from averageifs) for expedience. i can make the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use the examples from https://support.microsoft.com/en-us/office/averageif-function-faec8e2e-0dec-4308-af69-f5576d8ac642 though - ie the data and test scenarios? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the M$ examples are rubbish - very focused on a small number of happy path cases and occasionally an exception case but at least using them as a guide is better than ignoring them and focusing a use case that suits you and maybe noone else |
||
*/ | ||
@Test | ||
void testExample1() { | ||
ValueEval[] b2b5 = new ValueEval[] { | ||
new StringEval("Quiz"), | ||
new StringEval("Grade"), | ||
new NumberEval(75), | ||
new NumberEval(94) | ||
}; | ||
|
||
ValueEval[] args; | ||
// "=AVERAGEIFS(B2:B5, B2:B5, ">70", B2:B5, "<90")" | ||
args = new ValueEval[]{ | ||
EvalFactory.createAreaEval("B2:B5", b2b5), | ||
new StringEval(">70"), | ||
EvalFactory.createAreaEval("B2:B5", b2b5) | ||
}; | ||
confirm(84.5, args); | ||
|
||
ValueEval[] c2c5 = new ValueEval[] { | ||
new StringEval("Quiz"), | ||
new StringEval("Grade"), | ||
new NumberEval(85), | ||
new NumberEval(80) | ||
}; | ||
// "=AVERAGEIFS(C2:C5, C2:C5, ">95")" | ||
args = new ValueEval[]{ | ||
EvalFactory.createAreaEval("C2:C5", c2c5), | ||
new StringEval(">95"), | ||
EvalFactory.createAreaEval("C2:C5", c2c5) | ||
}; | ||
|
||
confirmError(ErrorEval.DIV_ZERO, args); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to follow a code-style where brackets are on the previous line, can you re-format this class to follow that, also "extends" is usually on the same line as the class, i.e.
public class AverageIf extends Baseifs {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. i've never been a fan of K&R formatting, but i can make that change.