{"record":{"id":"2385503dabc0d36d","repo":"iOfficeAI/OfficeCLI","slug":"pivot-cache-definition-is-missing","errorCode":null,"errorMessage":"Pivot cache definition is missing","messagePattern":"Pivot cache definition is missing","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Readback.cs","lineNumber":382,"sourceCode":"    /// resizes pivotDef.PivotFields to match the new column count. Existing\n    /// PivotField Axis/DataField assignments are reset because indices may no\n    /// longer line up — RebuildFieldAreas reapplies them after this returns.\n    /// </summary>\n    private static void RefreshPivotCacheFromSource(PivotTablePart pivotPart, string newSourceSpec,\n        Dictionary<string, string>? pendingFieldAreaProps = null)\n    {\n        if (string.IsNullOrWhiteSpace(newSourceSpec))\n            throw new ArgumentException(\"source must not be empty\");\n        newSourceSpec = newSourceSpec.Trim();\n        if (newSourceSpec.StartsWith(\"[\"))\n            throw new ArgumentException(\n                \"External workbook references are not supported in pivot source. \"\n                + \"Use a local sheet name (e.g. Sheet1!A1:D10)\");\n\n        var cachePart = pivotPart.GetPartsOfType<PivotTableCacheDefinitionPart>().FirstOrDefault()\n            ?? throw new InvalidOperationException(\"Pivot table has no cache definition part\");\n        var cacheDef = cachePart.PivotCacheDefinition\n            ?? throw new InvalidOperationException(\"Pivot cache definition is missing\");\n        var existingWsSource = cacheDef.CacheSource?.WorksheetSource\n            ?? throw new InvalidOperationException(\"Pivot cache source is not a worksheet source\");\n\n        // Parse the new source spec.\n        string newSheetName;\n        string newRef;\n        if (newSourceSpec.Contains('!'))\n        {\n            var parts = newSourceSpec.Split('!', 2);\n            newSheetName = parts[0].Trim().Trim('\\'', '\"').Trim();\n            newRef = parts[1].Trim();\n        }\n        else\n        {\n            newSheetName = existingWsSource.Sheet?.Value ?? \"\";\n            newRef = newSourceSpec;\n        }\n","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Readback.cs#L364-L400","documentation":"The cache definition part exists but its PivotCacheDefinition root element is null — the part is present but empty or has an unrecognised/unloaded root. This is a structural corruption detectable only at runtime; the helper cannot proceed because every subsequent operation (CacheSource, WorksheetSource, CacheFields) hangs off the cache definition element.","triggerScenarios":"The cache definition XML was deleted but the part and relationship remain; the root element is in a namespace the SDK did not bind; the part stream is empty or contains malformed XML that the SDK silently left unloaded.","commonSituations":"Truncated file; partial write by a crashing producer; namespace or schema-version mismatch with the loaded OpenXML SDK; hand-edited package where the cache definition content was emptied.","solutions":["Restore from a known-good copy of the file","Recreate the pivot from scratch so a fresh, well-formed cache definition is generated","Inspect the part's XML directly (rename to .zip and open pivotCacheDefinition1.xml) to confirm it has a <pivotCacheDefinition> root"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"var cachePart = pivotPart.GetPartsOfType<PivotTableCacheDefinitionPart>().FirstOrDefault();\nif (cachePart?.PivotCacheDefinition == null)\n    throw new InvalidOperationException(\"Cache definition part is missing its root element\");","typeGuard":"static bool HasCacheDefinitionRoot(PivotTablePart p)\n{\n    var cp = p.GetPartsOfType<PivotTableCacheDefinitionPart>().FirstOrDefault();\n    return cp?.PivotCacheDefinition != null;\n}","tryCatchPattern":"try { RefreshPivotCacheFromSource(pivotPart, sourceSpec); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Pivot cache definition is missing\"))\n{ /* restore from backup or recreate the pivot */ }","preventionTips":["Inspect pivotCacheDefinition1.xml inside the .zip to confirm it has a root element","Recreate the pivot if the cache definition content is empty","Avoid low-level XML edits that could empty a part"],"tags":["pivottable","cache-refresh","corrupt-file","invalid-operation","openxml-package"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}