Skip to content

Commit fac448f

Browse files
committed
Additional methods for working with Tables by name
1 parent 4fc43c9 commit fac448f

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,41 @@ 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+
public function getTableByName(string $name): ?Table
2240+
{
2241+
$tableIndex = $this->getTableIndexByName($name);
2242+
2243+
return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex];
2244+
}
2245+
2246+
protected function getTableIndexByName(string $name): ?int
2247+
{
2248+
$name = Shared\StringHelper::strToUpper($name);
2249+
foreach ($this->tableCollection as $index => $table) {
2250+
/** @var Table $table */
2251+
if (Shared\StringHelper::strToUpper($table->getName()) === $name) {
2252+
return $index;
2253+
}
2254+
}
2255+
2256+
return null;
2257+
}
2258+
22242259
/**
22252260
* Remove Table by name.
22262261
*
@@ -2230,11 +2265,10 @@ public function addTable(Table $table): self
22302265
*/
22312266
public function removeTableByName(string $name): self
22322267
{
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-
}
2268+
$tableIndex = $this->getTableIndexByName($name);
2269+
2270+
if ($tableIndex !== null) {
2271+
unset($this->tableCollection[$tableIndex]);
22382272
}
22392273

22402274
return $this;

0 commit comments

Comments
 (0)