Skip to content

Commit

Permalink
Fix MATCH when comparing different numeric types (#1521)
Browse files Browse the repository at this point in the history
Let MATCH compare numerics of different type (e.g. integers and floats).

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->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 <[email protected]>
  • Loading branch information
arnested and Mark Baker authored Jun 19, 2020
1 parent 73c336a commit 1a44ef9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Calculation/LookupRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public static function MATCH($lookupValue, $lookupArray, $matchType = 1)

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;
Expand Down
13 changes: 13 additions & 0 deletions tests/data/Calculation/LookupRef/MATCH.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@
[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
Expand Down

0 comments on commit 1a44ef9

Please sign in to comment.