Skip to content

Commit 8930e63

Browse files
committed
Add Some Tests
1 parent 66ae679 commit 8930e63

File tree

8 files changed

+89
-40
lines changed

8 files changed

+89
-40
lines changed

src/PhpSpreadsheet/Calculation/LookupRef/Offset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private static function extractWorksheet(?string $cellAddress, Cell $cell): arra
103103

104104
$sheetName = '';
105105
if (str_contains($cellAddress, '!')) {
106-
[$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true, true);
106+
[$sheetName, $cellAddress] = Worksheet::extractSheetTitle($cellAddress, true);
107107
}
108108

109109
$worksheet = ($sheetName !== '')

src/PhpSpreadsheet/Worksheet/Validations.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static function validateCellRange(AddressRange|string|array $cellRange):
110110
return (string) $cellRange;
111111
}
112112

113-
public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet, bool $replaceDollar = false): string
113+
public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
114114
{
115115
// Uppercase coordinate
116116
$coordinate = strtoupper($coordinate);
@@ -120,9 +120,6 @@ public static function definedNameToCoordinate(string $coordinate, Worksheet $wo
120120
if ($defined !== null) {
121121
if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
122122
$coordinate = Preg::replace('/^=/', '', $defined->getValue());
123-
if ($replaceDollar) {
124-
$coordinate = str_replace('$', '', $coordinate);
125-
}
126123
}
127124
}
128125

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,9 +1396,10 @@ public function getColumnStyle(string $column): ?Style
13961396
public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate): Style
13971397
{
13981398
if (is_string($cellCoordinate)) {
1399-
$cellCoordinate = Validations::definedNameToCoordinate($cellCoordinate, $this, true);
1399+
$cellCoordinate = Validations::definedNameToCoordinate($cellCoordinate, $this);
14001400
}
14011401
$cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate);
1402+
$cellCoordinate = str_replace('$', '', $cellCoordinate);
14021403

14031404
// set this sheet as active
14041405
$this->getParentOrThrow()->setActiveSheetIndex($this->getParentOrThrow()->getIndex($this));

tests/PhpSpreadsheetTests/Calculation/Functions/Information/IsRefTest.php

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,41 @@
66

77
use PhpOffice\PhpSpreadsheet\NamedRange;
88
use PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef\AllSetupTeardown;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910

1011
class IsRefTest extends AllSetupTeardown
1112
{
12-
private bool $skipA13 = true;
13-
14-
public function testIsRef(): void
13+
#[DataProvider('providerIsRef')]
14+
public function testIsRef(mixed $expected, string $ref): void
1515
{
16+
if ($expected === 'incomplete') {
17+
self::markTestIncomplete('Calculation is too complicated');
18+
}
1619
$sheet = $this->getSheet();
1720

1821
$sheet->getParentOrThrow()->addDefinedName(new NamedRange('NAMED_RANGE', $sheet, 'C1'));
22+
$sheet->getCell('A1')->setValue("=ISREF($ref)");
23+
self::assertSame($expected, $sheet->getCell('A1')->getCalculatedValue());
24+
}
1925

20-
$sheet->getCell('A1')->setValue('=ISREF(B1)');
21-
$sheet->getCell('A2')->setValue('=ISREF(B1:B2)');
22-
$sheet->getCell('A3')->setValue('=ISREF(B1:D4 C1:C5)');
23-
$sheet->getCell('A4')->setValue('=ISREF("PHP")');
24-
$sheet->getCell('A5')->setValue('=ISREF(B1*B2)');
25-
$sheet->getCell('A6')->setValue('=ISREF(Worksheet2!B1)');
26-
$sheet->getCell('A7')->setValue('=ISREF(NAMED_RANGE)');
27-
$sheet->getCell('A8')->setValue('=ISREF(INDIRECT("' . $sheet->getTitle() . '" & "!" & "A1"))');
28-
$sheet->getCell('A9')->setValue('=ISREF(INDIRECT("A1"))');
29-
$sheet->getCell('A10')->setValue('=ISREF(INDIRECT("Invalid Worksheet" & "!" & "A1"))');
30-
$sheet->getCell('A11')->setValue('=ISREF(INDIRECT("Invalid Worksheet" & "!A1"))');
31-
$sheet->getCell('A12')->setValue('=ISREF(ZZZ1)');
32-
$sheet->getCell('A13')->setValue('=ISREF(CHOOSE(2, A1, B1, C1))');
33-
34-
self::assertTrue($sheet->getCell('A1')->getCalculatedValue()); // Cell Reference
35-
self::assertTrue($sheet->getCell('A2')->getCalculatedValue()); // Cell Range
36-
self::assertTrue($sheet->getCell('A3')->getCalculatedValue()); // Complex Cell Range
37-
self::assertFalse($sheet->getCell('A4')->getCalculatedValue()); // Text String
38-
self::assertFalse($sheet->getCell('A5')->getCalculatedValue()); // Result of a math expression
39-
self::assertTrue($sheet->getCell('A6')->getCalculatedValue()); // Cell Reference with worksheet
40-
self::assertTrue($sheet->getCell('A7')->getCalculatedValue()); // Named Range
41-
self::assertTrue($sheet->getCell('A8')->getCalculatedValue()); // Indirect to a Cell Reference
42-
self::assertTrue($sheet->getCell('A9')->getCalculatedValue()); // Indirect to a Worksheet/Cell Reference
43-
self::assertFalse($sheet->getCell('A10')->getCalculatedValue()); // Indirect to an Invalid Worksheet/Cell Reference
44-
self::assertFalse($sheet->getCell('A11')->getCalculatedValue()); // Indirect to an Invalid Worksheet/Cell Reference
45-
self::assertFalse($sheet->getCell('A12')->getCalculatedValue()); // Invalid Cell Reference
46-
if ($this->skipA13) {
47-
self::markTestIncomplete('Calculation for A13 is too complicated');
48-
}
49-
self::assertTrue($sheet->getCell('A13')->getCalculatedValue()); // returned Cell Reference
26+
public static function providerIsRef(): array
27+
{
28+
return [
29+
'cell reference' => [true, 'B1'],
30+
'invalid cell reference' => [false, 'ZZZ1'],
31+
'cell range' => [true, 'B1:B2'],
32+
'complex cell range' => [true, 'B1:D4 C1:C5'],
33+
'text string' => [false, '"PHP"'],
34+
'math expression' => [false, 'B1*B2'],
35+
'unquoted sheet name' => [true, 'Worksheet2!B1'],
36+
'quoted sheet name' => [true, "'Worksheet2'!B1:B2"],
37+
'quoted sheet name with apostrophe' => [true, "'Work''sheet2'!B1:B2"],
38+
'named range' => [true, 'NAMED_RANGE'],
39+
'unknown named range' => ['#NAME?', 'xNAMED_RANGE'],
40+
'indirect to a cell reference' => [true, 'INDIRECT("A1")'],
41+
'indirect to a worksheet/cell reference' => [true, 'INDIRECT("\'Worksheet\'!A1")'],
42+
'indirect to invalid worksheet/cell reference' => [false, 'INDIRECT("\'Invalid Worksheet\'!A1")'],
43+
'returned cell reference' => ['incomplete', 'CHOOSE(2, A1, B1, C1)'],
44+
];
5045
}
5146
}

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/ColumnOnSpreadsheetTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,22 @@ public function testCOLUMNLocalDefinedName(): void
5454
$result = $sheet->getCell('B3')->getCalculatedValue();
5555
self::assertSame('#NAME?', $result);
5656
}
57+
58+
public function testCOLUMNSheetWithApostrophe(): void
59+
{
60+
$this->setArrayAsValue();
61+
$sheet = $this->getSheet();
62+
63+
$sheet1 = $this->getSpreadsheet()->createSheet();
64+
$sheet1->setTitle("apo''strophe");
65+
$this->getSpreadsheet()->addNamedRange(new NamedRange('newnr', $sheet1, '$F$5:$H$5', true)); // defined locally, only usable on sheet1
66+
67+
$sheet1->getCell('B3')->setValue('=COLUMN(newnr)');
68+
$result = $sheet1->getCell('B3')->getCalculatedValue();
69+
self::assertSame(6, $result);
70+
71+
$sheet->getCell('B3')->setValue('=COLUMN(newnr)');
72+
$result = $sheet->getCell('B3')->getCalculatedValue();
73+
self::assertSame('#NAME?', $result);
74+
}
5775
}

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/OffsetTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
use PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
88
use PhpOffice\PhpSpreadsheet\NamedRange;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910

