diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f4f3dc..fba9738f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Fixed - Resolve evaluation of utf-8 named ranges in calculation engine [#1522](https://github.com/PHPOffice/PhpSpreadsheet/pull/1522) +- Fix exact MATCH on ranges with empty cells [#1520](https://github.com/PHPOffice/PhpSpreadsheet/pull/1520) ## [1.13.0] - 2020-05-31 diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index f8272404..09042e2c 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -485,6 +485,13 @@ class LookupRef return Functions::NA(); } + if ($matchType == 1) { + // If match_type is 1 the list has to be processed from last to first + + $lookupArray = array_reverse($lookupArray); + $keySet = array_reverse(array_keys($lookupArray)); + } + // Lookup_array should contain only number, text, or logical values, or empty (null) cells foreach ($lookupArray as $i => $lookupArrayValue) { // check the type of the value @@ -498,17 +505,10 @@ class LookupRef $lookupArray[$i] = StringHelper::strToLower($lookupArrayValue); } if (($lookupArrayValue === null) && (($matchType == 1) || ($matchType == -1))) { - $lookupArray = array_slice($lookupArray, 0, $i - 1); + unset($lookupArray[$i]); } } - if ($matchType == 1) { - // If match_type is 1 the list has to be processed from last to first - - $lookupArray = array_reverse($lookupArray); - $keySet = array_reverse(array_keys($lookupArray)); - } - // ** // find the match // ** diff --git a/tests/data/Calculation/LookupRef/MATCH.php b/tests/data/Calculation/LookupRef/MATCH.php index b39edb9f..d9f0a83d 100644 --- a/tests/data/Calculation/LookupRef/MATCH.php +++ b/tests/data/Calculation/LookupRef/MATCH.php @@ -97,6 +97,20 @@ return [ -1, ], + // match on ranges with empty cells + [ + 3, // Expected + 4, // Input + [1, null, 4, null, 8], + 1, + ], + [ + 3, // Expected + 5, // Input + [1, null, 4, null, null], + 1, + ], + // 0s are causing errors, because things like 0 == 'x' is true. Thanks PHP! [ 3,