iOfficeAI/OfficeCLI · error · ArgumentException

Row span {cellRef} covers {r2 - r1 + 1} rows (limit 1024). N

Error message

Row span {cellRef} covers {r2 - r1 + 1} rows (limit 1024). Narrow the span or address rows individually as row[N].

What it means

A path axis ref like 1:5000 expands to more than 1024 rows. As with the column case, the library refuses to materialize the span to avoid resource blowup. The 1024 cap is a per-call guard, not the Excel row limit (1048576).

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.cs:52

        {
            var from = ColumnNameToIndex(colRef.Groups[1].Value.ToUpperInvariant());
            var to = ColumnNameToIndex(colRef.Groups[2].Value.ToUpperInvariant());
            if (from > to) (from, to) = (to, from);
            if (to - from + 1 > 1024)
                throw new ArgumentException(
                    $"Column span {cellRef} covers {to - from + 1} columns (limit 1024). Narrow the span or address columns individually as col[X].");
            var cols = new List<string>();
            for (var i = from; i <= to; i++) cols.Add($"col[{IndexToColumnName(i)}]");
            return cols;
        }
        var rowRef = System.Text.RegularExpressions.Regex.Match(cellRef, @"^(\d+):(\d+)$");
        if (rowRef.Success
            && uint.TryParse(rowRef.Groups[1].Value, out var r1) && r1 >= 1
            && uint.TryParse(rowRef.Groups[2].Value, out var r2) && r2 >= 1)
        {
            if (r1 > r2) (r1, r2) = (r2, r1);
            if (r2 - r1 + 1 > 1024)
                throw new ArgumentException(
                    $"Row span {cellRef} covers {r2 - r1 + 1} rows (limit 1024). Narrow the span or address rows individually as row[N].");
            var rows = new List<string>();
            for (var i = r1; i <= r2; i++) rows.Add($"row[{i}]");
            return rows;
        }
        return null;
    }

    /// <summary>
    /// Parse a print-margin value into inches (PageMargins schema unit).
    /// Accepts "1in", "2.5cm", "1.27cm", "72pt", "10mm", or a bare number (inches).
    /// </summary>
    internal static double ParseMarginInches(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("Invalid margin: empty value.");
        var v = value.Trim().ToLowerInvariant();
        double num;

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Narrow the span to <= 1024 rows per call.
  2. Address rows individually as row[N] for scattered targets.
  3. Use sheet-level row defaults or styles instead of expanding a span.

Example fix

// before
sheet.ApplyToAxis("1:5000", ...);   // 5000 rows > 1024

// after
for (var start = 1; start <= 5000; start += 1024) {
    var end = Math.Min(start + 1023, 5000);
    sheet.ApplyToAxis($"{start}:{end}", ...);
}
Defensive patterns

Strategy: validation

Validate before calling

const int MaxAxisSpan = 1024;
static bool RowSpanOk(uint r1, uint r2) =>
    Math.Abs((long)r2 - r1) + 1 <= MaxAxisSpan;

Try / catch

try { sheet.ApplyToAxis(span, ...); }
catch (ArgumentException ex) when (ex.Message.Contains("limit 1024")) {
    foreach (var chunk in ChunkRowSpan(span, 1024)) sheet.ApplyToAxis(chunk, ...);
}

Prevention

When it happens

Trigger: Passing an axis ref matching ^(\d+):(\d+)$ where both endpoints are >= 1 and (r2 - r1 + 1) > 1024, e.g. '1:5000', '100:2000'.

Common situations: Applying row formatting or values across a large dataset in one call; auto-generated rules targeting broad row ranges; misjudging the per-call budget.

Related errors


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