Skip to content

Commit 534cbc0

Browse files
committed
Accept table range as AddressRange and array
Table constructor now accepts AddressRange and array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow]
1 parent d414f13 commit 534cbc0

File tree

3 files changed

+39
-42
lines changed

3 files changed

+39
-42
lines changed

src/PhpSpreadsheet/Worksheet/Table.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Worksheet;
44

5+
use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
56
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
67
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
78
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
@@ -61,12 +62,13 @@ class Table
6162
/**
6263
* Create a new Table.
6364
*
64-
* @param string $range (e.g. A1:D4)
65+
* @param AddressRange|array<int>|string $range
66+
* A simple string containing a Cell range like 'A1:E10' is permitted
67+
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
68+
* or an AddressRange object.
6569
* @param string $name (e.g. Table1)
66-
*
67-
* @return $this
6870
*/
69-
public function __construct(string $range = '', string $name = '')
71+
public function __construct($range = '', string $name = '')
7072
{
7173
$this->setRange($range);
7274
$this->setName($name);
@@ -161,11 +163,18 @@ public function getRange(): string
161163

162164
/**
163165
* Set Table Cell Range.
166+
*
167+
* @param AddressRange|array<int>|string $range
168+
* A simple string containing a Cell range like 'A1:E10' is permitted
169+
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
170+
* or an AddressRange object.
164171
*/
165-
public function setRange(string $range): self
172+
public function setRange($range = ''): self
166173
{
167174
// extract coordinate
168-
[$worksheet, $range] = Worksheet::extractSheetTitle($range, true);
175+
if ($range !== '') {
176+
[, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true);
177+
}
169178
if (empty($range)) {
170179
// Discard all column rules
171180
$this->columns = [];

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,27 +2128,6 @@ public function addTable(Table $table): self
21282128
return $this;
21292129
}
21302130

2131-
/**
2132-
* Add Table Range by using numeric cell coordinates.
2133-
*
2134-
* @param int $columnIndex1 Numeric column coordinate of the first cell
2135-
* @param int $row1 Numeric row coordinate of the first cell
2136-
* @param int $columnIndex2 Numeric column coordinate of the second cell
2137-
* @param int $row2 Numeric row coordinate of the second cell
2138-
*
2139-
* @return $this
2140-
*/
2141-
public function addTableByColumnAndRow($columnIndex1, $row1, $columnIndex2, $row2): self
2142-
{
2143-
$cellRange = Coordinate::stringFromColumnIndex($columnIndex1) . $row1 . ':' . Coordinate::stringFromColumnIndex($columnIndex2) . $row2;
2144-
2145-
$table = new Table($cellRange);
2146-
$table->setWorksheet($this);
2147-
$this->addTable($table);
2148-
2149-
return $this;
2150-
}
2151-
21522131
/**
21532132
* Remove Table by name.
21542133
*

tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
44

5+
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
6+
use PhpOffice\PhpSpreadsheet\Cell\CellRange;
57
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
68
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
79
use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column;
@@ -133,25 +135,32 @@ public function testGetRange(): void
133135
self::assertEquals($expectedResult, $result);
134136
}
135137

136-
public function testSetRange(): void
138+
/**
139+
* @dataProvider validTableRangeProvider
140+
*
141+
* @param AddressRange|array<int>|string $fullRange
142+
* @param string $fullRange
143+
*/
144+
public function testSetRangeValidRange($fullRange, string $actualRange): void
137145
{
138-
$sheet = $this->getSheet();
139-
$title = $sheet->getTitle();
140146
$table = new Table(self::INITIAL_RANGE);
141-
$ranges = [
142-
'G1:J512' => "$title!G1:J512",
143-
'K1:N20' => 'K1:N20',
144-
];
145147

146-
foreach ($ranges as $actualRange => $fullRange) {
147-
// Setters return the instance to implement the fluent interface
148-
$result = $table->setRange($fullRange);
149-
self::assertInstanceOf(Table::class, $result);
148+
$result = $table->setRange($fullRange);
149+
self::assertInstanceOf(Table::class, $result);
150+
self::assertEquals($actualRange, $table->getRange());
151+
}
152+
153+
public function validTableRangeProvider(): array
154+
{
155+
$sheet = $this->getSheet();
156+
$title = $sheet->getTitle();
150157

151-
// Result should be the new table range
152-
$result = $table->getRange();
153-
self::assertEquals($actualRange, $result);
154-
}
158+
return [
159+
["$title!G1:J512", 'G1:J512'],
160+
['K1:N20', 'K1:N20'],
161+
[[3, 5, 6, 8], 'C5:F8'],
162+
[new CellRange(new CellAddress('C5', $sheet), new CellAddress('F8', $sheet)), 'C5:F8'],
163+
];
155164
}
156165

157166
public function testClearRange(): void

0 commit comments

Comments
 (0)