{"record":{"id":"6c2c696bed379d4d","repo":"PHPOffice/PhpSpreadsheet","slug":"a-worksheet-has-already-been-assigned-drawings-ca","errorCode":null,"errorMessage":"A Worksheet has already been assigned. Drawings can only exist on one Worksheet.","messagePattern":"A Worksheet has already been assigned\\. Drawings can only exist on one Worksheet\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/BaseDrawing.php","lineNumber":240,"sourceCode":"                    $this->worksheet->getDrawingCollection(),\n                    $this->worksheet->getInCellDrawingCollection(),\n                ];\n\n                foreach ($collections as $collection) {\n                    foreach ($collection as $key => $drawing) {\n                        if ($drawing->getHashCode() === $this->getHashCode()) {\n                            $collection->offsetUnset($key);\n                            $this->worksheet = null;\n\n                            break 2; // break both loops\n                        }\n                    }\n                }\n\n                // Set new Worksheet\n                $this->setWorksheet($worksheet);\n            } else {\n                throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one Worksheet.');\n            }\n        }\n\n        return $this;\n    }\n\n    public function getCoordinates(): string\n    {\n        return $this->coordinates;\n    }\n\n    public function setCoordinates(string $coordinates): self\n    {\n        $this->coordinates = $coordinates;\n        if ($this->worksheet !== null) {\n            if (!($this instanceof Drawing && $this->getPath() === '')) {\n                $this->worksheet->getCell($this->coordinates);\n            }","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/BaseDrawing.php#L222-L258","documentation":"BaseDrawing::setWorksheet() throws when the drawing already belongs to a worksheet and the $overrideOld flag is false (the default). A drawing object can live on only one worksheet, so attaching the same instance to a second sheet is refused instead of duplicating it silently.","triggerScenarios":"$sheet2->addDrawing($drawing) where $drawing was already added to $sheet1; re-adding a drawing retrieved from $sheet1->getDrawingCollection() to another sheet.","commonSituations":"Copying an image/chart to multiple sheets using the same object; template code that moves drawings between sheets without cloning.","solutions":["Clone the drawing for each additional sheet: $copy = clone $drawing; $copy->setWorksheet($sheet2);","Or pass overrideOld = true to move (not copy) it: $drawing->setWorksheet($sheet2, true)","When loading a file and reassigning drawings, always instantiate or clone fresh objects per sheet"],"exampleFix":"// before\n$sheet1->addDrawing($drawing);\n$sheet2->addDrawing($drawing); // throws\n\n// after\n$sheet1->addDrawing($drawing);\n$copy = clone $drawing;\n$sheet2->addDrawing($copy);","handlingStrategy":"validation","validationCode":"function attachDrawing(Worksheet $target, BaseDrawing $drawing): void\n{\n    if ($drawing->getWorksheet() !== null && $drawing->getWorksheet() !== $target) {\n        $drawing = clone $drawing; // separate copy per sheet\n    }\n    $drawing->setWorksheet($target);\n}","typeGuard":"function isUnassignedDrawing(\\PhpOffice\\PhpSpreadsheet\\Worksheet\\BaseDrawing $d): bool\n{\n    return $d->getWorksheet() === null;\n}","tryCatchPattern":"try {\n    $drawing->setWorksheet($sheet2);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    // already owned by sheet1: clone for the second sheet (or pass true to move it)\n    $copy = clone $drawing;\n    $copy->setWorksheet($sheet2);\n}","preventionTips":["Never add the same drawing instance to two sheets; clone per sheet","Use setWorksheet($sheet, true) when you intend to move, not copy","Remember cloned drawings still reference the same image path (shared, not duplicated)"],"tags":["drawing","worksheet","image","ownership","phpspreadsheet"],"backgroundTag":"duplicate-object-assignment","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}