{"record":{"id":"42128c11201d989a","repo":"iOfficeAI/OfficeCLI","slug":"formula-has-a-function-call-with-commas-1-argu","errorCode":null,"errorMessage":"Formula has a function call with {commas + 1} arguments; Excel's limit is 255 per function. Split the call or reference a range instead.","messagePattern":"Formula has a function call with (.+?) arguments; Excel's limit is 255 per function\\. Split the call or reference a range instead\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs","lineNumber":246,"sourceCode":"            if (c == '\"')\n            {\n                if (inStr && i + 1 < f.Length && f[i + 1] == '\"') { i++; continue; }\n                inStr = !inStr;\n                continue;\n            }\n            if (inStr) continue;\n            switch (c)\n            {\n                case '{': arrayDepth++; break;\n                case '}': if (arrayDepth > 0) arrayDepth--; break;\n                case '(': commaStack.Push(0); break;\n                case ')':\n                    if (commaStack.Count > 0)\n                    {\n                        var commas = commaStack.Pop();\n                        // args = commas + 1; reject 256+ args (>=255 commas).\n                        if (commas >= 255)\n                            throw new ArgumentException(\n                                $\"Formula has a function call with {commas + 1} arguments; Excel's limit is 255 per function. \"\n                                + \"Split the call or reference a range instead.\");\n                    }\n                    break;\n                case ',':\n                    if (arrayDepth == 0 && commaStack.Count > 0)\n                        commaStack.Push(commaStack.Pop() + 1);\n                    break;\n            }\n        }\n    }\n\n    // Excel's hard ceiling on the character length of a formula / defined-name\n    // refersTo / conditional-format expression. Content beyond this is silently\n    // accepted, persisted, and makes real Excel refuse the file (0x800A03EC).\n    internal const int MaxFormulaLength = 8192;\n\n    /// <summary>","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs#L228-L264","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reference a range instead of listing cells: '=SUM(A1:A256)' instead of '=SUM(A1,A2,...)'.","Split a 256+ arg call into multiple smaller calls (e.g. nested SUMs).","Batch generated args into ranges wherever contiguous."],"exampleFix":"// before\nstring formula = \"=SUM(\" + string.Join(\",\", cells) + \")\"; // cells.Count > 255\n// after\nstring formula = \"=SUM(A1:A256)\"; // range reference\n// or batch:\nstring formula = \"=SUM(SUM(A1:A128),SUM(A129:A256))\";","handlingStrategy":"validation","validationCode":"// Count top-level args per call before submitting\nstatic int MaxArgsPerCall(string formula)\n{\n    var stack = new Stack<int>(); int arrayDepth = 0, max = 0; bool inStr = false;\n    for (int i = 0; i < formula.Length; i++)\n    {\n        char c = formula[i];\n        if (c == '\"') { if (inStr && i+1 < formula.Length && formula[i+1]=='\"') i++; else inStr = !inStr; continue; }\n        if (inStr) continue;\n        if (c == '{') arrayDepth++;\n        else if (c == '}') { if (arrayDepth>0) arrayDepth--; }\n        else if (c == '(') stack.Push(0);\n        else if (c == ')' && stack.Count > 0) { max = Math.Max(max, stack.Pop()+1); }\n        else if (c == ',' && arrayDepth == 0 && stack.Count > 0) stack.Push(stack.Pop()+1);\n    }\n    return max;\n}","typeGuard":"null","tryCatchPattern":"try { /* set formula */ }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"arguments; Excel's limit is 255\"))\n{ /* rewrite as a range reference or nested calls, then retry */ }","preventionTips":["Excel caps each function call at 255 arguments.","Prefer range references (A1:A256) over listing cells.","Array-constant commas ({1,2,3}) do not count toward the limit."],"tags":["excel","formulas","limits","input-validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}