diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index 9672a6da..8fb2bc6d 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -627,7 +627,7 @@ class LookupRef return Functions::VALUE(); } - if (!is_array($arrayValues)) { + if (!is_array($arrayValues) || ($rowNum > count($arrayValues))) { return Functions::REF(); } @@ -647,7 +647,7 @@ class LookupRef if (isset($arrayColumn[$rowNum])) { $returnArray[] = $arrayColumn[$rowNum]; } else { - return $arrayValues[$rowNum]; + return [$rowNum => $arrayValues[$rowNum]]; } } else { return $arrayValues[$rowNum]; diff --git a/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php b/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php index 2671a529..40eb9c73 100644 --- a/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php @@ -66,4 +66,21 @@ class LookupRefTest extends PHPUnit_Framework_TestCase { return require 'data/Calculation/LookupRef/MATCH.php'; } + + /** + * @dataProvider providerINDEX + * @group fail19 + * + * @param mixed $expectedResult + */ + public function testINDEX($expectedResult, ...$args) + { + $result = LookupRef::INDEX(...$args); + self::assertEquals($expectedResult, $result); + } + + public function providerINDEX() + { + return require 'data/Calculation/LookupRef/INDEX.php'; + } } diff --git a/tests/data/Calculation/LookupRef/INDEX.php b/tests/data/Calculation/LookupRef/INDEX.php new file mode 100644 index 00000000..3a9c55a2 --- /dev/null +++ b/tests/data/Calculation/LookupRef/INDEX.php @@ -0,0 +1,76 @@ + ['R' => 1]], // Expected + // Input + [20 => ['R' => 1]], + ], + [ + '#VALUE!', // Expected + // Input + [ + 20 => ['R' => 1], + 21 => ['R' => 2], + ], + -1, + ], + [ + '#REF!', // Expected + // Input + [ + 20 => ['R' => 1], + 21 => ['R' => 2], + ], + 10, + ], + [ + [21 => ['R' => 2]], // Expected + // Input + [ + 20 => ['R' => 1], + 21 => ['R' => 2], + ], + 2, + ], + [ + [21 => ['R' => 2, 'S' => 4]], // Expected + // Input + [ + '20' => ['R' => 1, 'S' => 3], + '21' => ['R' => 2, 'S' => 4], + ], + 2, + 0, + ], + [ + '#VALUE!', // Expected + // Input + [ + '20' => ['R' => 1, 'S' => 3], + '21' => ['R' => 2, 'S' => 4], + ], + 2, + -1, + ], + [ + '#VALUE!', // Expected + // Input + [ + '20' => ['R' => 1, 'S' => 3], + '21' => ['R' => 2, 'S' => 4], + ], + 2, + 10, + ], + [ + 4, // Expected + // Input + [ + '20' => ['R' => 1, 'S' => 3], + '21' => ['R' => 2, 'S' => 4], + ], + 2, + 2, + ], +];