Remove table By name

Option to remove the table from table collection of
worksheet
This commit is contained in:
aswinkumar863 2022-04-03 18:24:38 +05:30
parent 3c3d949a5d
commit 50b91e8ede
No known key found for this signature in database
GPG Key ID: 8A69BFA6AEF8FA3E
2 changed files with 56 additions and 0 deletions

View File

@ -2057,6 +2057,24 @@ class Worksheet implements IComparable
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.
*/

View File

@ -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());
}
}