{"record":{"id":"7bd491559edc9727","repo":"iOfficeAI/OfficeCLI","slug":"workbook-is-missing","errorCode":null,"errorMessage":"Workbook is missing","messagePattern":"Workbook is missing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Cache.cs","lineNumber":1057,"sourceCode":"\n        // Clone the records part (if present) under the new cache part.\n        var origRecords = original.GetPartsOfType<PivotTableCacheRecordsPart>().FirstOrDefault();\n        if (origRecords != null)\n        {\n            var cloneRecords = clone.AddNewPart<PivotTableCacheRecordsPart>();\n            using (var src = origRecords.GetStream(FileMode.Open, FileAccess.Read))\n            using (var dst = cloneRecords.GetStream(FileMode.Create, FileAccess.Write))\n            {\n                src.CopyTo(dst);\n            }\n            // Re-point the cacheDef's r:id to the new records part.\n            if (clone.PivotCacheDefinition != null)\n                clone.PivotCacheDefinition.Id = clone.GetIdOfPart(cloneRecords);\n        }\n\n        // Register a new <pivotCache> entry in workbook.xml with a fresh cacheId.\n        var wb = workbookPart.Workbook\n            ?? throw new InvalidOperationException(\"Workbook is missing\");\n        var pivotCaches = wb.GetFirstChild<PivotCaches>();\n        if (pivotCaches == null)\n        {\n            pivotCaches = new PivotCaches();\n            var insertBefore = wb.GetFirstChild<WebPublishing>()\n                ?? wb.GetFirstChild<FileRecoveryProperties>()\n                ?? (OpenXmlElement?)wb.GetFirstChild<WebPublishObjects>();\n            if (insertBefore != null)\n                wb.InsertBefore(pivotCaches, insertBefore);\n            else\n                wb.AppendChild(pivotCaches);\n        }\n        uint newCacheId = pivotCaches.Elements<PivotCache>()\n            .Select(pc => pc.CacheId?.Value ?? 0u).DefaultIfEmpty(0u).Max() + 1;\n        pivotCaches.AppendChild(new PivotCache\n        {\n            CacheId = newCacheId,\n            Id = workbookPart.GetIdOfPart(clone)","sourceCodeStart":1039,"sourceCodeEnd":1075,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Cache.cs#L1039-L1075","documentation":"InvalidOperationException thrown when registering a new pivotCache in workbook.xml but workbookPart.Workbook is null. This is an internal-state/precondition failure: the cache-registration step needs the <workbook> root to append a <pivotCaches> entry, and the document is missing or corrupted at that level.","triggerScenarios":"Cloning/registering a pivot cache (e.g. on pivot create/copy) against a SpreadsheetDocument whose WorkbookPart.Workbook is null — a package missing its workbook.xml root or with a broken package structure.","commonSituations":"Operating on a corrupted or incompletely-generated workbook; a file whose workbook.xml was stripped; a malformed package assembled by another tool.","solutions":["Open and repair the workbook in Excel, then retry the pivot operation.","Verify the package is a valid xlsx with an intact workbook.xml before operating.","Re-create the workbook from a known-good source if the structure is unrecoverable."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"var wb = workbookPart.Workbook;\nif (wb == null) { /* report: workbook.xml root missing; repair or rebuild the package */ }","typeGuard":"static bool HasWorkbook(DocumentFormat.OpenXml.Packaging.WorkbookPart p) => p?.Workbook != null;","tryCatchPattern":null,"preventionTips":["Validate WorkbookPart.Workbook is non-null before pivot cache registration.","Repair malformed xlsx packages in Excel before operating.","Re-create the workbook from a known-good source if the root is missing."],"tags":["pivot-table","excel","ooxml","invalid-operation","precondition","officecli"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}