iOfficeAI/OfficeCLI · error · ArgumentException

Expected an integer cell count or a unit-qualified size (e.g

Error message

Expected an integer cell count or a unit-qualified size (e.g. '6cm', '2in') for {name}, got '{value}'.

What it means

Thrown by ParseAnchorDimension when the value is neither a plain integer nor a unit-qualified size parseable by EmuConverter.ParseEmu. ParseAnchorDimension accepts an integer cell count ('3','5') or a unit-qualified size ('6cm','2in','72pt') converted to an approximate cell count. Any other syntax is invalid.

Source

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

        if (int.TryParse(value, out var plainInt))
        {
            // R30-1: negative cell-count is meaningless and silently
            // produced an invalid file. Reject up front. CONSISTENCY with
            // ParseAnchorDimensionEmu's negative-int guard.
            if (plainInt <= 0)
                throw new ArgumentException($"Picture/shape {name} must be positive (got '{value}').");
            return plainInt;
        }

        // Not a plain integer — treat as EMU-convertible size string.
        long emu;
        try
        {
            emu = OfficeCli.Core.EmuConverter.ParseEmu(value);
        }
        catch
        {
            throw new ArgumentException($"Expected an integer cell count or a unit-qualified size (e.g. '6cm', '2in') for {name}, got '{value}'.");
        }
        // R30-1: unit-qualified negative ("-2in") parses to a negative
        // EMU; reject so the shape branch matches picture behavior.
        if (emu <= 0)
            throw new ArgumentException($"Picture/shape {name} must be positive (got '{value}').");

        // Rough conversion: 1 default Excel column ≈ 64px ≈ 0.677cm ≈ 609600 EMU.
        // 1 default Excel row    ≈ 15pt ≈ 0.529cm ≈ 190500 EMU.
        // For width/height passed as a unit, choose the larger of the two
        // converters so "6cm" yields a sensible ~9 columns result either axis.
        const long emuPerColApprox = 609600;
        const long emuPerRowApprox = 190500;
        if (name == "height")
            return Math.Max(1, (int)(emu / emuPerRowApprox));
        return Math.Max(1, (int)(emu / emuPerColApprox));
    }

    // CONSISTENCY(ole-width-units): OLE round-trip preserves sub-cell precision

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Pass a positive integer cell count: width=3.
  2. Or pass a unit-qualified size with a supported unit: width='2in', width='6cm', width='72pt'.
  3. To size by cell range, use anchor='B2:D2' on a twoCell anchor instead of width/height.
  4. Avoid unsupported units (% em ex rem vw vh) and check for suffix typos.

Example fix

// before
shape width=50%
// after
shape width=2in
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidAnchorDimension(string value)
    => int.TryParse(value, out _)
       || OfficeCli.Core.EmuConverter.TryParseEmu(value, out _);

Type guard

static bool IsAnchorDimensionValue(string s)
    => int.TryParse(s, out _) || OfficeCli.Core.EmuConverter.TryParseEmu(s, out _);

Try / catch

try { ParseAnchorDimension(value, "width"); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected an integer cell count"))
{
    // not a cell count or unit-qualified size; surface a user error
}

Prevention

When it happens

Trigger: Passing width='abc', width='50%', width='3cols', width='B2:D2' (cell ranges belong on anchor=, not width), or width='5em' (unsupported font-relative unit). EmuConverter's inner exception is wrapped and re-thrown with this message.

Common situations: Passing a cell-range where a dimension is expected; using percentage or font-relative units; typos in unit suffixes; mixing the anchor= and width/height grammars.

Related errors


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