iOfficeAI/OfficeCLI · error · ArgumentException

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

Error message

Invalid 'width' value: '{value}'. Expected a finite number or unit-qualified value (e.g. 8.43, 20px, 2cm, 1in, 60pt).

What it means

Thrown by ParseColWidthChars when the input is unit-qualified (last char is a letter) but EmuConverter.ParseEmu rejected it. The inner exception carries the precise reason (unknown unit, bad numeric prefix, font-context unit like 'em').

Source

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

                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.
        if (chars < 0 || chars > 255)
            throw new ArgumentException($"Invalid 'width' value: '{value}'. Column width must be between 0 and 255 character units.");
        return chars;
    }

    // Returns true if `s` would parse as a valid cell reference (e.g. A1,
    // TBL1, XFD1048576). Excel refuses to open files whose table names match
    // this pattern — the name is ambiguous with a cell address.
    internal static bool LooksLikeCellReference(string? s)
    {
        if (string.IsNullOrEmpty(s)) return false;
        var m = System.Text.RegularExpressions.Regex.Match(s, @"^\$?([A-Za-z]{1,3})\$?([0-9]+)$");
        if (!m.Success) return false;
        var col = m.Groups[1].Value.ToUpperInvariant();

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a supported unit for column width: px, cm, in, pt, mm (e.g. width=20px, width=2cm).
  2. Inspect the inner exception for the precise reason and fix the prefix or unit.
  3. Use a dot decimal separator in the numeric prefix.

Example fix

// before
set /Sheet1/col[B] --prop width=2em

// after (no font context -> convert em yourself, e.g. 2em at Calibri 11pt ≈ 14pt)
set /Sheet1/col[B] --prop width=14pt
Defensive patterns

Strategy: try-catch

Validate before calling

static readonly Regex ColWidthRe = new(@"^\s*-?\d+(\.\d+)?\s*(px|cm|mm|in|pt)?\s*$", RegexOptions.IgnoreCase);
static bool IsValidColWidth(string v) => !string.IsNullOrWhiteSpace(v) && ColWidthRe.IsMatch(v);

Try / catch

try { var chars = ExcelHandler.ParseColWidthChars(value); }
catch (ArgumentException ex) when (ex.Message.Contains("'width'"))
{ /* inspect ex.InnerException for the EmuConverter reason */ }

Prevention

When it happens

Trigger: Passing width=2em, width=20ft, width=abc px, or any unit-qualified value whose prefix or suffix EmuConverter refuses.

Common situations: AI assistants defaulting to 'em'/'rem'; typoed unit suffixes; locale comma decimals in the numeric prefix; passing '%' which is unsupported.

Related errors


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