iOfficeAI/OfficeCLI · error · ArgumentException

Invalid 'width' value: '{value}'. Expected a finite number (

Error message

Invalid 'width' value: '{value}'. Expected a finite number (column width in char units, e.g. 8.43).

What it means

Thrown by ParseColWidthChars when the bare-number branch parsed successfully but the value is NaN or Infinity. NumberStyles.Float accepts 'NaN'/'Infinity', so this guard rejects those sentinel strings before they would corrupt a column width.

Source

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

        return pts;
    }

    // CONSISTENCY(rc-units): Column width is in "maximum digit width" char
    // units (Calibri 11pt ≈ 7px per char). Accepts bare number (char units,
    // legacy) or unit-qualified px/cm/in/pt — physical sizes converted via
    // the 7-px-per-char approximation Excel uses internally.
    internal static double ParseColWidthChars(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("Column width cannot be empty.");
        var trimmed = value.Trim();
        double chars;
        if (double.TryParse(trimmed, System.Globalization.NumberStyles.Float,
                System.Globalization.CultureInfo.InvariantCulture, out var bare)
            && !char.IsLetter(trimmed[^1]))
        {
            if (double.IsNaN(bare) || double.IsInfinity(bare))
                throw new ArgumentException($"Invalid 'width' value: '{value}'. Expected a finite number (column width in char units, e.g. 8.43).");
            chars = bare;
        }
        else
        {
            try
            {
                var emu = OfficeCli.Core.EmuConverter.ParseEmu(trimmed);
                // 9525 EMU = 1 px; 7 px ≈ 1 char unit (Calibri 11pt MDW baseline)
                var px = emu / EmuConverter.EmuPerPxF;
                chars = px / 7.0;
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Invalid 'width' value: '{value}'. Expected a finite number or unit-qualified value (e.g. 8.43, 20px, 2cm, 1in, 60pt).", ex);
            }
        }
        // DEFERRED(xlsx/row-height-validation) RC2: Excel column width is bounded
        // [0, 255] character units. Validate at Set time.

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Replace the non-finite value with a finite number (column width is in character units, e.g. 8.43).
  2. Fix the upstream arithmetic that produced NaN/Infinity.
  3. Validate with double.IsFinite before constructing the prop value.

Example fix

// before
var w = ratio * base;     // ratio = 0/0 -> NaN
set($"/Sheet1/col[B] --prop width={w}");

// after
var w = double.IsFinite(ratio) ? ratio * base : 8.43;
set($"/Sheet1/col[B] --prop width={w}");
Defensive patterns

Strategy: validation

Validate before calling

if (!double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var d) || !double.IsFinite(d))
    throw new ArgumentException($"Column width must be a finite number, got '{value}'.");

Prevention

When it happens

Trigger: Passing width=NaN, width=Infinity, width=-Infinity, or any non-finite value the parser accepts.

Common situations: Upstream divide-by-zero producing NaN that is then interpolated into the prop; an LLM placeholder; serialization of a default(double) field.

Related errors


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