{"record":{"id":"2e2a0d39614aa023","repo":"iOfficeAI/OfficeCLI","slug":"corrupt-file-worksheet-data-missing","errorCode":null,"errorMessage":"Corrupt file: worksheet data missing","messagePattern":"Corrupt file: worksheet data missing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs","lineNumber":189,"sourceCode":"    /// Resolve sheet[N] index references in the first segment of a normalized path.\n    /// E.g. /sheet[1]/A1 → /Sheet1/A1 (if the first sheet is named \"Sheet1\").\n    /// Must be called after NormalizeExcelPath.\n    /// </summary>\n    private string ResolveSheetIndexInPath(string path)\n    {\n        if (!path.StartsWith('/')) return path;\n        var trimmed = path[1..]; // remove leading '/'\n        var slashIdx = trimmed.IndexOf('/');\n        var firstSegment = slashIdx >= 0 ? trimmed[..slashIdx] : trimmed;\n        var resolved = ResolveSheetName(firstSegment);\n        if (resolved == firstSegment) return path;\n        return slashIdx >= 0 ? $\"/{resolved}/{trimmed[(slashIdx + 1)..]}\" : $\"/{resolved}\";\n    }\n\n    // ==================== Private Helpers ====================\n\n    private static Worksheet GetSheet(WorksheetPart part) =>\n        part.Worksheet ?? throw new InvalidOperationException(\"Corrupt file: worksheet data missing\");\n\n    /// <summary>\n    /// Mark a worksheet as dirty. The actual save (with schema-order reorder) is\n    /// deferred to <see cref=\"FlushDirtyParts\"/> which runs in Dispose().\n    /// This replaces per-mutation Save() calls — batch operations over many cells\n    /// previously triggered one disk write per cell (O(n) saves); now they all\n    /// flush in a single pass at the end.\n    /// </summary>\n    private void SaveWorksheet(WorksheetPart part)\n    {\n        _dirtyWorksheets.Add(part);\n    }\n\n    /// <summary>\n    /// Flush all pending worksheet and stylesheet saves. Called from Dispose().\n    /// Each dirty WorksheetPart is reordered and saved exactly once regardless\n    /// of how many mutations targeted it.\n    /// </summary>","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs#L171-L207","documentation":"Thrown by GetSheet when a WorksheetPart's .Worksheet property is null. The part exists in the package but its underlying sheet XML is missing or unreadable — a corrupt OOXML package, not a caller bug. InvalidOperationException (not ArgumentException) signals file-level corruption.","triggerScenarios":"Opening any .xlsx where xl/worksheets/sheetN.xml is empty, malformed, or removed but the relationship still points at it. Reading any cell/range/table on that sheet triggers GetSheet.","commonSituations":"Files from a buggy generator that wrote the rels but not the sheet XML; zip-level corruption after a partial write; manual package edits that deleted a sheet part.","solutions":["Open the file in Excel and let it repair, then re-save.","Regenerate the file from the source of truth.","Inspect xl/worksheets/_rels/*.rels and the target sheetN.xml to find the broken relationship."],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"// Cannot meaningfully pre-validate; treat the document as untrusted at load time\nstatic bool TryGetWorksheet(WorksheetPart part, out Worksheet ws)\n{\n    ws = part?.Worksheet;\n    return ws != null;\n}","typeGuard":"null","tryCatchPattern":"try { /* any cell/range/table op */ }\ncatch (InvalidOperationException ex) when (ex.Message == \"Corrupt file: worksheet data missing\")\n{ // mark file as corrupt, do not retry on same file; offer repair/regenerate path }","preventionTips":["Validate generated files by reopening them before publishing.","Keep source data so a corrupt file can be regenerated.","Catch InvalidOperationException separately from ArgumentException — it signals corruption, not bad input."],"tags":["excel","corrupt-file","ooxml","worksheet"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}