iOfficeAI/OfficeCLI · error · ArgumentException

Invalid source range: {sourceRef}

Error message

Invalid source range: {sourceRef}

What it means

ArgumentException thrown in ReadSourceData when the sourceRef does not parse as exactly two colon-separated cell refs (after stripping '$'). The reader expects an Excel range like 'A1:D100'. Anything without exactly one ':' — a single cell, three parts, or no colon — is rejected.

Source

Thrown at src/officecli/Core/PivotTableHelper.Cache.cs:264

            9  => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec",
            _  => month.ToString(System.Globalization.CultureInfo.InvariantCulture),
        };

    private static string CapitalizeFirst(string s)
        => string.IsNullOrEmpty(s) ? s : char.ToUpperInvariant(s[0]) + s.Substring(1);

    // ==================== Source Data Reader ====================

    private static (string[] headers, List<string[]> columnData, uint?[] columnStyleIds) ReadSourceData(
        WorksheetPart sourceSheet, string sourceRef)
    {
        var ws = sourceSheet.Worksheet ?? throw new InvalidOperationException("Worksheet missing");
        var sheetData = ws.GetFirstChild<SheetData>();
        if (sheetData == null) return (Array.Empty<string>(), new List<string[]>(), Array.Empty<uint?>());

        // Parse range "A1:D100"
        var parts = sourceRef.Replace("$", "").Split(':');
        if (parts.Length != 2) throw new ArgumentException($"Invalid source range: {sourceRef}");

        var (startCol, startRow) = ParseCellRef(parts[0]);
        var (endCol, endRow) = ParseCellRef(parts[1]);

        var startColIdx = ColToIndex(startCol);
        var endColIdx = ColToIndex(endCol);
        // R6-3: reject columns beyond Excel's hard max (XFD = 16384). Previously
        // XFE / XFZ / ZZZZ silently parsed into oversized indices, produced a
        // giant colCount, and either crashed deep in the renderer or wrote an
        // invalid source range into the cache.
        const int ExcelMaxColumn = 16384; // XFD
        if (startColIdx > ExcelMaxColumn)
            throw new ArgumentException($"Column {startCol} out of range (max: XFD)");
        if (endColIdx > ExcelMaxColumn)
            throw new ArgumentException($"Column {endCol} out of range (max: XFD)");
        var colCount = endColIdx - startColIdx + 1;

        // Read all rows in range. We also capture the StyleIndex of the first

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Supply a two-corner A1 range including both endpoints, e.g. sourceRef="A1:D100".
  2. Remove any extra colons; whole-sheet references must be spelled with the full corner range (e.g. 'A1:Z1000'), not a single cell.
  3. Ensure the value is an A1 range, not a defined name.

Example fix

// before
sourceRef="A1"
// after
sourceRef="A1:D100"
Defensive patterns

Strategy: validation

Validate before calling

// Require exactly two colon-separated corners after stripping $:
static bool IsValidSourceRange(string sourceRef)
    => sourceRef.Replace("$", "").Split(':').Length == 2;

Prevention

When it happens

Trigger: Passing a pivot table sourceRef / source range that is a single cell ('A1'), a whole-column form the reader does not accept, has multiple colons ('A1:B2:C3'), or is empty/garbage. sourceRef.Replace("$","").Split(':') must yield exactly 2 parts.

Common situations: Using a single-cell reference where a full range is required; passing a named range instead of an A1-style range; malformed range from user input or a broken dump/round-trip.

Related errors


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