iOfficeAI/OfficeCLI · error · ArgumentException

Source range has no data rows

Error message

Source range has no data rows

What it means

Thrown by CreatePivotTable (the Add path) when headers exist but columnData has zero rows. The source range is header-only (e.g. A1:D1) — column names present but no data records below. This produces a pivot with an empty cache that Excel opens but renders nothing; the guard rejects it early (CONSISTENCY empty-pivot-source, Bt#8 / fuzzer baseline) with the same ArgumentException family as the no-headers case.

Source

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

        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"
        //   :day     → "2024-01-05"
        //
        // Compose multiple groupings for hierarchical date layouts:
        // `rows='日期:year,日期:quarter'` → 2-level year-then-quarter.
        //
        // Returns a list of DateGroupSpec describing each derived field so
        // BuildCacheDefinition can emit the native <fieldGroup> + <rangePr> +

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Extend the range to include at least one data row below the header (minimum A1:D2).
  2. Confirm the worksheet has populated data rows in the specified columns.
  3. If using a named/dynamic range, verify it spans more than one row.

Example fix

// before
Add pivot source=Sheet1!A1:D1 name='P'
// after
Add pivot source=Sheet1!A1:D100 name='P'
Defensive patterns

Strategy: validation

Validate before calling

// Before Add pivot, confirm at least one data row exists
var (testHeaders, testRows, _) = ReadSourceData(sourceSheet, sourceRef);
if (testHeaders.Length > 0 && (testRows.Count == 0 || testRows[0].Length == 0))
    throw new ArgumentException($"Source range {sourceRef} is header-only — no data rows.");

Prevention

When it happens

Trigger: Calling Add pivot source=Sheet1!A1:D1 (only the header row, no data beneath). Or a range whose data cells are all empty so ReadSourceData yields zero record rows.

Common situations: Range bottom boundary set too high or too low (e.g. A1:D1); template with headers but no data yet; data cleared but headers remain.

Related errors


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