iOfficeAI/OfficeCLI · error · ArgumentException

{context} is {content.Length} characters; Excel's limit is {

Error message

{context} is {content.Length} characters; Excel's limit is {MaxFormulaLength} per formula. A longer expression makes Excel refuse to open the file.

What it means

Thrown by ValidateFormulaLength when a formula's content (after stripping a leading '=') exceeds 8192 characters. Excel's hard ceiling on formula length is 8192; longer content is silently persisted but makes real Excel refuse to open the file (0x800A03EC). Shared by cell formulas, defined-name refersTo, and CF expressions so the limit is enforced identically everywhere.

Source

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

        }
    }

    // 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>
    /// Reject a formula whose length exceeds Excel's 8192-character ceiling.
    /// Shared by cell formulas, defined-name refs, and conditional-format
    /// expressions so the limit is enforced identically everywhere.
    /// </summary>
    internal static void ValidateFormulaLength(string? formula, string context = "formula")
    {
        if (formula == null) return;
        var content = formula.TrimStart('=');
        if (content.Length > MaxFormulaLength)
            throw new ArgumentException(
                $"{context} is {content.Length} characters; Excel's limit is {MaxFormulaLength} per formula. " +
                "A longer expression makes Excel refuse to open the file.");
    }

    internal static void ValidateFormulaCellRefs(string formula)
    {
        if (string.IsNullOrEmpty(formula)) return;
        var trimmed = formula.TrimStart('=');
        var stripped = StripFormulaStringLiterals(trimmed);

        // Formula-length ceiling (8192) — checked before the ref scan so an
        // oversized expression fails with a clear message instead of Excel's
        // 0x800A03EC after the fact.
        ValidateFormulaLength(formula);
        // R1C1-style references make real Excel refuse the file.
        ValidateNoR1C1Reference(formula);
        // Excel caps a function call at 255 arguments; a 256-arg call passes
        // schema validation but makes real Excel refuse the file (0x800A03EC).

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Move part of the logic into a helper cell or defined name and reference it.
  2. Use range references and array operations instead of expanding data inline.
  3. If unavoidable, split across multiple cells.

Example fix

// before
string formula = "=IF(A1=1,\"x\",IF(A1=2,\"y\",...))"; // > 8192 chars
// after
// Put the lookup table in a range and use VLOOKUP/XLOOKUP
string formula = "=XLOOKUP(A1,Keys,Values)";
Defensive patterns

Strategy: validation

Validate before calling

const int MaxFormulaLength = 8192;
static bool FormulaWithinLimit(string formula)
    => formula == null || formula.TrimStart('=').Length <= MaxFormulaLength;

Type guard

null

Try / catch

try { /* set formula */ }
catch (ArgumentException ex) when (ex.Message.Contains("Excel's limit is") && ex.Message.Contains("per formula"))
{ /* refactor into helper cells / ranges; do not truncate the formula */ }

Prevention

When it happens

Trigger: Writing any formula-bearing value (cell formula, defined name, CF expression) longer than 8192 chars after the '=' is stripped. Common with generated formulas, deeply nested IFS, or huge CONCATENATE chains.

Common situations: Programmatically building large formulas from data; nested IFS/SWITCH with many branches; CONCATENATE of many strings; copy-pasting a huge expression.

Related errors


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