iOfficeAI/OfficeCLI · error · ArgumentException

calculatedField requires a non-empty name

Error message

calculatedField requires a non-empty name

What it means

Thrown by the calculated-field builder while iterating parsed specs: every spec must carry a non-empty 'name'. It fires after ParseCalculatedFieldSpecs has already accepted the entry, so an empty/whitespace name slipped through one of the input forms (JSON object, or colon-separated bare prop). The guard exists because Excel requires every cacheField to have a non-empty Name attribute and would reject the file on refresh.

Source

Thrown at src/officecli/Core/PivotTableHelper.Definition.cs:1534

        var dataFields = pivotDef.DataFields;
        if (dataFields == null)
        {
            dataFields = new DataFields { Count = 0u };
            pivotDef.DataFields = dataFields;
        }

        // Mirror layout-dependent attributes (compact/outline) from an existing
        // source pivotField so the calc fields stay attribute-consistent with
        // the rest of the table. Excel rejects a pivotTable where some
        // pivotFields declare compact="0" outline="0" but later ones omit them.
        var templatePf = pivotFields.Elements<PivotField>().FirstOrDefault();
        bool templateCompactFalse = templatePf?.Compact?.Value == false;
        bool templateOutlineFalse = templatePf?.Outline?.Value == false;

        foreach (var (name, formula) in specs)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentException("calculatedField requires a non-empty name");
            if (string.IsNullOrWhiteSpace(formula))
                throw new ArgumentException($"calculatedField '{name}' requires a non-empty formula");
            if (existingNames.Contains(name))
                throw new ArgumentException(
                    $"calculatedField '{name}' collides with an existing field name");
            existingNames.Add(name);

            // 1. cacheField
            var cleanFormula = formula.TrimStart('=').Trim();
            var cacheField = new CacheField
            {
                Name = name,
                Formula = cleanFormula,
                DatabaseField = false,
                NumberFormatId = 0u
            };
            cacheFields.AppendChild(cacheField);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Supply a non-empty, non-whitespace name before the colon: calculatedField=Margin:=Sales-Cost
  2. If using JSON, ensure every object's 'name' is a non-empty string: [{"name":"Margin","formula":"=Sales-Cost"}]
  3. Trim and validate names in your own code before passing them to PivotTableHelper

Example fix

// before
calculatedField=":=A1*2"
// after
calculatedField="Margin:=A1*2"
Defensive patterns

Strategy: validation

Validate before calling

// Before building the spec list, drop or reject entries with blank names
var specs = rawSpecs.Where(s => !string.IsNullOrWhiteSpace(s.Name)).ToList();
if (rawSpecs.Count != specs.Count)
    throw new InvalidOperationException("One or more calculated field names are empty");

Type guard

static bool IsValidCalcFieldName(string? name) =>
    !string.IsNullOrWhiteSpace(name) && name.Trim().Length > 0;

Try / catch

try { AddCalculatedFields(props); }
catch (ArgumentException ex) when (ex.Message.Contains("non-empty name"))
{ /* log and prompt user to supply a name */ }

Prevention

When it happens

Trigger: Passing calculatedField=":=A1*2" (bare-prop form with empty left side), or calculatedFields JSON like [{"name":"","formula":"=A1*2"}] (note: JSON parser only filters null, not empty string, so "" reaches the loop), or a name consisting solely of whitespace like calculatedField=" :=A1*2".

Common situations: User typos the colon separator (writes calculatedField==A1*2 thinking '=' starts the formula), malformed template substitution that leaves the name slot blank, or programmatic generation that builds the spec from an unset variable.

Related errors


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