Remove table By name
Option to remove the table from table collection of worksheet
This commit is contained in:
parent
3c3d949a5d
commit
50b91e8ede
|
|
@ -2057,6 +2057,24 @@ class Worksheet implements IComparable
|
||||||
return $this->addTable(new Table($cellRange, $this));
|
return $this->addTable(new Table($cellRange, $this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove Table by name.
|
||||||
|
*
|
||||||
|
* @param string $name Table name
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function removeTableByName(string $name): self
|
||||||
|
{
|
||||||
|
foreach($this->tableCollection as $key => $table) {
|
||||||
|
if ($table->getName() === $name) {
|
||||||
|
unset($this->tableCollection[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove collection of Tables.
|
* Remove collection of Tables.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet\Table;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
|
||||||
|
|
||||||
|
class RemoveTableTest extends SetupTeardown
|
||||||
|
{
|
||||||
|
private const INITIAL_RANGE = 'H2:O256';
|
||||||
|
|
||||||
|
public function testRemoveTable(): void
|
||||||
|
{
|
||||||
|
$sheet = $this->getSheet();
|
||||||
|
|
||||||
|
$table = new Table(self::INITIAL_RANGE, $sheet);
|
||||||
|
$table->setName('Table1');
|
||||||
|
$sheet->addTable($table);
|
||||||
|
|
||||||
|
self::assertEquals(1, $sheet->getTableCollection()->count());
|
||||||
|
|
||||||
|
$sheet->removeTableByName('Table1');
|
||||||
|
self::assertEquals(0, $sheet->getTableCollection()->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRemoveCollection(): void
|
||||||
|
{
|
||||||
|
$sheet = $this->getSheet();
|
||||||
|
|
||||||
|
$table = new Table(self::INITIAL_RANGE, $sheet);
|
||||||
|
$table->setName('Table1');
|
||||||
|
$sheet->addTable($table);
|
||||||
|
|
||||||
|
self::assertEquals(1, $sheet->getTableCollection()->count());
|
||||||
|
|
||||||
|
$sheet->removeTableCollection();
|
||||||
|
self::assertEquals(0, $sheet->getTableCollection()->count());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue