Skip to content

Changes to INDEX Function #4088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 11 additions & 4 deletions src/PhpSpreadsheet/Calculation/LookupRef/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
}

$rowNum = $rowNum ?? 0;
$originalColumnNum = $columnNum;
$columnNum = $columnNum ?? 0;

try {
Expand All @@ -91,6 +90,17 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
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();
}
Expand All @@ -101,9 +111,6 @@ public static function index(mixed $matrix, mixed $rowNum = 0, mixed $columnNum
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,37 @@ public static function providerINDEXonSpreadsheet(): array
{
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'],
];
}
}
2 changes: 1 addition & 1 deletion tests/data/Calculation/LookupRef/INDEXonSpreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
2,
],
'Column number omitted from 2-column matrix' => [
'#REF!', // Expected
'abc', // Expected
[
['abc', 'def'],
['xyz', 'tuv'],
Expand Down
Loading