Fix from Josh from bug #44421 - Update Match function to properly support Area references

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@628035 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-02-15 12:13:25 +00:00
parent e113d12d34
commit 8bbaecfd84
4 changed files with 46 additions and 14 deletions

View File

@ -36,6 +36,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.1-beta1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">44421 - Update Match function to properly support Area references</action>
<action dev="POI-DEVELOPERS" type="fix">44417 - Improved handling of references for the need to quote the sheet name for some formulas, but not when fetching a sheet by name</action>
<action dev="POI-DEVELOPERS" type="fix">44413 - Fix for circular references in INDEX, OFFSET, VLOOKUP formulas, where a cell is actually allowed to reference itself</action>
<action dev="POI-DEVELOPERS" type="fix">44403 - Fix for Mid function handling its arguments wrong</action>

View File

@ -33,6 +33,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1-beta1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">44421 - Update Match function to properly support Area references</action>
<action dev="POI-DEVELOPERS" type="fix">44417 - Improved handling of references for the need to quote the sheet name for some formulas, but not when fetching a sheet by name</action>
<action dev="POI-DEVELOPERS" type="fix">44413 - Fix for circular references in INDEX, OFFSET, VLOOKUP formulas, where a cell is actually allowed to reference itself</action>
<action dev="POI-DEVELOPERS" type="fix">44403 - Fix for Mid function handling its arguments wrong</action>

View File

@ -26,7 +26,6 @@ import org.apache.poi.hssf.record.formula.eval.NumericValueEval;
import org.apache.poi.hssf.record.formula.eval.RefEval;
import org.apache.poi.hssf.record.formula.eval.StringEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.hssf.util.AreaReference;
/**
* Implementation for the MATCH() Excel function.<p/>
@ -188,7 +187,7 @@ public final class Match implements Function {
private static double evaluateMatchTypeArg(Eval arg, int srcCellRow, short srcCellCol)
throws EvalEx {
Eval match_type = arg;
if(arg instanceof AreaReference) {
if(arg instanceof AreaEval) {
AreaEval ae = (AreaEval) arg;
// an area ref can work as a scalar value if it is 1x1
if(ae.isColumn() && ae.isRow()) {

View File

@ -21,6 +21,7 @@ import junit.framework.TestCase;
import org.apache.poi.hssf.record.formula.AreaPtg;
import org.apache.poi.hssf.record.formula.eval.Area2DEval;
import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.BoolEval;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.record.formula.eval.Eval;
@ -53,6 +54,13 @@ public final class TestMatch extends TestCase {
NumericValueEval nve = (NumericValueEval)actualEval;
assertEquals(expected, nve.getNumberValue(), 0);
}
/**
* Convenience method
* @return <code>new Area2DEval(new AreaPtg(ref), values)</code>
*/
private static AreaEval createAreaEval(String ref, ValueEval[] values) {
return new Area2DEval(new AreaPtg(ref), values);
}
public void testSimpleNumber() {
@ -64,9 +72,7 @@ public final class TestMatch extends TestCase {
new NumberEval(25),
};
AreaPtg areaPtg = new AreaPtg("A1:A5");
Area2DEval ae = new Area2DEval(areaPtg, values);
AreaEval ae = createAreaEval("A1:A5", values);
confirmInt(2, invokeMatch(new NumberEval(5), ae, MATCH_LARGEST_LTE));
confirmInt(2, invokeMatch(new NumberEval(5), ae, MATCH_EXACT));
@ -86,9 +92,7 @@ public final class TestMatch extends TestCase {
new NumberEval(4),
};
AreaPtg areaPtg = new AreaPtg("A1:A5");
Area2DEval ae = new Area2DEval(areaPtg, values);
AreaEval ae = createAreaEval("A1:A5", values);
confirmInt(2, invokeMatch(new NumberEval(10), ae, MATCH_SMALLEST_GTE));
confirmInt(2, invokeMatch(new NumberEval(10), ae, MATCH_EXACT));
@ -108,8 +112,7 @@ public final class TestMatch extends TestCase {
new StringEval("Ian"),
};
AreaPtg areaPtg = new AreaPtg("A1:A5");
Area2DEval ae = new Area2DEval(areaPtg, values);
AreaEval ae = createAreaEval("A1:A5", values);
// Note String comparisons are case insensitive
confirmInt(3, invokeMatch(new StringEval("Ed"), ae, MATCH_LARGEST_LTE));
@ -129,8 +132,7 @@ public final class TestMatch extends TestCase {
BoolEval.TRUE,
};
AreaPtg areaPtg = new AreaPtg("A1:A4");
Area2DEval ae = new Area2DEval(areaPtg, values);
AreaEval ae = createAreaEval("A1:A4", values);
// Note String comparisons are case insensitive
confirmInt(2, invokeMatch(BoolEval.FALSE, ae, MATCH_LARGEST_LTE));
@ -157,8 +159,7 @@ public final class TestMatch extends TestCase {
new StringEval("Ed"),
};
AreaPtg areaPtg = new AreaPtg("A1:A13");
Area2DEval ae = new Area2DEval(areaPtg, values);
AreaEval ae = createAreaEval("A1:A13", values);
assertEquals(ErrorEval.NA, invokeMatch(new StringEval("Aaron"), ae, MATCH_LARGEST_LTE));
@ -181,4 +182,34 @@ public final class TestMatch extends TestCase {
confirmInt(12, invokeMatch(BoolEval.TRUE, ae, MATCH_LARGEST_LTE));
}
/**
* Ensures that the match_type argument can be an <tt>AreaEval</tt>.<br/>
* Bugzilla 44421
*/
public void testMatchArgTypeArea() {
ValueEval[] values = {
new NumberEval(4),
new NumberEval(5),
new NumberEval(10),
new NumberEval(10),
new NumberEval(25),
};
AreaEval ae = createAreaEval("A1:A5", values);
AreaEval matchAE = createAreaEval("C1:C1", new ValueEval[] { MATCH_LARGEST_LTE, });
try {
confirmInt(4, invokeMatch(new NumberEval(10), ae, matchAE));
} catch (RuntimeException e) {
if(e.getMessage().startsWith("Unexpected match_type type")) {
// identified bug 44421
fail(e.getMessage());
}
// some other error ??
throw e;
}
}
}