{"record":{"id":"38bfe5a8868b0089","repo":"iOfficeAI/OfficeCLI","slug":"cell-value-where-exceeds-excel-s-maxcelltextleng","errorCode":null,"errorMessage":"Cell value{where} exceeds Excel's {MaxCellTextLength}-character limit (got {value.Length})","messagePattern":"Cell value(.+?) exceeds Excel's (.+?)-character limit \\(got (.+?)\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs","lineNumber":560,"sourceCode":"                 System.Text.RegularExpressions.Regex.Matches(scan, @\"(?<![A-Za-z0-9_'.#])([A-Za-z_][A-Za-z0-9_.]*)!\"))\n        {\n            if (!names.Contains(m.Groups[1].Value)) return true;\n        }\n        return false;\n    }\n\n    // R13-1: Excel rejects cell values longer than 32767 chars (2^15 - 1) with\n    // 0x800A03EC on save/open. Reject at write time with a clear error rather\n    // than silently writing a file Excel will refuse to open.\n    internal const int MaxCellTextLength = 32767;\n\n    internal static void EnsureCellValueLength(string? value, string? cellRef = null)\n    {\n        if (value == null) return;\n        if (value.Length > MaxCellTextLength)\n        {\n            var where = string.IsNullOrEmpty(cellRef) ? \"\" : $\" at {cellRef}\";\n            throw new ArgumentException(\n                $\"Cell value{where} exceeds Excel's {MaxCellTextLength}-character limit (got {value.Length})\");\n        }\n        // XML-illegal control chars / lone surrogates: without this the value\n        // enters the in-memory DOM fine and only fails at close-time save —\n        // \"save failed during shutdown\", leaving sheetData empty on disk\n        // (total data loss). Same guard Word text paths already use.\n        OfficeCli.Core.ParseHelpers.ValidateXmlText(value,\n            string.IsNullOrEmpty(cellRef) ? \"cell value\" : $\"cell value at {cellRef}\");\n    }\n\n    // Numeric literal forms Excel's <v> parser accepts. double.TryParse is\n    // far more lenient (leading '+', thousands separators, padding, currency\n    // NumberStyles) — writing such text verbatim into an untyped (numeric)\n    // <v> makes real Excel refuse the file (0x800A03EC) even though\n    // schema validation stays green.\n    private static readonly System.Text.RegularExpressions.Regex CanonicalNumericLiteral =\n        new(@\"^-?(\\d+\\.?\\d*|\\.\\d+)([eE][+-]?\\d+)?$\", System.Text.RegularExpressions.RegexOptions.Compiled);\n","sourceCodeStart":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Helpers.Validation.cs#L542-L578","documentation":"Excel cells hold at most 32767 (2^15 - 1) characters. A longer value causes Excel to fail open/save with 0x800A03EC and, worse, can leave the sheetData empty on disk (total data loss). The library rejects over-length values at write time via EnsureCellValueLength and also validates XML-illegal control chars/lone surrogates.","triggerScenarios":"Writing a string longer than 32767 chars to any cell via a set/value API. The MaxCellTextLength constant (32767) is the ceiling.","commonSituations":"Dumping logs, JSON blobs, stack traces, or full document text into a cell; concatenating many records into one cell; CSV import of an unbounded text field.","solutions":["Truncate the value to 32767 chars before writing.","Store oversized content in a sidecar text file and reference it, or split across multiple cells.","Validate length upstream and surface the limit to the user."],"exampleFix":"// before\nsheet.SetValue(\"A1\", giantJsonBlob);   // 50000 chars\n\n// after\nvar v = giantJsonBlob.Length > 32767 ? giantJsonBlob.Substring(0, 32767) : giantJsonBlob;\nsheet.SetValue(\"A1\", v);","handlingStrategy":"validation","validationCode":"const int MaxCellTextLength = 32767;\nstatic string ClampCellValue(string? v) =>\n    v == null ? string.Empty : (v.Length > MaxCellTextLength ? v.Substring(0, MaxCellTextLength) : v);","typeGuard":null,"tryCatchPattern":"try { sheet.SetValue(cell, value); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"32767-character limit\")) {\n    sheet.SetValue(cell, value.Substring(0, 32767));\n}","preventionTips":["Never dump unbounded blobs (logs, JSON) into a single cell; cap upstream.","Split oversized content across cells or into a sidecar file."],"tags":["excel","cell-value","validation","limits"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}