Skip to content

Commit

Permalink
Fix HLOOKUP on single row (#1512)
Browse files Browse the repository at this point in the history
Fixes a bug when doing a HLOOKUP on a single row.

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

/**
 * Single row.
 */
$singleRow = "=HLOOKUP(10, {5, 10, 15}, 1, 0)";
$sheet->getCell('A1')->setValue($singleRow);

// Should echo 10, but echos '#N/A' and some PHP notices and warnings.
echo $sheet->getCell('A1')->getCalculatedValue() . PHP_EOL;

/**
 * Multiple rows.
 */
$multipleRows = "=HLOOKUP(10, {5, 10, 15; 20, 25, 30}, 1, 0)";
$sheet->getCell('A2')->setValue($multipleRows);

// Should echo: 10 and also does.
echo $sheet->getCell('A2')->getCalculatedValue() . PHP_EOL;
```

Co-authored-by: Mark Baker <[email protected]>
  • Loading branch information
arnested and Mark Baker authored Jun 19, 2020
1 parent 38fab4e commit a5a0268
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 HLOOKUP on single row [#1512](https://github.com/PHPOffice/PhpSpreadsheet/pull/1512)
- 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)

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 @@ -807,7 +807,7 @@ public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not
return Functions::REF();
}
$f = array_keys($lookup_array);
$firstRow = array_pop($f);
$firstRow = reset($f);
if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array))) {
return Functions::REF();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/data/Calculation/LookupRef/HLOOKUP.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@
2,
true,
],
[
3,
3,
[
[
1,
2,
3,
],
],
1,
true,
],
[
5,
'x',
Expand Down

0 comments on commit a5a0268

Please sign in to comment.