Skip to content

Commit 530e664

Browse files
committed
Table name as an constructor argument
Replaced worksheet argument with table name
1 parent 44d63f0 commit 530e664

File tree

7 files changed

+63
-81
lines changed

7 files changed

+63
-81
lines changed

samples/Table/01_Table.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252

5353
// Create Table
5454
$helper->log('Create Table');
55-
$table = new Table();
56-
$table->setName('Sales_Data');
57-
$table->setRange('A1:D17');
55+
$table = new Table('A1:D17', 'Sales_Data');
5856

5957
// Create Columns
6058
$table->getColumn('D')->setShowFilterButton(false);

samples/Table/03_Column_Formula.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949

5050
// Create Table
5151
$helper->log('Create Table');
52-
$table = new Table();
53-
$table->setName('Sales_Data');
52+
$table = new Table('A1:G15', 'Sales_Data');
5453
$table->setRange('A1:G15');
5554

5655
// Set Column Formula

src/PhpSpreadsheet/Worksheet/Table.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ class Table
6161
* Create a new Table.
6262
*
6363
* @param string $range (e.g. A1:D4)
64+
* @param string $name (e.g. Table1)
6465
*
6566
* @return $this
6667
*/
67-
public function __construct(string $range = '', ?Worksheet $worksheet = null)
68+
public function __construct(string $range = '', string $name = '')
6869
{
6970
$this->setRange($range);
70-
$this->setWorksheet($worksheet);
71+
$this->setName($name);
7172
$this->style = new TableStyle();
7273
}
7374

