From 534cbc04c040079c26c8855670ba8ed94566c176 Mon Sep 17 00:00:00 2001 From: aswinkumar863 Date: Sat, 23 Apr 2022 18:43:38 +0530 Subject: [PATCH] Accept table range as AddressRange and array Table constructor now accepts AddressRange and array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] --- src/PhpSpreadsheet/Worksheet/Table.php | 21 +++++++--- src/PhpSpreadsheet/Worksheet/Worksheet.php | 21 ---------- .../Worksheet/Table/TableTest.php | 39 ++++++++++++------- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Table.php b/src/PhpSpreadsheet/Worksheet/Table.php index 4ec4cbfc..66839d41 100644 --- a/src/PhpSpreadsheet/Worksheet/Table.php +++ b/src/PhpSpreadsheet/Worksheet/Table.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Cell\AddressRange; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; @@ -61,12 +62,13 @@ class Table /** * Create a new Table. * - * @param string $range (e.g. A1:D4) + * @param AddressRange|array|string $range + * A simple string containing a Cell range like 'A1:E10' is permitted + * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), + * or an AddressRange object. * @param string $name (e.g. Table1) - * - * @return $this */ - public function __construct(string $range = '', string $name = '') + public function __construct($range = '', string $name = '') { $this->setRange($range); $this->setName($name); @@ -161,11 +163,18 @@ class Table /** * Set Table Cell Range. + * + * @param AddressRange|array|string $range + * A simple string containing a Cell range like 'A1:E10' is permitted + * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), + * or an AddressRange object. */ - public function setRange(string $range): self + public function setRange($range = ''): self { // extract coordinate - [$worksheet, $range] = Worksheet::extractSheetTitle($range, true); + if ($range !== '') { + [, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true); + } if (empty($range)) { // Discard all column rules $this->columns = []; diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 66315083..0baf6ecd 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2128,27 +2128,6 @@ class Worksheet implements IComparable return $this; } - /** - * Add Table Range by using numeric cell coordinates. - * - * @param int $columnIndex1 Numeric column coordinate of the first cell - * @param int $row1 Numeric row coordinate of the first cell - * @param int $columnIndex2 Numeric column coordinate of the second cell - * @param int $row2 Numeric row coordinate of the second cell - * - * @return $this - */ - public function addTableByColumnAndRow($columnIndex1, $row1, $columnIndex2, $row2): self - { - $cellRange = Coordinate::stringFromColumnIndex($columnIndex1) . $row1 . ':' . Coordinate::stringFromColumnIndex($columnIndex2) . $row2; - - $table = new Table($cellRange); - $table->setWorksheet($this); - $this->addTable($table); - - return $this; - } - /** * Remove Table by name. * diff --git a/tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php b/tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php index dd49aae1..c7cbee25 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/Table/TableTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; +use PhpOffice\PhpSpreadsheet\Cell\CellAddress; +use PhpOffice\PhpSpreadsheet\Cell\CellRange; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column; @@ -133,25 +135,32 @@ class TableTest extends SetupTeardown self::assertEquals($expectedResult, $result); } - public function testSetRange(): void + /** + * @dataProvider validTableRangeProvider + * + * @param AddressRange|array|string $fullRange + * @param string $fullRange + */ + public function testSetRangeValidRange($fullRange, string $actualRange): void + { + $table = new Table(self::INITIAL_RANGE); + + $result = $table->setRange($fullRange); + self::assertInstanceOf(Table::class, $result); + self::assertEquals($actualRange, $table->getRange()); + } + + public function validTableRangeProvider(): array { $sheet = $this->getSheet(); $title = $sheet->getTitle(); - $table = new Table(self::INITIAL_RANGE); - $ranges = [ - 'G1:J512' => "$title!G1:J512", - 'K1:N20' => 'K1:N20', + + return [ + ["$title!G1:J512", 'G1:J512'], + ['K1:N20', 'K1:N20'], + [[3, 5, 6, 8], 'C5:F8'], + [new CellRange(new CellAddress('C5', $sheet), new CellAddress('F8', $sheet)), 'C5:F8'], ]; - - foreach ($ranges as $actualRange => $fullRange) { - // Setters return the instance to implement the fluent interface - $result = $table->setRange($fullRange); - self::assertInstanceOf(Table::class, $result); - - // Result should be the new table range - $result = $table->getRange(); - self::assertEquals($actualRange, $result); - } } public function testClearRange(): void