iOfficeAI/OfficeCLI · error · ArgumentException

Source range has no data

Error message

Source range has no data

What it means

Thrown by RefreshPivotCacheFromSource after ReadSourceData(sourceWsPart, newRef) returns an empty headers array. This means the new source range produced no header row at all — the cells at the top of the specified range are entirely empty, or the range reference itself resolves to nothing readable.

Source

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

            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)
        {
            // Axes that the same Set call is explicitly overwriting are
            // excluded from validation — their new values will be parsed
            // against the fresh headers by RebuildFieldAreas.
            bool rowsOverwritten = pendingFieldAreaProps?.ContainsKey("rows") == true;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the range's first row contains at least one non-empty cell to serve as the header.
  2. Double-check the range coordinates — the top-left cell should be the first header.
  3. If the data moved, update the range to the new location.
  4. Confirm the range reference syntax matches what ReadSourceData expects (e.g. A1:D10).

Example fix

// before — row 1 is blank, headers are on row 2
Set pivot source=Sheet1!A1:D50
// after
Set pivot source=Sheet1!A2:D50
Defensive patterns

Strategy: validation

Validate before calling

// Before Set source=, confirm the range has a non-empty header row
var (testHeaders, _, _) = ReadSourceData(sourceWsPart, testRange);
if (testHeaders.Length == 0)
    throw new ArgumentException($"Range {testRange} has no header data — check coordinates.");

Prevention

When it happens

Trigger: Calling Set source=A1:Z50 where every cell in row 1 (the header row) is empty. Or passing a range reference whose syntax is malformed so ReadSourceData interprets it as an empty region.

Common situations: Pointing the source at the wrong region of a sheet; the data was moved and the old range is now blank; off-by-one in the range (e.g. header row is actually row 2 but spec says A1); range points below the last used row.

Related errors


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