@@ -86,24 +87,26 @@ public function setName(string $name): self
8687
{
8788
$name = trim($name);
8889

89-
if (strlen($name) === 1 && in_array($name, ['C', 'c', 'R', 'r'])) {
90-
throw new PhpSpreadsheetException('The table name is invalid');
91-
}
92-
if (strlen($name) > 255) {
93-
throw new PhpSpreadsheetException('The table name cannot be longer than 255 characters');
94-
}
95-
// Check for A1 or R1C1 cell reference notation
96-
if (
97-
preg_match(Coordinate::A1_COORDINATE_REGEX, $name) ||
98-
preg_match('/^R\[?\-?[0-9]*\]?C\[?\-?[0-9]*\]?$/i', $name)
99-
) {
100-
throw new PhpSpreadsheetException('The table name can\'t be the same as a cell reference');
101-
}
102-
if (!preg_match('/^[\p{L}_\\\\]/iu', $name)) {
103-
throw new PhpSpreadsheetException('The table name must begin a name with a letter, an underscore character (_), or a backslash (\)');
104-
}
105-
if (!preg_match('/^[\p{L}_\\\\][\p{L}\p{M}0-9\._]+$/iu', $name)) {
106-
throw new PhpSpreadsheetException('The table name contains invalid characters');
90+
if (!empty($name)) {
91+
if (strlen($name) === 1 && in_array($name, ['C', 'c', 'R', 'r'])) {
92+
throw new PhpSpreadsheetException('The table name is invalid');
93+
}
94+
if (strlen($name) > 255) {
95+
throw new PhpSpreadsheetException('The table name cannot be longer than 255 characters');
96+
}
97+
// Check for A1 or R1C1 cell reference notation
98+
if (
99+
preg_match(Coordinate::A1_COORDINATE_REGEX, $name) ||
100+
preg_match('/^R\[?\-?[0-9]*\]?C\[?\-?[0-9]*\]?$/i', $name)
101+
) {
102+
throw new PhpSpreadsheetException('The table name can\'t be the same as a cell reference');
103+
}
104+
if (!preg_match('/^[\p{L}_\\\\]/iu', $name)) {
105+
throw new PhpSpreadsheetException('The table name must begin a name with a letter, an underscore character (_), or a backslash (\)');
106+
}
107+
if (!preg_match('/^[\p{L}_\\\\][\p{L}\p{M}0-9\._]+$/iu', $name)) {
108+
throw new PhpSpreadsheetException('The table name contains invalid characters');
109+
}
107110
}
108111

109112
$this->name = $name;

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,11 @@ public function addTableByColumnAndRow($columnIndex1, $row1, $columnIndex2, $row
22122212
{
22132213
$cellRange = Coordinate::stringFromColumnIndex($columnIndex1) . $row1 . ':' . Coordinate::stringFromColumnIndex($columnIndex2) . $row2;
22142214

2215-
return $this->addTable(new Table($cellRange, $this));
2215+
$table = new Table($cellRange);
2216+
$table->setWorksheet($this);
2217+
$this->addTable($table);
2218+
2219+
return $this;
22162220
}
22172221

22182222
/**

tests/PhpSpreadsheetTests/Worksheet/Table/RemoveTableTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testRemoveTable(): void
1212
{
1313
$sheet = $this->getSheet();
1414

15-
$table = new Table(self::INITIAL_RANGE, $sheet);
15+
$table = new Table(self::INITIAL_RANGE);
1616
$table->setName('Table1');
1717
$sheet->addTable($table);
1818

@@ -26,7 +26,7 @@ public function testRemoveCollection(): void
2626
{
2727
$sheet = $this->getSheet();
2828

29-
$table = new Table(self::INITIAL_RANGE, $sheet);
29+
$table = new Table(self::INITIAL_RANGE);
3030
$table->setName('Table1');
3131
$sheet->addTable($table);
3232

tests/PhpSpreadsheetTests/Worksheet/Table/TableStyleTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ class TableStyleTest extends SetupTeardown
1111

1212
public function testVariousSets(): void
1313
{
14-
$sheet = $this->getSheet();
15-
$table = new Table(self::INITIAL_RANGE, $sheet);
14+
$table = new Table(self::INITIAL_RANGE);
1615
$style = $table->getStyle();
1716

1817
$result = $style->setTheme(TableStyle::TABLE_STYLE_DARK1);
@@ -38,8 +37,7 @@ public function testVariousSets(): void
3837

3938
public function testTable(): void
4039
{
41-
$sheet = $this->getSheet();
42-
$table = new Table(self::INITIAL_RANGE, $sheet);
40+
$table = new Table(self::INITIAL_RANGE);
4341
$style = new TableStyle();
4442
$style->setTable($table);
4543
self::assertEquals($table, $style->getTable());

tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class TableTest extends SetupTeardown
1414
public function testToString(): void
1515
{
1616
$expectedResult = self::INITIAL_RANGE;
17-
$sheet = $this->getSheet();
18-
$table = new Table(self::INITIAL_RANGE, $sheet);
17+
$table = new Table(self::INITIAL_RANGE);
1918

2019
// magic __toString should return the active table range
2120
$result = (string) $table;
@@ -27,8 +26,7 @@ public function testToString(): void
2726
*/
2827
public function testValidTableNames(string $name, string $expected): void
2928
{
30-
$sheet = $this->getSheet();
31-
$table = new Table(self::INITIAL_RANGE, $sheet);
29+
$table = new Table(self::INITIAL_RANGE);
3230

3331
$result = $table->setName($name);
3432
self::assertInstanceOf(Table::class, $result);
@@ -38,6 +36,7 @@ public function testValidTableNames(string $name, string $expected): void
3836
public function validTableNamesProvider(): array
3937
{
4038
return [
39+
['', ''],
4140
['Table_1', 'Table_1'],
4241
['_table_2', '_table_2'],
4342
['\table_3', '\table_3'],
@@ -52,8 +51,7 @@ public function validTableNamesProvider(): array
5251
*/
5352
public function testInvalidTableNames(string $name): void
5453
{
55-
$sheet = $this->getSheet();
56-
$table = new Table(self::INITIAL_RANGE, $sheet);
54+
$table = new Table(self::INITIAL_RANGE);
5755

5856
$this->expectException(PhpSpreadsheetException::class);
5957

@@ -95,8 +93,7 @@ public function testUniqueTableName(): void
9593

9694
public function testVariousSets(): void
9795
{
98-
$sheet = $this->getSheet();
99-
$table = new Table(self::INITIAL_RANGE, $sheet);
96+
$table = new Table(self::INITIAL_RANGE);
10097

10198
$result = $table->setShowHeaderRow(false);
10299
self::assertInstanceOf(Table::class, $result);
@@ -110,15 +107,15 @@ public function testVariousSets(): void
110107
public function testGetWorksheet(): void
111108
{
112109
$sheet = $this->getSheet();
113-
$table = new Table(self::INITIAL_RANGE, $sheet);
110+
$table = new Table(self::INITIAL_RANGE);
111+
$sheet->addTable($table);
114112
$result = $table->getWorksheet();
115113
self::assertSame($sheet, $result);
116114
}
117115

118116
public function testSetWorksheet(): void
119117
{
120-
$sheet = $this->getSheet();
121-
$table = new Table(self::INITIAL_RANGE, $sheet);
118+
$table = new Table(self::INITIAL_RANGE);
122119
$spreadsheet = $this->getSpreadsheet();
123120
$sheet2 = $spreadsheet->createSheet();
124121
// Setters return the instance to implement the fluent interface
@@ -129,8 +126,7 @@ public function testSetWorksheet(): void
129126
public function testGetRange(): void
130127
{
131128
$expectedResult = self::INITIAL_RANGE;
132-
$sheet = $this->getSheet();
133-
$table = new Table(self::INITIAL_RANGE, $sheet);
129+
$table = new Table(self::INITIAL_RANGE);
134130

135131
// Result should be the active table range
136132
$result = $table->getRange();
@@ -141,7 +137,7 @@ public function testSetRange(): void
141137
{
142138
$sheet = $this->getSheet();
143139
$title = $sheet->getTitle();
144-
$table = new Table(self::INITIAL_RANGE, $sheet);
140+
$table = new Table(self::INITIAL_RANGE);
145141
$ranges = [
146142
'G1:J512' => "$title!G1:J512",
147143
'K1:N20' => 'K1:N20',
@@ -161,8 +157,7 @@ public function testSetRange(): void
161157
public function testClearRange(): void
162158
{
163159
$expectedResult = '';
164-
$sheet = $this->getSheet();
165-
$table = new Table(self::INITIAL_RANGE, $sheet);
160+
$table = new Table(self::INITIAL_RANGE);
166161

167162
// Setters return the instance to implement the fluent interface
168163
$result = $table->setRange('');
@@ -180,8 +175,7 @@ public function testSetRangeInvalidRange(string $range): void
180175
{
181176
$this->expectException(PhpSpreadsheetException::class);
182177

183-
$sheet = $this->getSheet();
184-
new Table($range, $sheet);
178+
new Table($range);
185179
}
186180

187181
public function invalidTableRangeProvider(): array
@@ -198,8 +192,7 @@ public function invalidTableRangeProvider(): array
198192
public function testGetColumnsEmpty(): void
199193
{
200194
// There should be no columns yet defined
201-
$sheet = $this->getSheet();
202-
$table = new Table(self::INITIAL_RANGE, $sheet);
195+
$table = new Table(self::INITIAL_RANGE);
203196
$result = $table->getColumns();
204197
self::assertIsArray($result);
205198
self::assertCount(0, $result);
@@ -212,8 +205,7 @@ public function testGetColumnOffset(): void
212205
'K' => 3,
213206
'M' => 5,
214207
];
215-
$sheet = $this->getSheet();
216-
$table = new Table(self::INITIAL_RANGE, $sheet);
208+
$table = new Table(self::INITIAL_RANGE);
217209

218210
// If we request a specific column by its column ID, we should get an
219211
// integer returned representing the column offset within the range
@@ -296,8 +288,7 @@ public function testGetInvalidColumnOffset(): void
296288
public function testSetColumnWithString(): void
297289
{
298290
$expectedResult = 'L';
299-
$sheet = $this->getSheet();
300-
$table = new Table(self::INITIAL_RANGE, $sheet);
291+
$table = new Table(self::INITIAL_RANGE);
301292

302293
// Setters return the instance to implement the fluent interface
303294
$result = $table->setColumn($expectedResult);
@@ -315,8 +306,7 @@ public function testSetColumnWithString(): void
315306
public function testSetInvalidColumnWithString(): void
316307
{
317308
$this->expectException(PhpSpreadsheetException::class);
318-
$sheet = $this->getSheet();
319-
$table = new Table(self::INITIAL_RANGE, $sheet);
309+
$table = new Table(self::INITIAL_RANGE);
320310

321311
$invalidColumn = 'A';
322312
$table->setColumn($invalidColumn);
@@ -326,8 +316,7 @@ public function testSetColumnWithColumnObject(): void
326316
{
327317
$expectedResult = 'M';
328318
$columnObject = new Column($expectedResult);
329-
$sheet = $this->getSheet();
330-
$table = new Table(self::INITIAL_RANGE, $sheet);
319+
$table = new Table(self::INITIAL_RANGE);
331320

332321
// Setters return the instance to implement the fluent interface
333322
$result = $table->setColumn($columnObject);
@@ -347,26 +336,23 @@ public function testSetInvalidColumnWithObject(): void
347336
$this->expectException(PhpSpreadsheetException::class);
348337

349338
$invalidColumn = 'E';
350-
$sheet = $this->getSheet();
351-
$table = new Table(self::INITIAL_RANGE, $sheet);
339+
$table = new Table(self::INITIAL_RANGE);
352340
$table->setColumn($invalidColumn);
353341
}
354342

355343
public function testSetColumnWithInvalidDataType(): void
356344
{
357345
$this->expectException(PhpSpreadsheetException::class);
358346

359-
$sheet = $this->getSheet();
360-
$table = new Table(self::INITIAL_RANGE, $sheet);
347+
$table = new Table(self::INITIAL_RANGE);
361348
$invalidColumn = 123.456;
362349
// @phpstan-ignore-next-line
363350
$table->setColumn($invalidColumn);
364351
}
365352

366353
public function testGetColumns(): void
367354
{
368-
$sheet = $this->getSheet();
369-
$table = new Table(self::INITIAL_RANGE, $sheet);
355+
$table = new Table(self::INITIAL_RANGE);
370356

371357
$columnIndexes = ['L', 'M'];
372358

@@ -391,8 +377,7 @@ public function testGetColumns(): void
391377

392378
public function testGetColumn(): void
393379
{
394-
$sheet = $this->getSheet();
395-
$table = new Table(self::INITIAL_RANGE, $sheet);
380+
$table = new Table(self::INITIAL_RANGE);
396381

397382
$columnIndexes = ['L', 'M'];
398383

@@ -410,8 +395,7 @@ public function testGetColumn(): void
410395

411396
public function testGetColumnByOffset(): void
412397
{
413-
$sheet = $this->getSheet();
414-
$table = new Table(self::INITIAL_RANGE, $sheet);
398+
$table = new Table(self::INITIAL_RANGE);
415399

416400
$columnIndexes = [
417401
0 => 'H',
@@ -430,8 +414,7 @@ public function testGetColumnByOffset(): void
430414

431415
public function testGetColumnIfNotSet(): void
432416
{
433-
$sheet = $this->getSheet();
434-
$table = new Table(self::INITIAL_RANGE, $sheet);
417+
$table = new Table(self::INITIAL_RANGE);
435418
// If we request a specific column by its column ID, we should
436419
// get a \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet\Table\Column object returned
437420
$result = $table->getColumn('K');
@@ -441,8 +424,7 @@ public function testGetColumnIfNotSet(): void
441424
public function testGetColumnWithoutRangeSet(): void
442425
{
443426
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
444-
$sheet = $this->getSheet();
445-
$table = new Table(self::INITIAL_RANGE, $sheet);
427+
$table = new Table(self::INITIAL_RANGE);
446428

447429
// Clear the range
448430
$table->setRange('');
@@ -451,8 +433,7 @@ public function testGetColumnWithoutRangeSet(): void
451433

452434
public function testClearRangeWithExistingColumns(): void
453435
{
454-
$sheet = $this->getSheet();
455-
$table = new Table(self::INITIAL_RANGE, $sheet);
436+
$table = new Table(self::INITIAL_RANGE);
456437
$expectedResult = '';
457438

458439
$columnIndexes = ['L', 'M', 'N'];
@@ -476,8 +457,7 @@ public function testClearRangeWithExistingColumns(): void
476457

477458
public function testSetRangeWithExistingColumns(): void
478459
{
479-
$sheet = $this->getSheet();
480-
$table = new Table(self::INITIAL_RANGE, $sheet);
460+
$table = new Table(self::INITIAL_RANGE);
481461
$expectedResult = 'G1:J512';
482462

483463
// These columns should be retained
@@ -509,7 +489,8 @@ public function testSetRangeWithExistingColumns(): void
509489
public function testClone(): void
510490
{
511491
$sheet = $this->getSheet();
512-
$table = new Table(self::INITIAL_RANGE, $sheet);
492+
$table = new Table(self::INITIAL_RANGE);
493+
$sheet->addTable($table);
513494
$columnIndexes = ['L', 'M'];
514495

515496
foreach ($columnIndexes as $columnIndex) {
@@ -546,8 +527,7 @@ public function testNoWorksheet(): void
546527

547528
public function testClearColumn(): void
548529
{
549-
$sheet = $this->getSheet();
550-
$table = new Table(self::INITIAL_RANGE, $sheet);
530+
$table = new Table(self::INITIAL_RANGE);
551531
$columnIndexes = ['J', 'K', 'L', 'M'];
552532

553533
foreach ($columnIndexes as $columnIndex) {

0 commit comments

Comments
 (0)