{"record":{"id":"ef70268c6db5d6ae","repo":"iOfficeAI/OfficeCLI","slug":"pivot-table-has-no-cache-definition-part","errorCode":null,"errorMessage":"Pivot table has no cache definition part","messagePattern":"Pivot table has no cache definition part","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Readback.cs","lineNumber":380,"sourceCode":"    /// CacheSource). Replaces CacheFields, updates WorksheetSource.Reference\n    /// (and Sheet if changed), rewrites the PivotTableCacheRecordsPart, and\n    /// 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;","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Readback.cs#L362-L398","documentation":"RefreshPivotCacheFromSource walks the pivot table part for its PivotTableCacheDefinitionPart and throws if none is attached. A well-formed .xlsx produced by Excel or by this library always has one (the cache definition is what makes a pivot a pivot), so its absence indicates a corrupt, hand-assembled, or partially-written package. This is an InvalidOperationException, not an input error.","triggerScenarios":"Running refresh against a pivot whose cache part was deleted; opening a file that was written by a non-conformant producer; an earlier mutation detached the relationship; a test fixture built without the cache part.","commonSituations":"Corrupt downloads; files truncated mid-write; packages edited by low-level XML tooling that broke relationships; pivots created by code that skipped cache part creation.","solutions":["Reopen the original file from a known-good backup and retry","If the file was hand-produced, ensure a PivotTableCacheDefinitionPart is created and related to the PivotTablePart","Validate the package structure (e.g. with the Open XML SDK validator) before attempting a refresh"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"bool HasCacheDefinitionPart(PivotTablePart p) =>\n    p.GetPartsOfType<PivotTableCacheDefinitionPart>().Any();","typeGuard":"static bool HasCacheDefinitionPart(PivotTablePart p) =>\n    p.GetPartsOfType<PivotTableCacheDefinitionPart>().Any();","tryCatchPattern":"try { RefreshPivotCacheFromSource(pivotPart, sourceSpec); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no cache definition part\"))\n{ /* restore from backup or recreate the pivot */ }","preventionTips":["Keep known-good backups of pivot-bearing workbooks","Validate the package with the Open XML SDK validator before refresh","Recreate pivots through the normal creation flow rather than hand-editing parts"],"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"}