iOfficeAI/OfficeCLI · error · InvalidOperationException

Pivot table has no cache definition part

Error message

Pivot table has no cache definition part

What it means

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.

Source

Thrown at src/officecli/Core/PivotTableHelper.Readback.cs:380

    /// CacheSource). Replaces CacheFields, updates WorksheetSource.Reference
    /// (and Sheet if changed), rewrites the PivotTableCacheRecordsPart, and
    /// resizes pivotDef.PivotFields to match the new column count. Existing
    /// PivotField Axis/DataField assignments are reset because indices may no
    /// longer line up — RebuildFieldAreas reapplies them after this returns.
    /// </summary>
    private static void RefreshPivotCacheFromSource(PivotTablePart pivotPart, string newSourceSpec,
        Dictionary<string, string>? pendingFieldAreaProps = null)
    {
        if (string.IsNullOrWhiteSpace(newSourceSpec))
            throw new ArgumentException("source must not be empty");
        newSourceSpec = newSourceSpec.Trim();
        if (newSourceSpec.StartsWith("["))
            throw new ArgumentException(
                "External workbook references are not supported in pivot source. "
                + "Use a local sheet name (e.g. Sheet1!A1:D10)");

        var cachePart = pivotPart.GetPartsOfType<PivotTableCacheDefinitionPart>().FirstOrDefault()
            ?? throw new InvalidOperationException("Pivot table has no cache definition part");
        var cacheDef = cachePart.PivotCacheDefinition
            ?? throw new InvalidOperationException("Pivot cache definition is missing");
        var existingWsSource = cacheDef.CacheSource?.WorksheetSource
            ?? throw new InvalidOperationException("Pivot cache source is not a worksheet source");

        // Parse the new source spec.
        string newSheetName;
        string newRef;
        if (newSourceSpec.Contains('!'))
        {
            var parts = newSourceSpec.Split('!', 2);
            newSheetName = parts[0].Trim().Trim('\'', '"').Trim();
            newRef = parts[1].Trim();
        }
        else
        {
            newSheetName = existingWsSource.Sheet?.Value ?? "";
            newRef = newSourceSpec;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Reopen the original file from a known-good backup and retry
  2. If the file was hand-produced, ensure a PivotTableCacheDefinitionPart is created and related to the PivotTablePart
  3. Validate the package structure (e.g. with the Open XML SDK validator) before attempting a refresh
Defensive patterns

Strategy: try-catch

Validate before calling

bool HasCacheDefinitionPart(PivotTablePart p) =>
    p.GetPartsOfType<PivotTableCacheDefinitionPart>().Any();

Type guard

static bool HasCacheDefinitionPart(PivotTablePart p) =>
    p.GetPartsOfType<PivotTableCacheDefinitionPart>().Any();

Try / catch

try { RefreshPivotCacheFromSource(pivotPart, sourceSpec); }
catch (InvalidOperationException ex) when (ex.Message.Contains("no cache definition part"))
{ /* restore from backup or recreate the pivot */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/ef70268c6db5d6ae. Report an issue: GitHub.