diff --git a/CHANGELOG.md b/CHANGELOG.md index 74d75470..b62c555f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Improve XLSX parsing speed if no readFilter is applied - [#772](https://github.com/PHPOffice/PhpSpreadsheet/issues/772) - Fix column names if read filter calls in XLSX reader skip columns - [#777](https://github.com/PHPOffice/PhpSpreadsheet/pull/777) +- Fix LOOKUP function which was breaking on edge cases - [#796](https://github.com/PHPOffice/PhpSpreadsheet/issues/796) ## [1.5.2] - 2018-11-25 diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index 9a2937fb..92115fff 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -811,11 +811,13 @@ class LookupRef if (!is_array($lookup_vector)) { return Functions::NA(); } + $hasResultVector = isset($result_vector); $lookupRows = count($lookup_vector); $l = array_keys($lookup_vector); $l = array_shift($l); $lookupColumns = count($lookup_vector[$l]); - if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) { + // we correctly orient our results + if (($lookupRows === 1 && $lookupColumns > 1) || (!$hasResultVector && $lookupRows === 2 && $lookupColumns !== 2)) { $lookup_vector = self::TRANSPOSE($lookup_vector); $lookupRows = count($lookup_vector); $l = array_keys($lookup_vector); @@ -829,18 +831,20 @@ class LookupRef $l = array_keys($result_vector); $l = array_shift($l); $resultColumns = count($result_vector[$l]); - if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) { + // we correctly orient our results + if ($resultRows === 1 && $resultColumns > 1) { $result_vector = self::TRANSPOSE($result_vector); $resultRows = count($result_vector); $r = array_keys($result_vector); $resultColumns = count($result_vector[array_shift($r)]); } - if ($lookupRows == 2) { + if ($lookupRows === 2 && !$hasResultVector) { $result_vector = array_pop($lookup_vector); $lookup_vector = array_shift($lookup_vector); } - if ($lookupColumns != 2) { + + if ($lookupColumns !== 2) { foreach ($lookup_vector as &$value) { if (is_array($value)) { $k = array_keys($value); diff --git a/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php b/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php index e7c949fa..acf709be 100644 --- a/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php @@ -51,6 +51,22 @@ class LookupRefTest extends TestCase return require 'data/Calculation/LookupRef/VLOOKUP.php'; } + /** + * @dataProvider providerLOOKUP + * + * @param mixed $expectedResult + */ + public function testLOOKUP($expectedResult, ...$args) + { + $result = LookupRef::LOOKUP(...$args); + self::assertEquals($expectedResult, $result); + } + + public function providerLOOKUP() + { + return require 'data/Calculation/LookupRef/LOOKUP.php'; + } + /** * @dataProvider providerMATCH * diff --git a/tests/data/Calculation/LookupRef/LOOKUP.php b/tests/data/Calculation/LookupRef/LOOKUP.php new file mode 100644 index 00000000..667d7fd1 --- /dev/null +++ b/tests/data/Calculation/LookupRef/LOOKUP.php @@ -0,0 +1,111 @@ +