Skip to content

Commit a5a0268

Browse files
arnestedMark Baker
and
Mark Baker
authored
Fix HLOOKUP on single row (#1512)
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]>
1 parent 38fab4e commit a5a0268

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
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 HLOOKUP on single row [#1512](https://github.com/PHPOffice/PhpSpreadsheet/pull/1512)
1314
- Fix MATCH when comparing different numeric types [#1521](https://github.com/PHPOffice/PhpSpreadsheet/pull/1521)
1415
- Fix exact MATCH on ranges with empty cells [#1520](https://github.com/PHPOffice/PhpSpreadsheet/pull/1520)
1516

src/PhpSpreadsheet/Calculation/LookupRef.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not
807807
return Functions::REF();
808808
}
809809
$f = array_keys($lookup_array);
810-
$firstRow = array_pop($f);
810+
$firstRow = reset($f);
811811
if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array))) {
812812
return Functions::REF();
813813
}

tests/data/Calculation/LookupRef/HLOOKUP.php

+13
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@
275275
2,
276276
true,
277277
],
278+
[
279+
3,
280+
3,
281+
[
282+
[
283+
1,
284+
2,
285+
3,
286+
],
287+
],
288+
1,
289+
true,
290+
],
278291
[
279292
5,
280293
'x',

0 commit comments

Comments
 (0)