Skip to content
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
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a

### Added

- Nothing yet.
- Passthrough support (with some restrictions) for otherwise unsupported drawing elements. [Issue #4037](https://github.com/PHPOffice/PhpSpreadsheet/issues/4037) [Issue #4704](https://github.com/PHPOffice/PhpSpreadsheet/issues/4704) [PR #4712](https://github.com/PHPOffice/PhpSpreadsheet/pull/4712)

### Removed

Expand All @@ -31,8 +31,11 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a

- Slightly better support for escaped characters in Xlsx Reader/Writer. [Discussion #4724](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4724) [PR #4726](https://github.com/PHPOffice/PhpSpreadsheet/pull/4726)
- CODE/UNICODE and CHAR/UNICHAR. [PR #4727](https://github.com/PHPOffice/PhpSpreadsheet/pull/4727)
- Minor changes to TextGrid. [PR #4735](https://github.com/PHPOffice/PhpSpreadsheet/pull/4735)
- Minor changes to TextGrid. [PR #4735](https://github.com/PHPOffice/PhpSpreadsheet/pull/4735) [PR #4743](https://github.com/PHPOffice/PhpSpreadsheet/pull/4743)
- Single-character table names. [Issue #4739](https://github.com/PHPOffice/PhpSpreadsheet/issues/4739) [PR #4740](https://github.com/PHPOffice/PhpSpreadsheet/pull/4740)
- Improvements to SORT and SORTBY. [PR #4743](https://github.com/PHPOffice/PhpSpreadsheet/pull/4743)
- Coverage-related changes in Shared. [PR #4745](https://github.com/PHPOffice/PhpSpreadsheet/pull/4745)
- ListWorksheetInfo improvements for Xlsx and Ods. [Issue #3255](https://github.com/PHPOffice/PhpSpreadsheet/issues/3255) [PR #4746](https://github.com/PHPOffice/PhpSpreadsheet/pull/4746)

## 2025-11-24 - 5.3.0

Expand Down
16 changes: 9 additions & 7 deletions src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,31 @@ public function listWorksheetInfo(string $filename): array
];

// Loop through each child node of the table:table element reading
$currCells = 0;
$currRow = 0;
do {
$xml->read();
if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
$rowspan = $xml->getAttribute('table:number-rows-repeated');
$rowspan = empty($rowspan) ? 1 : (int) $rowspan;
$tmpInfo['totalRows'] += $rowspan;
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
$currCells = 0;
$currRow += $rowspan;
$currCol = 0;
// Step into the row
$xml->read();
do {
$doread = true;
if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
$mergeSize = $xml->getAttribute('table:number-columns-repeated');
$mergeSize = empty($mergeSize) ? 1 : (int) $mergeSize;
$currCol += $mergeSize;
if (!$xml->isEmptyElement) {
++$currCells;
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCol);
$tmpInfo['totalRows'] = $currRow;
$xml->next();
$doread = false;
}
} elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
$mergeSize = $xml->getAttribute('table:number-columns-repeated');
$currCells += (int) $mergeSize;
$currCol += (int) $mergeSize;
}
if ($doread) {
$xml->read();
Expand All @@ -217,7 +220,6 @@ public function listWorksheetInfo(string $filename): array
}
} while ($xml->name != 'table:table');

$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
$tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
$tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
$worksheetInfo[] = $tmpInfo;
Expand Down
17 changes: 14 additions & 3 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,26 @@ public function listWorksheetInfo(string $filename): array
$xml->setParserProperty(2, true);

$currCells = 0;
$currRow = 0;
while ($xml->read()) {
if ($xml->localName == 'row' && $xml->nodeType == XMLReader::ELEMENT && $xml->namespaceURI === $mainNS) {
$row = (int) $xml->getAttribute('r');
$tmpInfo['totalRows'] = $row;
if ($this->readEmptyCells) {
$tmpInfo['totalRows'] = $row;
} else {
$currRow = $row;
}
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
$currCells = 0;
} elseif ($xml->localName == 'c' && $xml->nodeType == XMLReader::ELEMENT && $xml->namespaceURI === $mainNS) {
$cell = $xml->getAttribute('r');
$currCells = $cell ? max($currCells, Coordinate::indexesFromString($cell)[0]) : ($currCells + 1);
if ($this->readEmptyCells || !$xml->isEmptyElement) {
if ($currRow !== 0) {
$tmpInfo['totalRows'] = $currRow;
$currRow = 0;
}
$cell = $xml->getAttribute('r');
$currCells = $cell ? max($currCells, Coordinate::indexesFromString($cell)[0]) : ($currCells + 1);
}
}
}
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
Expand Down
58 changes: 33 additions & 25 deletions tests/PhpSpreadsheetTests/Reader/Ods/OdsInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testReadFileProperties(): void

// Test "listWorksheetNames" method

self::assertEquals([
self::assertSame([
'Sheet1',
'Second Sheet',
], $reader->listWorksheetNames($filename));
Expand All @@ -47,7 +47,7 @@ public function testReadBadFileProperties(): void

// Test "listWorksheetNames" method

self::assertEquals([
self::assertSame([
'Sheet1',
'Second Sheet',
], $reader->listWorksheetNames(__FILE__));
Expand All @@ -56,14 +56,9 @@ public function testReadBadFileProperties(): void
public function testReadFileInfo(): void
{
$filename = 'tests/data/Reader/Ods/data.ods';

// Load into this instance
$reader = new Ods();

// Test "listWorksheetNames" method

$wsinfo = $reader->listWorkSheetInfo($filename);
self::assertEquals([
self::assertSame([
[
'worksheetName' => 'Sheet1',
'lastColumnLetter' => 'C',
Expand All @@ -74,10 +69,10 @@ public function testReadFileInfo(): void
],
[
'worksheetName' => 'Second Sheet',
'lastColumnLetter' => 'A',
'lastColumnIndex' => 0,
'lastColumnLetter' => 'B',
'lastColumnIndex' => 1,
'totalRows' => 2,
'totalColumns' => 1,
'totalColumns' => 2,
'sheetState' => 'visible',
],
], $wsinfo);
Expand All @@ -87,27 +82,40 @@ public function testReadBadFileInfo(): void
{
$this->expectException(ReaderException::class);
$filename = __FILE__;

// Load into this instance
$reader = new Ods();
$wsinfo = $reader->listWorkSheetInfo($filename);
}

// Test "listWorksheetNames" method

public function testReadFileInfoWithEmpties(): void
{
$filename = 'tests/data/Reader/Ods/RepeatedCells.ods';
$reader = new Ods();
$wsinfo = $reader->listWorkSheetInfo($filename);
self::assertEquals([
self::assertSame([
[
'worksheetName' => 'Sheet1',
'lastColumnLetter' => 'C',
'lastColumnIndex' => 2,
'totalRows' => 11,
'totalColumns' => 3,
'lastColumnLetter' => 'K',
'lastColumnIndex' => 10,
'totalRows' => 1,
'totalColumns' => 11,
'sheetState' => 'visible',
],
], $wsinfo);
}

public function testOneMoreWorksheetInfo(): void
{
$filename = 'tests/data/Reader/Ods/issue.4528.ods';
$reader = new Ods();
$wsinfo = $reader->listWorkSheetInfo($filename);
self::assertSame([
[
'worksheetName' => 'Second Sheet',
'lastColumnLetter' => 'A',
'lastColumnIndex' => 0,
'totalRows' => 2,
'totalColumns' => 1,
'worksheetName' => 'Francais',
'lastColumnLetter' => 'AZ',
'lastColumnIndex' => 51,
'totalRows' => 811,
'totalColumns' => 52,
'sheetState' => 'visible',
],
], $wsinfo);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/WorksheetInfoNamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,17 @@ public function testListWorksheetInfoChartSheet(): void
self::assertSame(0, $chartSheetInfo['totalRows']);
self::assertSame(0, $chartSheetInfo['totalColumns']);
}

public function testListWorksheetMissingRows(): void
{
$filename = 'tests/data/Reader/XLSX/issue.3255.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetInfo($filename);
self::assertSame(4, $actual[0]['totalColumns']);
self::assertSame(1048576, $actual[0]['totalRows']);
$reader->setReadEmptyCells(false);
$actual = $reader->listWorksheetInfo($filename);
self::assertSame(3, $actual[0]['totalColumns'], 'all cells in D have no data');
self::assertSame(15, $actual[0]['totalRows'], 'rows 16 and 1048576 have no cells with data');
}
}
Binary file added tests/data/Reader/XLSX/issue.3255.xlsx
Binary file not shown.