Skip to content

Commit

Permalink
#1593 Fixed an issue with MATCH function when range is sorted in desc…
Browse files Browse the repository at this point in the history
…ending order (#1594)
  • Loading branch information
swmal authored Sep 12, 2024
1 parent e57e92b commit 5bb2d0d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ internal static int BinarySearch(object lookupValue, IRangeInfo lookupRange, boo
return asc ? SearchAsc(lookupValue, lookupRange, comparer, direction) : SearchDesc(lookupValue, lookupRange, comparer);
}

internal static int GetMaxIndex(IRangeInfo returnArray)
{
return returnArray.Size.NumberOfRows > returnArray.Size.NumberOfCols ?
returnArray.Size.NumberOfRows : returnArray.Size.NumberOfCols;
}

internal static int GetMatchIndex(int ix, IRangeInfo returnArray, LookupMatchMode matchMode, bool asc)
{
if (ix > -1) return ix;
Expand All @@ -150,9 +156,8 @@ internal static int GetMatchIndex(int ix, IRangeInfo returnArray, LookupMatchMod
else if (matchMode == LookupMatchMode.ExactMatchReturnNextLarger)
{
var adjustment = asc ? 0 : -1;
var max = returnArray.Size.NumberOfRows > returnArray.Size.NumberOfCols ?
returnArray.Size.NumberOfRows : returnArray.Size.NumberOfCols;
result = result >= max ? result : result + adjustment;
var max = GetMaxIndex(returnArray);
result = result >= max ? max : result + adjustment;
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public override CompileResult Execute(IList<FunctionArgument> arguments, Parsing
{
return CompileResult.GetErrorResult(eErrorType.NA);
}
return CreateResult(index + 1, DataType.Integer);
var max = LookupBinarySearch.GetMaxIndex(lookupRange);
return CreateResult(index >= max ? index : index + 1, DataType.Integer);
}
/// <summary>
/// If the function is allowed in a pivot table calculated field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,37 @@ public void MatchShouldWorkWithSingleCell()

Assert.AreEqual(1, formulaCell.GetValue<int>());
}

[TestMethod]
public void MatchTest_GreaterThanOrEqual_ShouldReturnIndexOfLast()
{
_worksheet.Cells["G3"].Value = 1d;
_worksheet.Cells["G4"].Value = 0.75;
_worksheet.Cells["G5"].Value = 0.5;
_worksheet.Cells["G6"].Value = 0.27;
_worksheet.Cells["G7"].Value = 0.1;

_worksheet.Cells["A1"].Formula = "MATCH(0.01,G3:G7,-1)";

_worksheet.Calculate();

Assert.AreEqual(5, _worksheet.Cells["A1"].Value);
}

[TestMethod]
public void MatchTest_GreaterThanOrEqual_ShouldReturnIndexOfSecondLast()
{
_worksheet.Cells["G3"].Value = 1d;
_worksheet.Cells["G4"].Value = 0.75;
_worksheet.Cells["G5"].Value = 0.5;
_worksheet.Cells["G6"].Value = 0.27;
_worksheet.Cells["G7"].Value = 0.1;

_worksheet.Cells["A1"].Formula = "MATCH(0.11,G3:G7,-1)";

_worksheet.Calculate();

Assert.AreEqual(4, _worksheet.Cells["A1"].Value);
}
}
}

0 comments on commit 5bb2d0d

Please sign in to comment.