{"record":{"id":"9d3315d9b69a155a","repo":"PHPOffice/PhpSpreadsheet","slug":"cellstylexf-index-is-out-of-bounds","errorCode":null,"errorMessage":"CellStyleXf index is out of bounds.","messagePattern":"CellStyleXf index is out of bounds\\.","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Spreadsheet.php","lineNumber":1419,"sourceCode":"\n    /**\n     * Add a cellStyleXf to the workbook.\n     */\n    public function addCellStyleXf(Style $style): void\n    {\n        $this->cellStyleXfCollection[] = $style;\n        $style->setIndex(count($this->cellStyleXfCollection) - 1);\n    }\n\n    /**\n     * Remove cellStyleXf by index.\n     *\n     * @param int $cellStyleIndex Index to cellXf\n     */\n    public function removeCellStyleXfByIndex(int $cellStyleIndex): void\n    {\n        if ($cellStyleIndex > count($this->cellStyleXfCollection) - 1) {\n            throw new Exception('CellStyleXf index is out of bounds.');\n        }\n        array_splice($this->cellStyleXfCollection, $cellStyleIndex, 1);\n    }\n\n    /**\n     * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells\n     * and columns in the workbook.\n     */\n    public function garbageCollect(): void\n    {\n        // how many references are there to each cellXf ?\n        $countReferencesCellXf = [];\n        foreach ($this->cellXfCollection as $index => $cellXf) {\n            $countReferencesCellXf[$index] = 0;\n        }\n\n        foreach ($this->getWorksheetIterator() as $sheet) {\n            // from cells","sourceCodeStart":1401,"sourceCodeEnd":1437,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Spreadsheet.php#L1401-L1437","documentation":"Spreadsheet::removeCellStyleXfByIndex(int) deletes one entry from the cellStyleXf collection (the named 'cell styles' like the styles dropdown in Excel, distinct from cellXfs). It throws 'CellStyleXf index is out of bounds.' when the index exceeds count-1. This is a low-level maintenance API on a collection most user code never touches.","triggerScenarios":"removeCellStyleXfByIndex($i) with $i >= count($spreadsheet->getCellStyleXfCollection()); off-by-one loops over the styleXf collection; indexes copied from another workbook's collection.","commonSituations":"Style-cleanup tooling written against the wrong collection (cellXf vs cellStyleXf) assuming equal sizes; scripts pruning template styles after loading themed files; debugging code iterating collections with <= instead of <.","solutions":["Validate against the correct collection: if ($i >= 0 && $i < count($spreadsheet->getCellStyleXfCollection())) ...","Prefer $spreadsheet->garbageCollect() for pruning unused styles.","Iterate downward when removing multiple entries.","Confirm you actually mean cellStyleXf (named styles) and not cellXf (per-cell formatting)."],"exampleFix":"// before\n$spreadsheet->removeCellStyleXfByIndex($i); // $i beyond collection end\n\n// after\n$max = count($spreadsheet->getCellStyleXfCollection()) - 1;\nif ($i >= 0 && $i <= $max) {\n    $spreadsheet->removeCellStyleXfByIndex($i);\n}","handlingStrategy":"validation","validationCode":"if ($cellStyleIndex >= 0 && $cellStyleIndex < count($spreadsheet->getCellStyleXfCollection())) {\n    $spreadsheet->removeCellStyleXfByIndex($cellStyleIndex);\n}","typeGuard":"function isValidCellStyleXfIndex(\\PhpOffice\\PhpSpreadsheet\\Spreadsheet $s, int $i): bool\n{\n    return $i >= 0 && $i < count($s->getCellStyleXfCollection());\n}","tryCatchPattern":"try {\n    $spreadsheet->removeCellStyleXfByIndex($i);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    error_log('cellStyleXf removal skipped: ' . $e->getMessage());\n}","preventionTips":["Confirm you are indexing cellStyleXf (named styles), not cellXf.","Use garbageCollect() for pruning unused styles.","Validate against the same collection the removal targets."],"tags":["styles","index-out-of-bounds","cellstylexf","phpspreadsheet"],"backgroundTag":"index-out-of-bounds","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}