1011
class OffsetTest extends AllSetupTeardown
1112
{
12-
#[\PHPUnit\Framework\Attributes\DataProvider('providerOFFSET')]
13+
#[DataProvider('providerOFFSET')]
1314
public function testOFFSET(mixed $expectedResult, null|string $cellReference = null): void
1415
{
1516
$result = LookupRef\Offset::OFFSET($cellReference);
@@ -58,4 +59,19 @@ public function testOffsetNamedRange(): void
5859

5960
self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
6061
}
62+
63+
public function testOffsetNamedRangeApostropheSheet(): void
64+
{
65+
$workSheet = $this->getSheet();
66+
$workSheet->setTitle("apo'strophe");
67+
$workSheet->setCellValue('A1', 1);
68+
$workSheet->setCellValue('A2', 2);
69+
70+
$this->getSpreadsheet()->addNamedRange(new NamedRange('demo', $workSheet, '=$A$1'));
71+
72+
$workSheet->setCellValue('B1', '=demo');
73+
$workSheet->setCellValue('B2', '=OFFSET(demo, 1, 0)');
74+
75+
self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
76+
}
6177
}

tests/PhpSpreadsheetTests/Reader/Ods/DefinedNamesTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ public function testDefinedNamesValue(): void
3131
$spreadsheet->disconnectWorksheets();
3232
}
3333

34+
public function testDefinedNamesApostropheValue(): void
35+
{
36+
$filename = 'tests/data/Reader/Ods/DefinedNames.apostrophe.ods';
37+
$reader = new Ods();
38+
$spreadsheet = $reader->load($filename);
39+
$calculation = Calculation::getInstance($spreadsheet);
40+
$calculation->setInstanceArrayReturnType(
41+
Calculation::RETURN_ARRAY_AS_VALUE
42+
);
43+
$worksheet = $spreadsheet->getActiveSheet();
44+
self::assertSame("apo'strophe", $worksheet->getTitle());
45+
46+
$firstDefinedNameValue = $worksheet->getCell('First')->getValue();
47+
$secondDefinedNameValue = $worksheet->getCell('Second')->getValue();
48+
$calculatedFormulaValue = $worksheet->getCell('B2')->getCalculatedValue();
49+
50+
self::assertSame(3, $firstDefinedNameValue);
51+
self::assertSame(4, $secondDefinedNameValue);
52+
self::assertSame(12, $calculatedFormulaValue);
53+
$spreadsheet->disconnectWorksheets();
54+
}
55+
3456
public function testDefinedNamesArray(): void
3557
{
3658
$filename = 'tests/data/Reader/Ods/DefinedNames.ods';
Binary file not shown.

0 commit comments

Comments
 (0)