Accept table range as AddressRange and array

Table constructor now accepts AddressRange and array
of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow]
This commit is contained in:
aswinkumar863 2022-04-23 18:43:38 +05:30
parent d414f139f1
commit 534cbc04c0
No known key found for this signature in database
GPG Key ID: 8A69BFA6AEF8FA3E
3 changed files with 39 additions and 42 deletions

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Worksheet; namespace PhpOffice\PhpSpreadsheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
@ -61,12 +62,13 @@ class Table
/** /**
* Create a new Table. * Create a new Table.
* *
* @param string $range (e.g. A1:D4) * @param AddressRange|array<int>|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) * @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->setRange($range);
$this->setName($name); $this->setName($name);
@ -161,11 +163,18 @@ class Table
/** /**
* Set Table Cell Range. * Set Table Cell Range.
*
* @param AddressRange|array<int>|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 // extract coordinate
[$worksheet, $range] = Worksheet::extractSheetTitle($range, true); if ($range !== '') {
[, $range] = Worksheet::extractSheetTitle(Validations::validateCellRange($range), true);
}
if (empty($range)) { if (empty($range)) {
// Discard all column rules // Discard all column rules
$this->columns = []; $this->columns = [];

View File

@ -2128,27 +2128,6 @@ class Worksheet implements IComparable
return $this; 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. * Remove Table by name.
* *

View File

@ -2,6 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table; namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
use PhpOffice\PhpSpreadsheet\Cell\CellRange;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Worksheet\Table; use PhpOffice\PhpSpreadsheet\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column; use PhpOffice\PhpSpreadsheet\Worksheet\Table\Column;
@ -133,25 +135,32 @@ class TableTest extends SetupTeardown
self::assertEquals($expectedResult, $result); self::assertEquals($expectedResult, $result);
} }
public function testSetRange(): void /**
* @dataProvider validTableRangeProvider
*
* @param AddressRange|array<int>|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(); $sheet = $this->getSheet();
$title = $sheet->getTitle(); $title = $sheet->getTitle();
$table = new Table(self::INITIAL_RANGE);
$ranges = [ return [
'G1:J512' => "$title!G1:J512", ["$title!G1:J512", 'G1:J512'],
'K1:N20' => 'K1:N20', ['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 public function testClearRange(): void