{"record":{"id":"5b945db49b585f64","repo":"PHPOffice/PhpSpreadsheet","slug":"workbook-already-contains-a-table-named-this-n","errorCode":null,"errorMessage":"Workbook already contains a table named '{$this->name}'","messagePattern":"Workbook already contains a table named '(.+?)'","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/Table.php","lineNumber":350,"sourceCode":"     */\n    public function getWorksheet(): ?Worksheet\n    {\n        return $this->workSheet;\n    }\n\n    /**\n     * Set Table's Worksheet.\n     */\n    public function setWorksheet(?Worksheet $worksheet = null): self\n    {\n        if ($this->name !== '' && $worksheet !== null) {\n            $spreadsheet = $worksheet->getParentOrThrow();\n            $tableName = StringHelper::strToUpper($this->name);\n\n            foreach ($spreadsheet->getWorksheetIterator() as $sheet) {\n                foreach ($sheet->getTableCollection() as $table) {\n                    if (StringHelper::strToUpper($table->getName()) === $tableName) {\n                        throw new PhpSpreadsheetException(\"Workbook already contains a table named '{$this->name}'\");\n                    }\n                }\n            }\n        }\n\n        $this->workSheet = $worksheet;\n        $this->autoFilter->setParent($worksheet);\n\n        return $this;\n    }\n\n    /**\n     * Get all Table Columns.\n     *\n     * @return Table\\Column[]\n     */\n    public function getColumns(): array\n    {","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/Table.php#L332-L368","documentation":"Thrown by Table::setWorksheet() (reached via Worksheet::addTable()) when the table's name, compared case-insensitively with StringHelper::strToUpper, already exists in the table collection of any worksheet in the parent workbook. Excel requires table names to be unique across the whole workbook, so PhpSpreadsheet enforces workbook-wide uniqueness at the moment the table is attached. The check only runs when the name is non-empty and the target worksheet is non-null.","triggerScenarios":"Calling $worksheet->addTable($table) or $table->setWorksheet($sheet) while any sheet in $worksheet->getParentOrThrow() already has a table with the same name; re-adding a cloned table to another sheet without renaming it; matching names with different case such as 'Sales' vs 'SALES'.","commonSituations":"Creating tables in a loop with a hardcoded name like 'Table1'; loading an existing xlsx that already contains a table and appending another with the same name; copying worksheets that carry tables. Often appears right after a refactor that moved addTable() inside a loop.","solutions":["Give each table a unique generated name before attaching, e.g. $table->setName(sprintf('Table%d', ++$i))","Scan the workbook first (getWorksheetIterator + getTableCollection) and rename or remove the colliding table","Never call addTable()/setWorksheet() twice for the same name; reuse the existing Table object or unset it from the source sheet first"],"exampleFix":"// before\nforeach ($dataSheets as $i => $sheet) {\n    $table = new Table('A1:E10');\n    $table->setName('SalesTable');\n    $sheet->addTable($table); // throws on 2nd iteration\n}\n\n// after\nforeach ($dataSheets as $i => $sheet) {\n    $table = new Table('A1:E10');\n    $table->setName('SalesTable' . ($i + 1));\n    $sheet->addTable($table);\n}","handlingStrategy":"validation","validationCode":"function tableExists(Spreadsheet $spreadsheet, string $name): bool\n{\n    $name = StringHelper::strToUpper($name); // or mb_strtoupper($name)\n    foreach ($spreadsheet->getWorksheetIterator() as $sheet) {\n        foreach ($sheet->getTableCollection() as $table) {\n            if (mb_strtoupper($table->getName()) === $name) {\n                return true;\n            }\n        }\n    }\n\n    return false;\n}\n\nif (!tableExists($spreadsheet, $newName)) {\n    $sheet->addTable($table);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $sheet->addTable($table);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    if (str_contains($e->getMessage(), 'Workbook already contains a table')) {\n        $table->setName($table->getName() . '_' . uniqid());\n        $sheet->addTable($table);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Generate table names with a counter or uuid suffix instead of constants","Centralize table creation in one helper that guarantees name uniqueness","When copying sheets, rename cloned tables before attaching them"],"tags":["phpspreadsheet","table","duplicate-name","workbook","xlsx"],"backgroundTag":"duplicate-resource-name","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}