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

51 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Information;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef\AllSetupTeardown;
use PHPUnit\Framework\Attributes\DataProvider;
class IsRefTest extends AllSetupTeardown
{
#[DataProvider('providerIsRef')]
public function testIsRef(mixed $expected, string $ref): void
{
if ($expected === 'incomplete') {
self::markTestIncomplete('Calculation is too complicated');
}
$sheet = $this->getSheet();
$sheet->getParentOrThrow()->addDefinedName(new NamedRange('NAMED_RANGE', $sheet, 'C1'));
$sheet->getCell('A1')->setValue("=ISREF($ref)");
self::assertSame($expected, $sheet->getCell('A1')->getCalculatedValue());
}
public static function providerIsRef(): array
{
return [
'cell reference' => [true, 'B1'],
'invalid cell reference' => [false, 'ZZZ1'],
'last valid column' => [true, 'XFD1'],
'beyond last valid column' => [false, 'XFE1'],
'last valid row' => [true, 'A1048576'],
'beyond last valid row' => [false, 'A1048577'],
'cell range' => [true, 'B1:B2'],
'complex cell range' => [true, 'B1:D4 C1:C5'],
'text string' => [false, '"PHP"'],
'math expression' => [false, 'B1*B2'],
'unquoted sheet name' => [true, 'Worksheet2!B1'],
'quoted sheet name' => [true, "'Worksheet2'!B1:B2"],
'quoted sheet name with apostrophe' => [true, "'Work''sheet2'!B1:B2"],
'named range' => [true, 'NAMED_RANGE'],
'unknown named range' => [false, 'xNAMED_RANGE'],
'indirect to a cell reference' => [true, 'INDIRECT("A1")'],
'indirect to a worksheet/cell reference' => [true, 'INDIRECT("\'Worksheet\'!A1")'],
'indirect to invalid worksheet/cell reference' => [false, 'INDIRECT("\'Invalid Worksheet\'!A1")'],
'returned cell reference' => ['incomplete', 'CHOOSE(2, A1, B1, C1)'],
];
}
}