iOfficeAI/OfficeCLI · error · InvalidOperationException

Source sheet relationship does not resolve to a WorksheetPar

Error message

Source sheet relationship does not resolve to a WorksheetPart

What it means

Thrown when the sheet's relationship id resolves via GetPartById but the returned OpenXmlPart is not a WorksheetPart (the 'as WorksheetPart' cast yields null). This happens when the named 'sheet' is actually a chartsheet or another non-worksheet part type, so the pivot source cannot read cell data from it.

Source

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

            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
        // would leave the DataFields pointing past the rendered columnData,
        // crashing RenderPivotIntoSheet with ArgumentOutOfRangeException.
        // Prefer strict error over data loss: user must explicitly restate the
        // affected axes in the same Set call if they intended to drop them.
        var newFieldCount = headers.Length;
        var existingPivotDef = pivotPart.PivotTableDefinition;
        if (existingPivotDef != null)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Point the pivot source at a range that lives on an actual worksheet, not a chartsheet.
  2. Check that the sheet tab you are referencing is a worksheet type (has a grid of cells), not a chart-only sheet.
  3. If the file is corrupt, re-save in Excel/LibreOffice to rebuild part types.

Example fix

// before — 'Chart1' is a chartsheet, not a worksheet
Set pivot source=Chart1!A1:D10
// after — use the data worksheet
Set pivot source=Data!A1:D10
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the target sheet resolves to a real WorksheetPart before setting source
var sheetEntry = workbookPart.Workbook?.Sheets?.Elements<Sheet>()
    .FirstOrDefault(s => s.Name?.Value == targetSheetName);
if (sheetEntry?.Id?.Value is string relId)
{
    var part = workbookPart.GetPartById(relId);
    if (part is not WorksheetPart)
        throw new InvalidOperationException($"'{targetSheetName}' is not a worksheet (got {part?.GetType().Name}); use a data worksheet.");
}

Type guard

static bool IsWorksheet(WorkbookPart wbPart, string sheetName) =>
    wbPart.Workbook?.Sheets?.Elements<Sheet>()
        .FirstOrDefault(s => s.Name?.Value == sheetName)?.Id?.Value is string rid
    && wbPart.GetPartById(rid) is WorksheetPart;

Prevention

When it happens

Trigger: Setting source= to a range on a chartsheet (ChartsheetPart) instead of a real worksheet. Or a workbook where the relationship target for the sheet was changed to a non-worksheet part type through corruption or aggressive manipulation.

Common situations: Workbook contains chart sheets; user picks the wrong tab name that happens to be a chartsheet; a third-party tool repackaged the workbook and mislabeled part types.

Related errors


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