iOfficeAI/OfficeCLI · error · ArgumentException

Source range has no data

Error message

Source range has no data

What it means

Thrown by CreatePivotTable (the Add path) after ReadSourceData(sourceSheet, sourceRef) returns an empty headers array. The source range specified at pivot creation produced no header row — the top row of the range is entirely empty or the range resolves to nothing. This is the same check as error 343 but on the creation path rather than the refresh path.

Source

Thrown at src/officecli/Core/PivotTableHelper.cs:958

        // CONSISTENCY(thread-static-pivot-opts): same pattern — grand totals
        // options reach item builders, geometry, and every renderer via
        // ActiveRowGrandTotals/ActiveColGrandTotals.
        using var _gtScope = PushGrandTotalsOptions(properties);
        // CONSISTENCY(thread-static-pivot-opts): same pattern for subtotals.
        using var _subScope = PushSubtotalsOptions(properties);
        // CONSISTENCY(thread-static-pivot-opts): same pattern for layout mode.
        using var _layoutScope = PushLayoutMode(properties);
        // CONSISTENCY(thread-static-pivot-opts): same pattern for repeatItemLabels.
        using var _repeatScope = PushRepeatItemLabels(properties);
        // CONSISTENCY(thread-static-pivot-opts): same pattern for insertBlankRow.
        using var _blankRowScope = PushInsertBlankRow(properties);
        // CONSISTENCY(thread-static-pivot-opts): same pattern for grandTotalCaption.
        using var _captionScope = PushGrandTotalCaption(properties);

        // 1. Read source data to build cache
        var (headers, columnData, columnStyleIds) = ReadSourceData(sourceSheet, sourceRef);
        if (headers.Length == 0)
            throw new ArgumentException("Source range has no data");
        // CONSISTENCY(empty-pivot-source): a header row with zero data rows
        // (e.g. A1:D1) silently produces an empty pivot whose cache has no
        // records — Excel opens it but renders nothing. Reject it with the
        // same family of ArgumentException as the no-headers case so callers
        // get a single, predictable error path. Bt#8 / fuzzer baseline.
        if (columnData.Count == 0 || columnData[0].Length == 0)
            throw new ArgumentException("Source range has no data rows");

        // 1b. Date auto-grouping preprocessing. Scans rows/cols/filters props
        // for `fieldName:grouping` syntax (e.g. `rows='日期:month,城市'`) and
        // creates a new virtual column per grouped field containing the
        // bucketed labels. The raw field spec is rewritten to reference the
        // new virtual column so ParseFieldList below sees a clean name.
        //
        // Supported groupings:
        //   :year    → "2024"
        //   :quarter → "2024-Q1"
        //   :month   → "2024-01"

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Ensure the range's first row contains at least one non-empty cell as the header.
  2. Verify the range top-left cell is the first header column.
  3. Check the range syntax matches the expected format (e.g. A1:D10).
  4. If data moved, point the source at the new location.

Example fix

// before — headers are on row 2, range starts at row 1
Add pivot source=Sheet1!A1:D10 name='P'
// after
Add pivot source=Sheet1!A2:D10 name='P'
Defensive patterns

Strategy: validation

Validate before calling

// Before Add pivot, confirm the range has headers
var (testHeaders, _, _) = ReadSourceData(sourceSheet, sourceRef);
if (testHeaders.Length == 0)
    throw new ArgumentException($"Source range {sourceRef} has no header data.");

Prevention

When it happens

Trigger: Calling Add pivot source=Sheet1!A1:D10 where row 1 (the header row) is completely blank. Or a malformed range reference that ReadSourceData cannot interpret, yielding zero headers.

Common situations: Pointing the source at the wrong area of a sheet; headers are on a different row than the range's top; data was cleared; off-by-one in the range coordinates.

Related errors


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