Skip to content

Commit c196847

Browse files
authored
Merge pull request #4088 from oleibman/issue64
Changes to INDEX Function
2 parents 6c1b00b + 59dfd1f commit c196847

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4141
- Problem rendering line chart with missing plot label. [PR #4074](https://github.com/PHPOffice/PhpSpreadsheet/pull/4074)
4242
- More RTL in Xlsx/Html Comments [Issue #4004](https://github.com/PHPOffice/PhpSpreadsheet/issues/4004) [PR #4065](https://github.com/PHPOffice/PhpSpreadsheet/pull/4065)
4343
- Empty String in sharedStrings. [Issue #4063](https://github.com/PHPOffice/PhpSpreadsheet/issues/4063) [PR #4064](https://github.com/PHPOffice/PhpSpreadsheet/pull/4064)
44+
- Changes to INDEX function. [Issue #64](https://github.com/PHPOffice/PhpSpreadsheet/issues/64) [PR #4088](https://github.com/PHPOffice/PhpSpreadsheet/pull/4088)
45+
- Ods Reader and Whitespace Text Nodes. [Issue #804](https://github.com/PHPOffice/PhpSpreadsheet/issues/804) [PR #4087](https://github.com/PHPOffice/PhpSpreadsheet/pull/4087)
4446
- 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)
4547
- Treat invalid formulas as strings. [Issue #1310](https://github.com/PHPOffice/PhpSpreadsheet/issues/1310) [PR #4073](https://github.com/PHPOffice/PhpSpreadsheet/pull/4073)
4648

src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
8181
}
8282

8383
$rowNum = $rowNum ?? 0;
84-
$originalColumnNum = $columnNum;
8584
$columnNum = $columnNum ?? 0;
8685

8786
try {
@@ -91,6 +90,17 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
9190
return $e->getMessage();
9291
}
9392

93+
if (is_array($matrix) && count($matrix) === 1 && $rowNum > 1) {
94+
$matrixKey = array_keys($matrix)[0];
95+
if (is_array($matrix[$matrixKey])) {
96+
$tempMatrix = [];
97+
foreach ($matrix[$matrixKey] as $key => $value) {
98+
$tempMatrix[$key] = [$value];
99+
}
100+
$matrix = $tempMatrix;
101+
}
102+
}
103+
94104
if (!is_array($matrix) || ($rowNum > count($matrix))) {
95105
return ExcelError::REF();
96106
}
@@ -101,9 +111,6 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
101111
if ($columnNum > count($columnKeys)) {
102112
return ExcelError::REF();
103113
}
104-
if ($originalColumnNum === null && 1 < count($columnKeys)) {
105-
return ExcelError::REF();
106-
}
107114

108115
if ($columnNum === 0) {
109116
return self::extractRowValue($matrix, $rowKeys, $rowNum);

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/IndexOnSpreadsheetTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,37 @@ public static function providerINDEXonSpreadsheet(): array
3434
{
3535
return require 'tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php';
3636
}
37+
38+
/**
39+
* @dataProvider providerIndexLiteralArrays
40+
*/
41+
public function testLiteralArrays(mixed $expectedResult, string $indexArgs): void
42+
{
43+
$sheet = $this->getSheet();
44+
$sheet->getCell('A10')->setValue(10);
45+
$sheet->getCell('B10')->setValue(11);
46+
$sheet->getCell('C10')->setValue(12);
47+
$sheet->getCell('D10')->setValue(13);
48+
$sheet->getCell('X10')->setValue(10);
49+
$sheet->getCell('X11')->setValue(11);
50+
$sheet->getCell('X12')->setValue(12);
51+
$sheet->getCell('X13')->setValue(13);
52+
$sheet->getCell('A1')->setValue("=INDEX($indexArgs)");
53+
$result = $sheet->getCell('A1')->getCalculatedValue();
54+
self::assertEquals($expectedResult, $result);
55+
}
56+
57+
public static function providerIndexLiteralArrays(): array
58+
{
59+
return [
60+
'issue 64' => ['Fourth', '{"First","Second","Third","Fourth","Fifth","Sixth","Seventh"}, 4'],
61+
'issue 64 selecting first "row"' => ['First', '{"First","Second","Third","Fourth","Fifth","Sixth","Seventh"}, 1'],
62+
'array result condensed to single value' => [40, '{10,11;20,21;30,31;40,41;50,51;60,61},4'],
63+
'both row and column' => [41, '{10,11;20,21;30,31;40,41;50,51;60,61},4,2'],
64+
'1*1 array' => ['first', '{"first"},1'],
65+
'array expressed in rows' => [20, '{10;20;30;40},2'],
66+
'spreadsheet single row' => [11, 'A10:D10,2'],
67+
'spreadsheet single column' => [13, 'X10:X13,4'],
68+
];
69+
}
3770
}

tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
2,
8383
],
8484
'Column number omitted from 2-column matrix' => [
85-
'#REF!', // Expected
85+
'abc', // Expected
8686
[
8787
['abc', 'def'],
8888
['xyz', 'tuv'],

0 commit comments

Comments
 (0)