{"record":{"id":"361b08683c056075","repo":"iOfficeAI/OfficeCLI","slug":"workbook-not-found-361b08","errorCode":null,"errorMessage":"Workbook not found","messagePattern":"Workbook not found","errorType":"exception","errorClass":"System.InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Cf.cs","lineNumber":472,"sourceCode":"        // Build DifferentialFormat (dxf) for the formatting.\n        // A dxf Font may carry: b, i, u, strike, sz, rFont, color.\n        // All sub-props are threaded together so users can combine\n        // (e.g. bold + italic + underline + custom size + name).\n        var dxf = new DifferentialFormat();\n        var dxfFont = BuildFormulaCfFont(properties);\n        if (dxfFont != null) dxf.Append(dxfFont);\n\n        if (properties.TryGetValue(\"fill\", out var fillColor))\n        {\n            var normalizedFillColor = ParseHelpers.NormalizeArgbColor(fillColor);\n            dxf.Append(new Fill(new PatternFill(\n                new BackgroundColor { Rgb = normalizedFillColor })\n            { PatternType = PatternValues.Solid }));\n        }\n\n        // Add dxf to stylesheet (ensure it exists)\n        var fcfWbPart = _doc.WorkbookPart\n            ?? throw new InvalidOperationException(\"Workbook not found\");\n        var fcfStyleMgr = new ExcelStyleManager(fcfWbPart);\n        fcfStyleMgr.EnsureStylesPart();\n        var stylesheet = fcfWbPart.WorkbookStylesPart!.Stylesheet!;\n\n        var dxfs = stylesheet.GetFirstChild<DifferentialFormats>();\n        if (dxfs == null)\n        {\n            dxfs = new DifferentialFormats { Count = 0 };\n            stylesheet.Append(dxfs);\n        }\n        dxfs.Append(dxf);\n        dxfs.Count = (uint)dxfs.Elements<DifferentialFormat>().Count();\n        _dirtyStylesheet = true;\n\n        var dxfId = dxfs.Count!.Value - 1;\n\n        var fcfRule = new ConditionalFormattingRule\n        {","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Cf.cs#L454-L490","documentation":"Thrown by AddFormulaCf when _doc.WorkbookPart is null while trying to attach the DifferentialFormat (dxf) to the stylesheet. WorkbookPart being null indicates the SpreadsheetDocument is in a state where no workbook part is loaded — typically a corrupt, empty, or incorrectly-opened package. This is an InvalidOperationException (internal-state failure), not an ArgumentException (user input).","triggerScenarios":"Calling Add formula CF on a SpreadsheetDocument whose WorkbookPart is null. This happens when the document was opened from a non-xlsx stream, a template missing the workbook part, or after a prior mutation corrupted the package in memory.","commonSituations":"Opening a .xltm/.xltx template whose root part differs; loading a partial/corrupt file; a programmatic caller constructing ExcelHandler around a freshly-created but uninitialized package.","solutions":["Open a valid .xlsx file that contains a workbook part; verify integrity before calling Add.","If the document was created programmatically, ensure the WorkbookPart was added (e.g. SpreadsheetDocument.Create with WorkbookPart) before invoking the handler.","Re-open the source file from a known-good copy."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (_doc.WorkbookPart is null)\n    throw new InvalidOperationException(\"Cannot add CF: document has no WorkbookPart. Open a valid .xlsx.\");","typeGuard":"static bool HasWorkbookPart(SpreadsheetDocument doc) => doc.WorkbookPart is not null;","tryCatchPattern":"try { return Add(path, \"formulacf\", pos, props); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Workbook not found\")\n{ /* reload document from known-good source, then retry once */ throw; }","preventionTips":["Validate the source .xlsx opens with a non-null WorkbookPart before any Add.","Do not wrap non-spreadsheet streams in the Excel handler.","For programmatically-created packages, add the WorkbookPart before handler use."],"tags":["excel","conditional-formatting","formula-cf","document-state","invalid-operation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}