iOfficeAI/OfficeCLI · error · ArgumentException

Invalid array constant: '{badElem}'. Inline arrays {...} may

Error message

Invalid array constant: '{badElem}'. Inline arrays {...} may contain only literal values (numbers, "text", TRUE/FALSE, errors) — not functions, cell references, or nested arrays. Reference a cell range instead (e.g. A1:A3).

What it means

Thrown by ModernFunctionQualifier.Qualify when an Excel inline array constant {...} contains a non-literal element. Excel inline arrays may hold only literal values (numbers, quoted text, TRUE/FALSE, errors); a function call, cell reference, name, or nested array inside {...} makes Excel report the whole workbook as corrupt. Qualify rejects it at write time.

Source

Thrown at src/officecli/Core/Formula/ModernFunctionQualifier.cs:300

    /// <summary>
    /// Returns the formula with Excel 2016+ modern function names qualified
    /// with <c>_xlfn.</c> / <c>_xlfn._xlws.</c> as required by OOXML. Leaves
    /// already-qualified names, older functions, quoted string literals, and
    /// non-function identifiers untouched.
    /// </summary>
    public static string Qualify(string formula)
    {
        if (string.IsNullOrEmpty(formula)) return formula;

        // Excel inline array constants {…} may hold only literal values.
        // A function call, cell reference, name, or nested array inside {…} makes
        // Excel report the whole workbook as corrupt (it prompts to repair on
        // open). Reject such a formula at write time so officecli never emits a
        // file Excel refuses to open.
        var badElem = FindNonLiteralArrayConstant(formula);
        if (badElem != null)
            throw new ArgumentException(
                $"Invalid array constant: '{badElem}'. Inline arrays {{...}} may contain only literal " +
                "values (numbers, \"text\", TRUE/FALSE, errors) — not functions, cell references, or " +
                "nested arrays. Reference a cell range instead (e.g. A1:A3).");

        // LET / LAMBDA parameter names are stored in the _xlpm. namespace; without
        // that prefix Excel reports the workbook as corrupt. Collect every such
        // name up front so all of its occurrences (declaration and uses) get the
        // prefix below.
        var lambdaParams = CollectLambdaParams(formula);

        // Walk the string and only rewrite identifiers outside quoted strings.
        // Excel formula strings are bounded by '"' with '""' as an escape.
        var sb = new System.Text.StringBuilder(formula.Length + 32);
        int i = 0;
        while (i < formula.Length)
        {
            char c = formula[i];
            if (c == '"')

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Replace references/functions inside {...} with literal values, or use a cell range (e.g. A1:A3) instead of an inline array.
  2. Move computed values into cells and reference the range.
  3. Ensure array constants contain only numbers, "text", TRUE/FALSE, or error literals.

Example fix

// before
formula = "={SUM(A1:A3),1}"; // function inside array
// after
formula = "=A1:A3"; // reference the range directly
Defensive patterns

Strategy: validation

Validate before calling

// Only literals allowed inside inline array constants.
static bool IsSafeInlineArray(string formula)
    => ModernFunctionQualifier.FindNonLiteralArrayConstant(formula) is null;

Try / catch

try { ModernFunctionQualifier.Qualify(formula); }
catch (ArgumentException ex) when (ex.Message.Contains("Invalid array constant"))
{ /* switch to a cell range */ }

Prevention

When it happens

Trigger: Writing a formula containing an inline array with a non-literal element, e.g. ={SUM(A1:A3),1} or ={A1,B1}. FindNonLiteralArrayConstant detects the offending element and Qualify throws before the formula is written.

Common situations: Building an array literal from cell references instead of a range; putting a function call inside {...}; confusing Excel array constants with a general list syntax.

Related errors


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