Skip to content

Commit b3109d6

Browse files
authored
Merge pull request #3257 from PHPOffice/Table_Get-Table-by-Name
Additional methods for working with Tables by name
2 parents 4fc43c9 + 70e668a commit b3109d6

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,51 @@ public function addTable(Table $table): self
22212221
return $this;
22222222
}
22232223

2224+
/**
2225+
* @return string[] array of Table names
2226+
*/
2227+
public function getTableNames(): array
2228+
{
2229+
$tableNames = [];
2230+
2231+
foreach ($this->tableCollection as $table) {
2232+
/** @var Table $table */
2233+
$tableNames[] = $table->getName();
2234+
}
2235+
2236+
return $tableNames;
2237+
}
2238+
2239+
/**
2240+
* @param string $name the table name to search
2241+
*
2242+
* @return null|Table The table from the tables collection, or null if not found
2243+
*/
2244+
public function getTableByName(string $name): ?Table
2245+
{
2246+
$tableIndex = $this->getTableIndexByName($name);
2247+
2248+
return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex];
2249+
}
2250+
2251+
/**
2252+
* @param string $name the table name to search
2253+
*
2254+
* @return null|int The index of the located table in the tables collection, or null if not found
2255+
*/
2256+
protected function getTableIndexByName(string $name): ?int
2257+
{
2258+
$name = Shared\StringHelper::strToUpper($name);
2259+
foreach ($this->tableCollection as $index => $table) {
2260+
/** @var Table $table */
2261+
if (Shared\StringHelper::strToUpper($table->getName()) === $name) {
2262+
return $index;
2263+
}
2264+
}
2265+
2266+
return null;
2267+
}
2268+
22242269
/**
22252270
* Remove Table by name.
22262271
*
@@ -2230,11 +2275,10 @@ public function addTable(Table $table): self
22302275
*/
22312276
public function removeTableByName(string $name): self
22322277
{
2233-
$name = Shared\StringHelper::strToUpper($name);
2234-
foreach ($this->tableCollection as $key => $table) {
2235-
if (Shared\StringHelper::strToUpper($table->getName()) === $name) {
2236-
unset($this->tableCollection[$key]);
2237-
}
2278+
$tableIndex = $this->getTableIndexByName($name);
2279+
2280+
if ($tableIndex !== null) {
2281+
unset($this->tableCollection[$tableIndex]);
22382282
}
22392283

22402284
return $this;

tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Exception;
66
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
78
use PhpOffice\PhpSpreadsheet\Spreadsheet;
89
use PhpOffice\PhpSpreadsheet\Worksheet\CellIterator;
10+
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
911
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
1012
use PHPUnit\Framework\TestCase;
1113

@@ -506,4 +508,27 @@ public function emptyColumnProvider(): array
506508
['I', true],
507509
];
508510
}
511+
512+
public function testGetTableNames(): void
513+
{
514+
$reader = new Xlsx();
515+
$spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx');
516+
$worksheet = $spreadsheet->getActiveSheet();
517+
518+
$tables = $worksheet->getTableNames();
519+
self::assertSame(['DeptSales'], $tables);
520+
}
521+
522+
public function testGetTableByName(): void
523+
{
524+
$reader = new Xlsx();
525+
$spreadsheet = $reader->load('tests/data/Worksheet/Table/TableFormulae.xlsx');
526+
$worksheet = $spreadsheet->getActiveSheet();
527+
528+
$table = $worksheet->getTableByName('Non-existent Table');
529+
self::assertNull($table);
530+
531+
$table = $worksheet->getTableByName('DeptSales');
532+
self::assertInstanceOf(Table::class, $table);
533+
}
509534
}

0 commit comments

Comments
 (0)