Skip to content
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

Exact match in VLOOKUP now returns first match #809

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

- Improve XLSX parsing speed if no readFilter is applied - [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772)
- Fix column names if read filter calls in XLSX reader skip columns - [#777](https://github.com/PHPOffice/PhpSpreadsheet/pull/777)
- Fix VLOOKUP with exact matches

## [1.5.2] - 2018-11-25

Expand Down
48 changes: 34 additions & 14 deletions src/PhpSpreadsheet/Calculation/LookupRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,24 +709,33 @@ public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not

$rowNumber = $rowValue = false;
foreach ($lookup_array as $rowKey => $rowData) {
// break if we have passed possible keys
if ((is_numeric($lookup_value) && is_numeric($rowData[$firstColumn]) && ($rowData[$firstColumn] > $lookup_value)) ||
(!is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]) && (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)))) {
break;
}
// remember the last key, but only if datatypes match
if ((is_numeric($lookup_value) && is_numeric($rowData[$firstColumn])) ||
(!is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]))) {
$rowNumber = $rowKey;
$rowValue = $rowData[$firstColumn];
if ($not_exact_match) {
$rowNumber = $rowKey;
$rowValue = $rowData[$firstColumn];

continue;
} elseif ((strtolower($rowData[$firstColumn]) == strtolower($lookup_value))
// Spreadsheets software returns first exact match,
// we have sorted and we might have broken key orders
// we want the first one (by its initial index)
&& (($rowNumber == false) || ($rowKey < $rowNumber))
) {
$rowNumber = $rowKey;
$rowValue = $rowData[$firstColumn];
}
}
}

if ($rowNumber !== false) {
if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
// if an exact match is required, we have what we need to return an appropriate response
return Functions::NA();
}
// otherwise return the appropriate value
// return the appropriate value
return $lookup_array[$rowNumber][$returnColumn];
}

Expand Down Expand Up @@ -774,20 +783,31 @@ public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not
}
$rowNumber = $rowValue = false;
foreach ($lookup_array[$firstColumn] as $rowKey => $rowData) {
// break if we have passed possible keys
if ((is_numeric($lookup_value) && is_numeric($rowData) && ($rowData > $lookup_value)) ||
(!is_numeric($lookup_value) && !is_numeric($rowData) && (strtolower($rowData) > strtolower($lookup_value)))) {
break;
}
$rowNumber = $rowKey;
$rowValue = $rowData;

// remember the last key, but only if datatypes match (as in VLOOKUP)
if ((is_numeric($lookup_value) && is_numeric($rowData)) ||
(!is_numeric($lookup_value) && !is_numeric($rowData))) {
if ($not_exact_match) {
$rowNumber = $rowKey;
$rowValue = $rowData;

continue;
} elseif ((strtolower($rowData) == strtolower($lookup_value))
&& (($rowNumber == false) || ($rowKey < $rowNumber))
) {
$rowNumber = $rowKey;
$rowValue = $rowData;
}
}
}

if ($rowNumber !== false) {
if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
// if an exact match is required, we have what we need to return an appropriate response
return Functions::NA();
}
// otherwise return the appropriate value
// otherwise return the appropriate value
return $lookup_array[$returnColumn][$rowNumber];
}

Expand Down
10 changes: 10 additions & 0 deletions tests/data/Calculation/LookupRef/HLOOKUP.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,14 @@
2,
true,
],
[
5,
'x',
[
['Selection column', '0', '0', '0', '0', 'x', 'x', 'x', 'x', 'x'],
['Value to retrieve', 1, 2, 3, 4, 5, 6, 7, 8, 9]
],
2,
false
]
];
21 changes: 21 additions & 0 deletions tests/data/Calculation/LookupRef/VLOOKUP.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,25 @@
2,
true,
],
[
5,
'x',
[
[
'Selection column',
'Value to retrieve',
],
['0', 1],
['0', 2],
['0', 3],
['0', 4],
['x', 5],
['x', 6],
['x', 7],
['x', 8],
['x', 9],
],
2,
false
]
];