iOfficeAI/OfficeCLI · error · ArgumentException

pivot at {ptPosition} does not fit: computed end col={minEnd

Error message

pivot at {ptPosition} does not fit: computed end col={minEndColIdx} row={minEndRow} exceeds sheet dimensions (max XFD1048576)

What it means

Thrown by AddPivotTable's fit check when the conservative lower-bound output rectangle would extend beyond Excel's sheet maximums. The handler computes the minimum end column as anchorCol + nSourceCols - 1 and minimum end row as anchorRow + nDataRows + 1 (header + data + grand-total); if either exceeds ExcelMaxCol (16384/XFD) or ExcelMaxRow (1048576) it rejects rather than writing a pivot Excel cannot place.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs:1754

            const int ExcelMaxCol = 16384; // XFD
            const int ExcelMaxRow = 1048576;
            var srcRefParts = sourceRef.Replace("$", "").Split(':');
            if (srcRefParts.Length == 2)
            {
                var (srcStartCol, srcStartRow) = ParseCellReference(srcRefParts[0].Trim().ToUpperInvariant());
                var (srcEndCol, srcEndRow)     = ParseCellReference(srcRefParts[1].Trim().ToUpperInvariant());
                int nSourceCols = ColumnNameToIndex(srcEndCol) - ColumnNameToIndex(srcStartCol) + 1;
                int nDataRows   = srcEndRow - srcStartRow; // header excluded
                var (anchorColStr, anchorRow) = ParseCellReference(ptPosition.ToUpperInvariant());
                int anchorColIdx = ColumnNameToIndex(anchorColStr);
                // Conservative lower-bound: pivot needs at least nSourceCols columns
                // (row-label cols + value cols + grand-total col) and at least
                // nDataRows + 2 rows (header + data rows + grand-total row).
                int minEndColIdx = anchorColIdx + nSourceCols - 1;
                int minEndRow    = anchorRow + nDataRows + 1;
                if (minEndColIdx > ExcelMaxCol || minEndRow > ExcelMaxRow)
                {
                    throw new ArgumentException(
                        $"pivot at {ptPosition} does not fit: computed end col={minEndColIdx} row={minEndRow} exceeds sheet dimensions (max XFD1048576)");
                }

                // CONSISTENCY(pivot-output-overlap): two pivot tables whose
                // <x:location> rectangles overlap on the same sheet make
                // Excel surface a "found a problem" repair dialog because
                // the output cells fight for ownership. Mirror the T4
                // table-table overlap check using the conservative output
                // bounds computed above. Cross-sheet pivots are fine.
                var newPivotRange = $"{IndexToColumnName(anchorColIdx)}{anchorRow}:" +
                                    $"{IndexToColumnName(minEndColIdx)}{minEndRow}";
                foreach (var existingPivot in ptWorksheet.PivotTableParts
                    .Select(ptp => ptp.PivotTableDefinition)
                    .Where(d => d != null))
                {
                    var existingLoc = existingPivot!.Location?.Reference?.Value;
                    if (string.IsNullOrEmpty(existingLoc)) continue;
                    if (RangesOverlap(newPivotRange.ToUpperInvariant(), existingLoc.ToUpperInvariant()))

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Move the anchor left/up, e.g. position=A1 or a column well inside the sheet.
  2. Omit position to let the handler auto-place after the source range (it picks a column 2 right of the source end).
  3. Reduce the source range size if the pivot genuinely cannot fit.

Example fix

// before
add /Report/pivottable --prop source=Data!A1:Z5000 --prop position=XFC1
// after
add /Report/pivottable --prop source=Data!A1:Z5000 --prop position=A1
Defensive patterns

Strategy: validation

Validate before calling

// Conservatively check the pivot output fits within Excel bounds before Add.
const int ExcelMaxCol = 16384, ExcelMaxRow = 1048576;
var (anchorColStr, anchorRow) = ParseCellReference(position.ToUpperInvariant());
int anchorColIdx = ColumnNameToIndex(anchorColStr);
int nSourceCols = ColumnNameToIndex(srcEndCol) - ColumnNameToIndex(srcStartCol) + 1;
int nDataRows = srcEndRow - srcStartRow;
if (anchorColIdx + nSourceCols - 1 > ExcelMaxCol || anchorRow + nDataRows + 1 > ExcelMaxRow)
    throw new InvalidOperationException($"pivot at {position} does not fit within sheet bounds.");

Try / catch

try { handler.Add(parentPath, "pivottable", null, props); }
catch (ArgumentException ex) when (ex.Message.Contains("does not fit"))
{ /* move the anchor left/up or omit position for auto-placement, then retry */ }

Prevention

When it happens

Trigger: Calling Add('/Report/pivottable', ...) with a large source range and an anchor position near the right or bottom edge, e.g. position=XFB1 with a 20-column source, or a position whose computed end exceeds XFD1048576.

Common situations: Auto-positioning collides with the edge, supplying a position far down/right, or a source with many columns/rows.

Related errors


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