From 1a44ef91095ea4923b63c29d16887923760f6d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Fri, 19 Jun 2020 20:54:04 +0200 Subject: [PATCH] Fix MATCH when comparing different numeric types (#1521) Let MATCH compare numerics of different type (e.g. integers and floats). ```php getActiveSheet(); // Row: 1, 2, 3, 4, 5. MATCH for 4.6. $sheet->getCell('A1')->setValue(1); $sheet->getCell('A2')->setValue(2); $sheet->getCell('A3')->setValue(3); $sheet->getCell('A4')->setValue(4); $sheet->getCell('A5')->setValue(5); $sheet->getCell('B1')->setValue('=MATCH(4.6, A1:A5, 1)'); // Should echo 4, but echos '#N/A'. echo $sheet->getCell('B1')->getCalculatedValue() . PHP_EOL; // Row: 1, 2, 3, 3.8, 5. MATCH for 4. $sheet->getCell('C1')->setValue(1); $sheet->getCell('C2')->setValue(2); $sheet->getCell('C3')->setValue(3); $sheet->getCell('C4')->setValue(3.8); $sheet->getCell('C5')->setValue(5); $sheet->getCell('D1')->setValue('=MATCH(4, C1:C5, 1)'); // Should echo 4, but echos 3. echo $sheet->getCell('D1')->getCalculatedValue() . PHP_EOL; ``` Co-authored-by: Mark Baker --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/LookupRef.php | 2 +- tests/data/Calculation/LookupRef/MATCH.php | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fba9738f..aee16380 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 MATCH when comparing different numeric types [#1521](https://github.com/PHPOffice/PhpSpreadsheet/pull/1521) - 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 09042e2c..904eb24b 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -515,7 +515,7 @@ class LookupRef if ($matchType === 0 || $matchType === 1) { foreach ($lookupArray as $i => $lookupArrayValue) { - $typeMatch = gettype($lookupValue) === gettype($lookupArrayValue); + $typeMatch = ((gettype($lookupValue) === gettype($lookupArrayValue)) || (is_numeric($lookupValue) && is_numeric($lookupArrayValue))); $exactTypeMatch = $typeMatch && $lookupArrayValue === $lookupValue; $nonOnlyNumericExactMatch = !$typeMatch && $lookupArrayValue === $lookupValue; $exactMatch = $exactTypeMatch || $nonOnlyNumericExactMatch; diff --git a/tests/data/Calculation/LookupRef/MATCH.php b/tests/data/Calculation/LookupRef/MATCH.php index d9f0a83d..671056dd 100644 --- a/tests/data/Calculation/LookupRef/MATCH.php +++ b/tests/data/Calculation/LookupRef/MATCH.php @@ -179,6 +179,19 @@ return [ [true, false, 'a', 'z', 222222, 2, 99999999], -1, ], + // when mixing numeric types + [ + 4, // Expected + 4.6, + [1, 2, 3, 4, 5], + 1, + ], + [ + 4, // Expected + 4, + [1, 2, 3, 3.8, 5], + 1, + ], // if element of same data type met and it is < than searched one #N/A - no further processing [ '#N/A', // Expected