{"record":{"id":"6f075280c0b24429","repo":"iOfficeAI/OfficeCLI","slug":"workbook-is-missing-6f0752","errorCode":null,"errorMessage":"Workbook is missing","messagePattern":"Workbook is missing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.cs","lineNumber":1067,"sourceCode":"        // the full source on every Refresh.\n        var labelFilterSpec = ParseLabelFilterSpec(headers, properties);\n\n        // 2b. Apply Top-N filter to the source rows (ranked by the first value\n        // field's aggregate on the outermost row field). Runs BEFORE cache\n        // build so the cache, rendered cells, and grand totals all reflect\n        // the filtered subset. See ApplyTopNFilter for semantics & limits.\n        if ((properties.TryGetValue(\"topN\", out var topNStr)\n             || properties.TryGetValue(\"topn\", out topNStr))\n            && int.TryParse(topNStr, System.Globalization.NumberStyles.Integer,\n                    System.Globalization.CultureInfo.InvariantCulture, out var topN))\n        {\n            ApplyTopNFilter(columnData, rowFields, valueFields, topN);\n        }\n\n        // 3. Generate unique cache ID\n        uint cacheId = 0;\n        var workbook = workbookPart.Workbook\n            ?? throw new InvalidOperationException(\"Workbook is missing\");\n        var pivotCaches = workbook.GetFirstChild<PivotCaches>();\n        if (pivotCaches != null)\n            cacheId = pivotCaches.Elements<PivotCache>().Select(pc => pc.CacheId?.Value ?? 0u).DefaultIfEmpty(0u).Max() + 1;\n\n        // Design change (cache sharing): if any existing pivot already binds\n        // to an equivalent (sheet, range) source, reuse its\n        // PivotTableCacheDefinitionPart instead of creating a new one. This\n        // matches Excel's \"one cache per source\" contract — refresh\n        // propagates across siblings, file size doesn't blow up. See\n        // CountCacheReferrers / FindMatchingCachePart in\n        // PivotTableHelper.Cache.cs for the supporting helpers.\n        // Date-grouped pivots add derived cacheFields (year/quarter/month/...)\n        // with <fieldGroup> XML. Calculated-field pivots append synthetic\n        // cacheFields with formula= attributes. Both mutate the cache schema\n        // in ways the sibling pivots don't expect — pivotFields.count on\n        // siblings stays at the original column count while cacheFields.count\n        // grows, and Excel rejects the workbook on that mismatch.\n        // Force a fresh cache when this pivot has either, so its schema","sourceCodeStart":1049,"sourceCodeEnd":1085,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.cs#L1049-L1085","documentation":"Thrown by CreatePivotTable when workbookPart.Workbook is null — the package's root Workbook element is missing. This is a structural integrity failure: a valid OOXML package always has a <workbook> element as the root of workbook.xml. Without it, no pivot table can be registered because the cache-id allocation and <pivotCaches> collection both live under Workbook.","triggerScenarios":"Creating a pivot in a workbook whose workbook.xml is missing or its root element is not deserializing to a Workbook instance. Typically a corrupt or incomplete package, or one produced by a tool that did not write the workbook part.","commonSituations":"File truncated or partially written; package assembled incorrectly (missing workbook.xml); a template file that was emptied or corrupted; opening a non-xlsx file that happens to have a .xlsx extension.","solutions":["Open and re-save the workbook in Excel or LibreOffice to rebuild the package structure.","If creating the workbook programmatically, ensure the Workbook part is properly initialized before adding pivots.","Verify the file is a valid xlsx (not a renamed xls, csv, or zip).","Start from a known-good workbook template."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { CreatePivotTable(workbookPart, sourceSheet, sourceRef, props); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Workbook is missing\")\n{\n    // The package is structurally corrupt — workbook.xml is absent.\n    // Re-save in Excel/LibreOffice, or start from a valid template.\n    throw new InvalidOperationException($\"'{path}' has no Workbook element. Re-save or repair the file.\", ex);\n}","preventionTips":["Use known-good workbook templates as starting points.","Verify the file is a valid xlsx (check the zip structure and workbook.xml presence).","Do not process files from untrusted sources without validation.","If generating packages programmatically, ensure the Workbook part is created before adding pivots."],"tags":["pivot","excel","ooxml","corruption","workbook","package-integrity","create"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}