{"record":{"id":"a8aba5c5dad73889","repo":"iOfficeAI/OfficeCLI","slug":"number-format-is-formatcode-length-chars-excel","errorCode":null,"errorMessage":"number format is {formatCode.Length} chars; Excel's limit is 255.","messagePattern":"number format is (.+?) chars; Excel's limit is 255\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/ExcelStyleManager.cs","lineNumber":721,"sourceCode":"        for (int i = 0; i < formatCode.Length; i++)\n        {\n            var c = formatCode[i];\n            if (c == '\"') nfInQuote = !nfInQuote;\n            else if (!nfInQuote && c == '[') nfInBracket = true;\n            else if (!nfInQuote && c == ']') nfInBracket = false;\n            else if (!nfInQuote && c == '\\\\') i++;\n            else if (!nfInQuote && !nfInBracket && c == ';') nfSections++;\n        }\n        if (nfSections > 4)\n            throw new ArgumentException(\n                $\"number format has {nfSections} sections: '{formatCode}'. Excel allows \" +\n                \"at most 4 (positive;negative;zero;text); more makes Excel refuse to open the file.\");\n\n        // Excel caps format codes at 255 chars; schema validate flags it but\n        // the write path should fail fast rather than rely on a separate\n        // validate run.\n        if (formatCode.Length > 255)\n            throw new ArgumentException(\n                $\"number format is {formatCode.Length} chars; Excel's limit is 255.\");\n\n        // Unbalanced [brackets] (e.g. formatCode=\"[\") pass schema validation\n        // but real Excel refuses the whole file (0x800A03EC). Count outside\n        // quoted literals; escaped \\[ is a literal char.\n        int nfBracketDepth = 0;\n        bool nfBrQuote = false;\n        for (int i = 0; i < formatCode.Length; i++)\n        {\n            var c = formatCode[i];\n            if (c == '\"') nfBrQuote = !nfBrQuote;\n            else if (!nfBrQuote && c == '\\\\') i++;\n            else if (!nfBrQuote && c == '[') nfBracketDepth++;\n            else if (!nfBrQuote && c == ']')\n            {\n                nfBracketDepth--;\n                if (nfBracketDepth < 0) break;\n            }","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/ExcelStyleManager.cs#L703-L739","documentation":"Thrown by the Excel number-format writer when a custom format code exceeds 255 characters. Excel itself enforces this cap (ECMA-376 stores it in a bounded field), so the write path fails fast instead of relying on a separate schema-validate run. A format over 255 chars makes Excel refuse to open the produced file.","triggerScenarios":"Calling an Excel styling API that sets a custom number format (e.g. style numberFormat=\"...\") with a formatCode string longer than 255 characters. The check fires after the section-count (>4) guard and before the bracket-balance check in the format validator.","commonSituations":"Programmatically concatenating many repeated color/condition sections into one format string; building a format from a template/loop without truncation; pasting a long format copied from another tool.","solutions":["Shorten the format code to <=255 characters, e.g. by reducing repeated color/locale tokens or splitting conditions across cells.","Audit the code that assembles the format string and cap/bound its length before passing it to the style API.","Run the project's schema validator on the format before write to surface the limit earlier."],"exampleFix":"// before\nvar fmt = string.Join(\";\", Enumerable.Repeat(\"[Red]0.00\", 80)); // >255 chars\nstyle.NumberFormat = fmt;\n// after\nvar fmt = string.Join(\";\", new[]{ \"[Red]0.00\", \"[Blue]-0.00\" }); // <=255\nstyle.NumberFormat = fmt;","handlingStrategy":"validation","validationCode":"static void AssertFormatLength(string fmt)\n{\n    if (fmt is null) return;\n    if (fmt.Length > 255)\n        throw new ArgumentOutOfRangeException(nameof(fmt), $\"number format is {fmt.Length} chars; limit is 255.\");\n}","typeGuard":null,"tryCatchPattern":"try { ApplyNumberFormat(fmt); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Excel's limit is 255\"))\n{ /* truncate or reject the format */ }","preventionTips":["Bound any generated/concatenated format string to 255 chars before writing.","Run the schema validator on formats in your test suite."],"tags":["excel","number-format","validation","ecma-376"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}