From 6db153ec5ad37c16fb8dd2f8076939f172e22360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Fri, 12 Jun 2020 20:04:34 +0200 Subject: [PATCH] Fix HLOOKUP on single row Fixes a bug when doing a HLOOKUP on a single row. ```php 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; ``` --- CHANGELOG.md | 1 + src/PhpSpreadsheet/Calculation/LookupRef.php | 2 +- tests/data/Calculation/LookupRef/HLOOKUP.php | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f4f3dc15..86c403f09f 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 HLOOKUP on single row [#1512](https://github.com/PHPOffice/PhpSpreadsheet/pull/1512) ## [1.13.0] - 2020-05-31 diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index f82724044b..c0c1f80331 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -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(); } diff --git a/tests/data/Calculation/LookupRef/HLOOKUP.php b/tests/data/Calculation/LookupRef/HLOOKUP.php index 644ddeba7c..b880f24705 100644 --- a/tests/data/Calculation/LookupRef/HLOOKUP.php +++ b/tests/data/Calculation/LookupRef/HLOOKUP.php @@ -275,6 +275,19 @@ 2, true, ], + [ + 3, + 3, + [ + [ + 1, + 2, + 3, + ], + ], + 1, + true, + ], [ 5, 'x',