mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-30 20:18:00 +00:00
14a989a7e3
The original issue leading to this PR was fixed by correctly treating a token in Calculator as a function rather than a defined name. However, it *should* have worked even when treating it as a defined name. There was a problem because Calculator was raising an exception for a missing defined name rather than returning `#NAME?`. It is now changed to return the error. That change initially had some adverse affects for functions ROW, ROWS, COLUMN, and COLUMNS. Those are fixed to handle the change correctly. As a bonus, a test for each which had been commented out, because it didn't work, is now uncommented and works correctly. One test for ISFORMULA and one for ISREF were also changed - the old expected result did not reflect Excel's behavior and the new one does. Sheet title and Defined Name matching used `strtoupper` to achieve case-insensitive compares. This handles only ASCII characters. They are changed to use a conversion routine which handles non-ASCII UTF-8 character sets.
77 lines
2.9 KiB
PHP
77 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\NamedRange;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
class ColumnOnSpreadsheetTest extends AllSetupTeardown
|
|
{
|
|
#[DataProvider('providerCOLUMNonSpreadsheet')]
|
|
public function testColumnOnSpreadsheet(mixed $expectedResult, string $cellReference = 'omitted'): void
|
|
{
|
|
$this->mightHaveException($expectedResult);
|
|
$this->setArrayAsValue();
|
|
$sheet = $this->getSheet();
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('namedrangex', $sheet, '$E$2:$E$6'));
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('namedrangey', $sheet, '$F$2:$H$2'));
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('namedrange3', $sheet, '$F$4:$H$4'));
|
|
|
|
$sheet1 = $this->getSpreadsheet()->createSheet();
|
|
$sheet1->setTitle('OtherSheet');
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('localname', $sheet1, '$F$6:$H$6', true));
|
|
|
|
if ($cellReference === 'omitted') {
|
|
$sheet->getCell('B3')->setValue('=COLUMN()');
|
|
} else {
|
|
$sheet->getCell('B3')->setValue("=COLUMN($cellReference)");
|
|
}
|
|
|
|
$result = $sheet->getCell('B3')->getCalculatedValue();
|
|
self::assertSame($expectedResult, $result);
|
|
}
|
|
|
|
public static function providerCOLUMNonSpreadsheet(): array
|
|
{
|
|
return require 'tests/data/Calculation/LookupRef/COLUMNonSpreadsheet.php';
|
|
}
|
|
|
|
public function testCOLUMNLocalDefinedName(): void
|
|
{
|
|
$this->setArrayAsValue();
|
|
$sheet = $this->getSheet();
|
|
|
|
$sheet1 = $this->getSpreadsheet()->createSheet();
|
|
$sheet1->setTitle('OtherSheet');
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('newnr', $sheet1, '$F$5:$H$5', true)); // defined locally, only usable on sheet1
|
|
|
|
$sheet1->getCell('B3')->setValue('=COLUMN(newnr)');
|
|
$result = $sheet1->getCell('B3')->getCalculatedValue();
|
|
self::assertSame(6, $result);
|
|
|
|
$sheet->getCell('B3')->setValue('=COLUMN(newnr)');
|
|
$result = $sheet->getCell('B3')->getCalculatedValue();
|
|
self::assertSame('#NAME?', $result);
|
|
}
|
|
|
|
public function testCOLUMNSheetWithApostrophe(): void
|
|
{
|
|
$this->setArrayAsValue();
|
|
$sheet = $this->getSheet();
|
|
|
|
$sheet1 = $this->getSpreadsheet()->createSheet();
|
|
$sheet1->setTitle("apo'strophe");
|
|
$this->getSpreadsheet()->addNamedRange(new NamedRange('newnr', $sheet1, '$F$5:$H$5', true)); // defined locally, only usable on sheet1
|
|
|
|
$sheet1->getCell('B3')->setValue('=COLUMN(newnr)');
|
|
$result = $sheet1->getCell('B3')->getCalculatedValue();
|
|
self::assertSame(6, $result);
|
|
|
|
$sheet->getCell('B3')->setValue('=COLUMN(newnr)');
|
|
$result = $sheet->getCell('B3')->getCalculatedValue();
|
|
self::assertSame('#NAME?', $result);
|
|
}
|
|
}
|