Files
oleibman 40f7cd00db Consistent Usage of Column and Row Limits
We have identical constants defined in several places, and use literals in others. We aren't consistent in checking limits. This PR makes the use of the constants in Cell/AddressRange the "official" source, deprecates all other constants, and substitutes the constants wherever literals are used. A number of different edge case tests are added.

During testing, I discovered that `columnIndexFromString` correctly throws an exception for 4-character string, but allows `XFE` through `ZZZ`, all of which are also invalid. There are similar inconsistencies with related routines, and this PR attempts to make them operate consistently. One suprise is that throwing for `row=0` causes serious regression problems, so it continues to be permitted (but the high row limit is enforced).

Further, Reference Helper sometimes dips into negative numbers, resulting in totally unexpected results (-1 affects column Z, -2 column Y, etc.). It is changed to ignore rows and columns outside the limits.
2026-03-03 19:04:11 -08:00

123 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;
class WorksheetInfoNamesTest extends TestCase
{
public function testListWorksheetInfo(): void
{
$filename = 'tests/data/Reader/XLSX/rowColumnAttributeTest.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetInfo($filename);
$expected = [
[
'worksheetName' => 'Sheet1',
'lastColumnLetter' => 'F',
'lastColumnIndex' => 5,
'totalRows' => '6',
'totalColumns' => 6,
'sheetState' => 'visible',
],
];
self::assertEquals($expected, $actual);
}
public function testListWorksheetInfoNamespace(): void
{
$filename = 'tests/data/Reader/XLSX/namespaces.xlsx';
$file = 'zip://';
$file .= $filename;
$file .= '#xl/workbook.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file');
} else {
self::assertStringContainsString('<x:workbook ', $data);
}
$reader = new Xlsx();
$actual = $reader->listWorksheetInfo($filename);
$expected = [
[
'worksheetName' => 'transactions',
'lastColumnLetter' => 'K',
'lastColumnIndex' => 10,
'totalRows' => 2,
'totalColumns' => 11,
'sheetState' => 'visible',
],
];
self::assertEquals($expected, $actual);
}
public function testListWorksheetNames(): void
{
$filename = 'tests/data/Reader/XLSX/rowColumnAttributeTest.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetNames($filename);
$expected = ['Sheet1'];
self::assertEquals($expected, $actual);
}
public function testListWorksheetNamesNamespace(): void
{
$filename = 'tests/data/Reader/XLSX/namespaces.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetNames($filename);
$expected = ['transactions'];
self::assertEquals($expected, $actual);
}
public function testListWorksheetNamesChartSheet(): void
{
$filename = 'tests/data/Reader/XLSX/ChartSheet.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetNames($filename);
$expected = ['Sheet1', 'Chart1'];
self::assertEquals($expected, $actual);
}
public function testListWorksheetInfoChartSheet(): void
{
$filename = 'tests/data/Reader/XLSX/ChartSheet.xlsx';
$reader = new Xlsx();
$actual = $reader->listWorksheetInfo($filename);
$chartSheetInfo = $actual[1];
self::assertSame('Chart1', $chartSheetInfo['worksheetName']);
self::assertSame(-1, $chartSheetInfo['lastColumnIndex']);
self::assertSame(0, $chartSheetInfo['totalRows']);
self::assertSame(0, $chartSheetInfo['totalColumns']);
self::assertSame('', $chartSheetInfo['lastColumnLetter']);
}
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');
}
}