iOfficeAI/OfficeCLI · error · ArgumentException

Formula has a function call with {commas + 1} arguments; Exc

Error message

Formula has a function call with {commas + 1} arguments; Excel's limit is 255 per function. Split the call or reference a range instead.

What it means

Thrown by ValidateFormulaArgCount when a single function call in the formula has 256+ arguments (>=255 top-level commas inside one parenthesis group, excluding array constants and string literals). Excel's hard per-function limit is 255 arguments; over it the file is schema-valid but real Excel refuses to open it (0x800A03EC).

Source

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

            if (c == '"')
            {
                if (inStr && i + 1 < f.Length && f[i + 1] == '"') { i++; continue; }
                inStr = !inStr;
                continue;
            }
            if (inStr) continue;
            switch (c)
            {
                case '{': arrayDepth++; break;
                case '}': if (arrayDepth > 0) arrayDepth--; break;
                case '(': commaStack.Push(0); break;
                case ')':
                    if (commaStack.Count > 0)
                    {
                        var commas = commaStack.Pop();
                        // args = commas + 1; reject 256+ args (>=255 commas).
                        if (commas >= 255)
                            throw new ArgumentException(
                                $"Formula has a function call with {commas + 1} arguments; Excel's limit is 255 per function. "
                                + "Split the call or reference a range instead.");
                    }
                    break;
                case ',':
                    if (arrayDepth == 0 && commaStack.Count > 0)
                        commaStack.Push(commaStack.Pop() + 1);
                    break;
            }
        }
    }

    // Excel's hard ceiling on the character length of a formula / defined-name
    // refersTo / conditional-format expression. Content beyond this is silently
    // accepted, persisted, and makes real Excel refuse the file (0x800A03EC).
    internal const int MaxFormulaLength = 8192;

    /// <summary>

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Reference a range instead of listing cells: '=SUM(A1:A256)' instead of '=SUM(A1,A2,...)'.
  2. Split a 256+ arg call into multiple smaller calls (e.g. nested SUMs).
  3. Batch generated args into ranges wherever contiguous.

Example fix

// before
string formula = "=SUM(" + string.Join(",", cells) + ")"; // cells.Count > 255
// after
string formula = "=SUM(A1:A256)"; // range reference
// or batch:
string formula = "=SUM(SUM(A1:A128),SUM(A129:A256))";
Defensive patterns

Strategy: validation

Validate before calling

// Count top-level args per call before submitting
static int MaxArgsPerCall(string formula)
{
    var stack = new Stack<int>(); int arrayDepth = 0, max = 0; bool inStr = false;
    for (int i = 0; i < formula.Length; i++)
    {
        char c = formula[i];
        if (c == '"') { if (inStr && i+1 < formula.Length && formula[i+1]=='"') i++; else inStr = !inStr; continue; }
        if (inStr) continue;
        if (c == '{') arrayDepth++;
        else if (c == '}') { if (arrayDepth>0) arrayDepth--; }
        else if (c == '(') stack.Push(0);
        else if (c == ')' && stack.Count > 0) { max = Math.Max(max, stack.Pop()+1); }
        else if (c == ',' && arrayDepth == 0 && stack.Count > 0) stack.Push(stack.Pop()+1);
    }
    return max;
}

Type guard

null

Try / catch

try { /* set formula */ }
catch (ArgumentException ex) when (ex.Message.Contains("arguments; Excel's limit is 255"))
{ /* rewrite as a range reference or nested calls, then retry */ }

Prevention

When it happens

Trigger: Writing a formula like '=SUM(A1,A2,...,A256)' with 256+ comma-separated args in one call; '=CONCATENATE(' with hundreds of args; generated formulas that expand a list into individual arguments.

Common situations: Programmatically building a function call from a list without batching; CONCATENATE/SUM with many individually-listed cells; generating args from a column without using a range.

Related errors


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