Skip to content

Commit

Permalink
Improve HLOOKUP and VLOOKUP performance
Browse files Browse the repository at this point in the history
  • Loading branch information
igitur committed Jul 10, 2019
1 parent 27b520d commit bf5e9db
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ClosedXML/Excel/CalcEngine/Functions/Lookup.cs
Expand Up @@ -63,12 +63,14 @@ private static object Hlookup(List<Expression> p)
if (range_lookup && matching_column == null)
{
var first_column = range.FirstColumn().ColumnNumber();
var number_of_columns_in_range = range.ColumnsUsed().Count();

matching_column = range.FindColumn(c =>
{
var column_index_in_range = c.ColumnNumber() - first_column + 1;
if (column_index_in_range < range.ColumnsUsed().Count() && !c.Cell(1).IsEmpty() && new Expression(c.Cell(1).Value).CompareTo(lookup_value) <= 0 && !c.ColumnRight().Cell(1).IsEmpty() && new Expression(c.ColumnRight().Cell(1).Value).CompareTo(lookup_value) > 0)
if (column_index_in_range < number_of_columns_in_range && !c.Cell(1).IsEmpty() && new Expression(c.Cell(1).Value).CompareTo(lookup_value) <= 0 && !c.ColumnRight().Cell(1).IsEmpty() && new Expression(c.ColumnRight().Cell(1).Value).CompareTo(lookup_value) > 0)
return true;
else if (column_index_in_range == range.ColumnsUsed().Count() && !c.Cell(1).IsEmpty() && new Expression(c.Cell(1).Value).CompareTo(lookup_value) <= 0)
else if (column_index_in_range == number_of_columns_in_range && !c.Cell(1).IsEmpty() && new Expression(c.Cell(1).Value).CompareTo(lookup_value) <= 0)
return true;
else
return false;
Expand Down Expand Up @@ -239,12 +241,14 @@ private static object Vlookup(List<Expression> p)
if (range_lookup && matching_row == null)
{
var first_row = range.FirstRow().RowNumber();
var number_of_rows_in_range = range.RowsUsed().Count();

matching_row = range.FindRow(r =>
{
var row_index_in_range = r.RowNumber() - first_row + 1;
if (row_index_in_range < range.RowsUsed().Count() && !r.Cell(1).IsEmpty() && new Expression(r.Cell(1).Value).CompareTo(lookup_value) <= 0 && !r.RowBelow().Cell(1).IsEmpty() && new Expression(r.RowBelow().Cell(1).Value).CompareTo(lookup_value) > 0)
if (row_index_in_range < number_of_rows_in_range && !r.Cell(1).IsEmpty() && new Expression(r.Cell(1).Value).CompareTo(lookup_value) <= 0 && !r.RowBelow().Cell(1).IsEmpty() && new Expression(r.RowBelow().Cell(1).Value).CompareTo(lookup_value) > 0)
return true;
else if (row_index_in_range == range.RowsUsed().Count() && !r.Cell(1).IsEmpty() && new Expression(r.Cell(1).Value).CompareTo(lookup_value) <= 0)
else if (row_index_in_range == number_of_rows_in_range && !r.Cell(1).IsEmpty() && new Expression(r.Cell(1).Value).CompareTo(lookup_value) <= 0)
return true;
else
return false;
Expand Down

0 comments on commit bf5e9db

Please sign in to comment.