Skip to content

Commit 73c336a

Browse files
authored
Fix exact MATCH on ranges with empty cells (#1520)
Fixes a bug when doing exact match on ranges with empty cells. ```php <?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); // Row: 1, null, 4, null, 8. $sheet->getCell('A1')->setValue(1); $sheet->getCell('A3')->setValue(4); $sheet->getCell('A5')->setValue(8); $sheet->getCell('B1')->setValue('=MATCH(4, A1:A5, 1)'); // Should echo 3, but echos '#N/A'. echo $sheet->getCell('B1')->getCalculatedValue() . PHP_EOL; // Row: 1, null, 4, null, null. $sheet->getCell('C1')->setValue(1); $sheet->getCell('C3')->setValue(4); $sheet->getCell('D1')->setValue('=MATCH(5, C1:C5, 1)'); // Should echo 3, but echos '#N/A'. echo $sheet->getCell('D1')->getCalculatedValue() . PHP_EOL; ```
1 parent d8b4c3b commit 73c336a

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Fixed
1111

1212
- Resolve evaluation of utf-8 named ranges in calculation engine [#1522](https://github.com/PHPOffice/PhpSpreadsheet/pull/1522)
13+
- Fix exact MATCH on ranges with empty cells [#1520](https://github.com/PHPOffice/PhpSpreadsheet/pull/1520)
1314

1415
## [1.13.0] - 2020-05-31
1516

src/PhpSpreadsheet/Calculation/LookupRef.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,13 @@ public static function MATCH($lookupValue, $lookupArray, $matchType = 1)
485485
return Functions::NA();
486486
}
487487

488+
if ($matchType == 1) {
489+
// If match_type is 1 the list has to be processed from last to first
490+
491+
$lookupArray = array_reverse($lookupArray);
492+
$keySet = array_reverse(array_keys($lookupArray));
493+
}
494+
488495
// Lookup_array should contain only number, text, or logical values, or empty (null) cells
489496
foreach ($lookupArray as $i => $lookupArrayValue) {
490497
// check the type of the value
@@ -498,17 +505,10 @@ public static function MATCH($lookupValue, $lookupArray, $matchType = 1)
498505
$lookupArray[$i] = StringHelper::strToLower($lookupArrayValue);
499506
}
500507
if (($lookupArrayValue === null) && (($matchType == 1) || ($matchType == -1))) {
501-
$lookupArray = array_slice($lookupArray, 0, $i - 1);
508+
unset($lookupArray[$i]);
502509
}
503510
}
504511

505-
if ($matchType == 1) {
506-
// If match_type is 1 the list has to be processed from last to first
507-
508-
$lookupArray = array_reverse($lookupArray);
509-
$keySet = array_reverse(array_keys($lookupArray));
510-
}
511-
512512
// **
513513
// find the match
514514
// **

tests/data/Calculation/LookupRef/MATCH.php

+14
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@
9797
-1,
9898
],
9999

100+
// match on ranges with empty cells
101+
[
102+
3, // Expected
103+
4, // Input
104+
[1, null, 4, null, 8],
105+
1,
106+
],
107+
[
108+
3, // Expected
109+
5, // Input
110+
[1, null, 4, null, null],
111+
1,
112+
],
113+
100114
// 0s are causing errors, because things like 0 == 'x' is true. Thanks PHP!
101115
[
102116
3,

0 commit comments

Comments
 (0)