Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
Fails to calculate INDEX() function
Sample formula:
=INDEX({"First","Second","Third","Fourth","Fifth","Sixth","Seventh"}, 4)

Errors thrown:
Undefined offset: 3 in `/var/www/html/hoosh/excel_v2/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 603`
`Undefined index: in /var/www/html/hoosh/excel_v2/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 610`


Pull request:
Changes to INDEX Function
Fix #64 (really!), closed as stale in December 2017, another in our "better late than never" series. Excel's INDEX function doesn't really behave quite as described. If a single row is used as an argument, either in literal form `{item1, item2, item3}` or expressed as a range `A1:A6`, INDEX is happy to evaluate the array as if each entry were a row rather than a single item. PhpSpreadsheet is changed to do likewise.

INDEX also returned `#REF!` when it would normally return an array (which would often be reduced to its leftmost topmost entry later). This code is deleted, invalidating one existing test, and INDEX will now operate like other functions which can return arrays.

This is:

- [x] a bugfix
- [ ] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [ ] Changes are covered by unit tests
  - [ ] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary

### Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8d4abd8f..32ad63b12 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
 - Problem rendering line chart with missing plot label. [PR #4074](https://github.com/PHPOffice/PhpSpreadsheet/pull/4074)
 - More RTL in Xlsx/Html Comments [Issue #4004](https://github.com/PHPOffice/PhpSpreadsheet/issues/4004) [PR #4065](https://github.com/PHPOffice/PhpSpreadsheet/pull/4065)
 - Empty String in sharedStrings. [Issue #4063](https://github.com/PHPOffice/PhpSpreadsheet/issues/4063) [PR #4064](https://github.com/PHPOffice/PhpSpreadsheet/pull/4064)
+- Changes to INDEX function. [Issue #64](https://github.com/PHPOffice/PhpSpreadsheet/issues/64) [PR #4088](https://github.com/PHPOffice/PhpSpreadsheet/pull/4088)
+- Ods Reader and Whitespace Text Nodes. [Issue #804](https://github.com/PHPOffice/PhpSpreadsheet/issues/804) [PR #4087](https://github.com/PHPOffice/PhpSpreadsheet/pull/4087)
 - Ods Xml Reader and Whitespace Text Nodes. [Issue #804](https://github.com/PHPOffice/PhpSpreadsheet/issues/804) [PR #4087](https://github.com/PHPOffice/PhpSpreadsheet/pull/4087)
 - Treat invalid formulas as strings. [Issue #1310](https://github.com/PHPOffice/PhpSpreadsheet/issues/1310) [PR #4073](https://github.com/PHPOffice/PhpSpreadsheet/pull/4073)
 
diff --git a/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php b/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php
index d578854de..228b46448 100644
--- a/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php
+++ b/src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php
@@ -81,7 +81,6 @@ class Matrix
         }
 
         $rowNum = $rowNum ?? 0;
-        $originalColumnNum = $columnNum;
         $columnNum = $columnNum ?? 0;
 
         try {
@@ -91,6 +90,17 @@ class Matrix
             return $e->getMessage();
         }
 
+        if (is_array($matrix) && count($matrix) === 1 && $rowNum > 1) {
+            $matrixKey = array_keys($matrix)[0];
+            if (is_array($matrix[$matrixKey])) {
+                $tempMatrix = [];
+                foreach ($matrix[$matrixKey] as $key => $value) {
+                    $tempMatrix[$key] = [$value];
+                }
+                $matrix = $tempMatrix;
+            }
+        }
+
         if (!is_array($matrix) || ($rowNum > count($matrix))) {
             return ExcelError::REF();
         }
@@ -101,9 +111,6 @@ class Matrix
         if ($columnNum > count($columnKeys)) {
             return ExcelError::REF();
         }
-        if ($originalColumnNum === null && 1 < count($columnKeys)) {
-            return ExcelError::REF();
-        }
 
         if ($columnNum === 0) {
             return self::extractRowValue($matrix, $rowKeys, $rowNum);


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexOnSpreadsheetTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexOnSpreadsheetTest.php
index e893fc7f3..3f508405a 100644
--- a/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexOnSpreadsheetTest.php
+++ b/tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexOnSpreadsheetTest.php
@@ -34,4 +34,37 @@ class IndexOnSpreadsheetTest extends AllSetupTeardown
     {
         return require 'tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php';
     }
+
+    /**
+     * @dataProvider providerIndexLiteralArrays
+     */
+    public function testLiteralArrays(mixed $expectedResult, string $indexArgs): void
+    {
+        $sheet = $this->getSheet();
+        $sheet->getCell('A10')->setValue(10);
+        $sheet->getCell('B10')->setValue(11);
+        $sheet->getCell('C10')->setValue(12);
+        $sheet->getCell('D10')->setValue(13);
+        $sheet->getCell('X10')->setValue(10);
+        $sheet->getCell('X11')->setValue(11);
+        $sheet->getCell('X12')->setValue(12);
+        $sheet->getCell('X13')->setValue(13);
+        $sheet->getCell('A1')->setValue("=INDEX($indexArgs)");
+        $result = $sheet->getCell('A1')->getCalculatedValue();
+        self::assertEquals($expectedResult, $result);
+    }
+
+    public static function providerIndexLiteralArrays(): array
+    {
+        return [
+            'issue 64' => ['Fourth', '{"First","Second","Third","Fourth","Fifth","Sixth","Seventh"}, 4'],
+            'issue 64 selecting first "row"' => ['First', '{"First","Second","Third","Fourth","Fifth","Sixth","Seventh"}, 1'],
+            'array result condensed to single value' => [40, '{10,11;20,21;30,31;40,41;50,51;60,61},4'],
+            'both row and column' => [41, '{10,11;20,21;30,31;40,41;50,51;60,61},4,2'],
+            '1*1 array' => ['first', '{"first"},1'],
+            'array expressed in rows' => [20, '{10;20;30;40},2'],
+            'spreadsheet single row' => [11, 'A10:D10,2'],
+            'spreadsheet single column' => [13, 'X10:X13,4'],
+        ];
+    }
 }
diff --git a/tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php b/tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php
index 76f6ddd46..b2202704d 100644
--- a/tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php
+++ b/tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php
@@ -82,7 +82,7 @@ return [
         2,
     ],
     'Column number omitted from 2-column matrix' => [
-        '#REF!', // Expected
+        'abc', // Expected
         [
             ['abc', 'def'],
             ['xyz', 'tuv'],