iOfficeAI/OfficeCLI · error · InvalidOperationException

Workbook part not reachable from pivot table part

Error message

Workbook part not reachable from pivot table part

What it means

To resolve the new source sheet, RefreshPivotCacheFromSource walks pivotPart.GetParentParts() up through a WorksheetPart to a WorkbookPart. If that chain cannot reach a WorkbookPart (no WorksheetPart ancestor, or no WorkbookPart above it), the helper cannot locate the target sheet and throws. This indicates an orphaned or detached pivot table part whose packaging relationships are broken.

Source

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

        // 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;
        }

        // Locate the source worksheet via the workbook part.
        var workbookPart = pivotPart.GetParentParts().OfType<WorksheetPart>().FirstOrDefault()
            ?.GetParentParts().OfType<WorkbookPart>().FirstOrDefault()
            ?? throw new InvalidOperationException("Workbook part not reachable from pivot table part");
        var sheetEntry = workbookPart.Workbook?.Sheets?.Elements<Sheet>()
            .FirstOrDefault(s => s.Name?.Value == newSheetName)
            ?? throw new ArgumentException($"Source sheet not found: {newSheetName}");
        if (sheetEntry.Id?.Value is not string srcRelId)
            throw new InvalidOperationException("Source sheet has no relationship id");
        var sourceWsPart = workbookPart.GetPartById(srcRelId) as WorksheetPart
            ?? throw new InvalidOperationException("Source sheet relationship does not resolve to a WorksheetPart");

        // Re-read source data from the new range.
        var (headers, columnData, _) = ReadSourceData(sourceWsPart, newRef);
        if (headers.Length == 0)
            throw new ArgumentException("Source range has no data");
        if (columnData.Count == 0 || columnData[0].Length == 0)
            throw new ArgumentException("Source range has no data rows");

        // R15-2: Before mutating any cache/pivot state, validate that existing
        // row/col/value/filter field references still fit inside the new
        // (possibly narrower) header list. A silent drop or index clamp here

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Reopen a known-good copy of the file
  2. Ensure the PivotTablePart is reachable from a WorksheetPart that is itself reachable from the WorkbookPart via standard relationships
  3. Recreate the pivot through normal creation flow so the part hierarchy is wired correctly
Defensive patterns

Strategy: try-catch

Validate before calling

var workbookPart = pivotPart.GetParentParts().OfType<WorksheetPart>().FirstOrDefault()
    ?.GetParentParts().OfType<WorkbookPart>().FirstOrDefault();
if (workbookPart == null)
    throw new InvalidOperationException("Pivot part is not attached to the workbook hierarchy");

Type guard

static bool IsAttachedToWorkbook(PivotTablePart p) =>
    p.GetParentParts().OfType<WorksheetPart>().FirstOrDefault()
        ?.GetParentParts().OfType<WorkbookPart>().FirstOrDefault() != null;

Try / catch

try { RefreshPivotCacheFromSource(pivotPart, sourceSpec); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Workbook part not reachable"))
{ /* restore from backup or recreate the pivot with correct relationships */ }

Prevention

When it happens

Trigger: A pivot table part that was attached directly to the workbook without going through a worksheet; a package where the worksheet-to-workbook relationship was severed; a test fixture built from loose parts without wiring parent relationships; a part extracted and re-inserted into a different package without re-establishing relationships.

Common situations: Files assembled by code that skipped the standard worksheet->workbook relationship; corruption of the .rels parts; packages reconstructed from extracted XML without re-linking; unusual producers that attach pivots at non-standard locations.

Related errors


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