iOfficeAI/OfficeCLI · error · InvalidOperationException

Source sheet has no relationship id

Error message

Source sheet has no relationship id

What it means

Thrown when the <Sheet> element WAS found in Workbook.Sheets but its Id attribute is null or not a string. Every <Sheet> in valid OOXML must carry an r:id relationship attribute linking it to a worksheet part. The pattern 'sheetEntry.Id?.Value is not string srcRelId' guards both the null-Id and non-string-Id cases, indicating the package's relationship layer is corrupt.

Source

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

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

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Open and re-save the workbook in Excel or LibreOffice to repair the relationship layer.
  2. Use a different worksheet as the pivot source.
  3. Validate the package structure with an OOXML repair/conformance tool.
  4. If generating the file programmatically, ensure every <sheet r:id="..."/> entry has a matching relationship in workbook.xml.rels.
Defensive patterns

Strategy: try-catch

Try / catch

try { SetPivotTableProperties(pivotPart, new { source = spec }); }
catch (InvalidOperationException ex) when (ex.Message.Contains("relationship id"))
{
    // Package corruption — the sheet's r:id is missing.
    // Re-save in Excel/LibreOffice, or fall back to a different source sheet.
    ReportPackageCorruption(workbookPath);
}

Prevention

When it happens

Trigger: The xlsx was hand-edited, produced by a broken third-party exporter, or suffered a partial/corrupted save, stripping the r:id from a <sheet> element. Effectively unreachable with files written by Excel or LibreOffice.

Common situations: Files generated by low-quality OOXML libraries that omit relationship ids; manual XML editing of the sheet inside the zip; a save interrupted partway through leaving workbook.xml with dangling sheet entries.

Related errors


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