{"record":{"id":"dba75428027ba47b","repo":"iOfficeAI/OfficeCLI","slug":"workbook-has-no-sheets-element","errorCode":null,"errorMessage":"Workbook has no sheets element","messagePattern":"Workbook has no sheets element","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":184,"sourceCode":"    }\n\n    public string Move(string sourcePath, string? targetParentPath, InsertPosition? position, Dictionary<string, string>? properties = null)\n    {\n        // xlsx has no track-change concept; `properties` is accepted for IDocumentHandler parity but ignored.\n        var index = position?.Index;\n        var segments = sourcePath.TrimStart('/').Split('/', 2);\n        var sheetName = segments[0];\n        var worksheet = FindWorksheet(sheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {sheetName}\");\n\n        if (segments.Length < 2)\n        {\n            // Move (reorder) the sheet within the workbook.\n            // CONSISTENCY(move-anchor): mirrors PowerPointHandler.Move slide reorder —\n            // supports --index / --after /Sheet2 / --before /Sheet3.\n            var workbook = GetWorkbook();\n            var sheets = workbook.GetFirstChild<Sheets>()\n                ?? throw new InvalidOperationException(\"Workbook has no sheets element\");\n            var sheetEl = sheets.Elements<Sheet>().FirstOrDefault(s =>\n                string.Equals(s.Name?.Value, sheetName, StringComparison.OrdinalIgnoreCase))\n                ?? throw new ArgumentException($\"Sheet not found: {sheetName}\");\n\n            // Resolve after/before anchor BEFORE removing sheetEl.\n            static string ExtractAnchorSheetName(string raw) =>\n                (raw.StartsWith(\"/\") ? raw[1..] : raw).Split('/', 2)[0];\n\n            Sheet? afterAnchor = null, beforeAnchor = null;\n            if (position?.After != null)\n            {\n                var anchorName = ExtractAnchorSheetName(position.After);\n                afterAnchor = sheets.Elements<Sheet>().FirstOrDefault(s =>\n                    string.Equals(s.Name?.Value, anchorName, StringComparison.OrdinalIgnoreCase))\n                    ?? throw new ArgumentException($\"After anchor not found: {position.After}\");\n            }\n            else if (position?.Before != null)\n            {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L166-L202","documentation":"Thrown during a whole-sheet reorder (sourcePath had no second segment) when workbook.GetFirstChild<Sheets>() is null. Every valid XLSX has a <sheets> catalog in xl/workbook.xml; its absence means the package is structurally corrupt or truncated, not that the user passed a bad argument. The move cannot proceed because the sheet catalog it must reorder does not exist.","triggerScenarios":"Operating on a hand-edited or partially-written .xlsx whose workbook.xml lost its <sheets> element; a file truncated during save/copy; a package produced by a non-conforming exporter that omits the sheets catalog.","commonSituations":"File copied while Excel still had it open/locked; generated by a broken export pipeline; corrupted download; a minimal package assembled without the sheets element.","solutions":["Open and re-save the file in Excel/LibreOffice to regenerate workbook.xml, then retry.","Validate the package with the Open XML SDK validator or an OOXML checker before handing it to the handler.","If you produce the file programmatically, ensure Workbook.Sheets is created and at least one Sheet is registered."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Structural defect: cannot cheaply pre-validate from the public API.\n// Trust the throw; optionally validate the package out-of-band before opening.","typeGuard":null,"tryCatchPattern":"try { handler.Move(\"/Sheet1\", null, InsertPosition.AtIndex(0)); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Workbook has no sheets element\")\n{ /* file is corrupt: ask the user to re-save/repair the workbook */ }","preventionTips":["Validate OOXML packages with the SDK validator before processing.","Re-save generated files in Excel/LibreOffice to normalize workbook.xml.","Never copy an .xlsx while Excel holds it open/locked."],"tags":["excel","openxml","move","corrupt-file","workbook"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}