iOfficeAI/OfficeCLI · error · ArgumentException

Expected an integer cell index or a unit-qualified offset (e

Error message

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

What it means

Thrown by ParseAnchorOriginCell when the value is neither a plain integer nor a unit-qualified offset parseable by EmuConverter.ParseEmu. This cell-origin parser accepts a non-negative cell index ('0','5') or a unit-qualified offset ('1in','2cm','72pt') converted to an approximate cell count. Any other syntax is invalid.

Source

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

            // by definition; if a user passes a number above the column
            // max, it's either a typo or an EMU value mistakenly fed
            // without a unit suffix. Either way, refuse rather than silently
            // remap. CONSISTENCY with R30-1 negative guard.
            if (plainInt > MaxCellIndex - 1)
                throw new ArgumentException(
                    $"Picture/shape {name} column/row index must be in [0, {MaxCellIndex - 1}] (got '{value}'). For EMU-scale offsets use a unit-qualified value like '1in' / '6cm' / '72pt'.");
            return (int)plainInt;
        }

        // Unit-qualified ("1in", "2cm") → EMU → cell count via the same per-cell constants.
        long emu;
        try
        {
            emu = OfficeCli.Core.EmuConverter.ParseEmu(value);
        }
        catch
        {
            throw new ArgumentException($"Expected an integer cell index or a unit-qualified offset (e.g. '1in', '2cm') for {name}, got '{value}'.");
        }
        if (emu < 0)
            throw new ArgumentException($"Picture/shape {name} must be non-negative (got '{value}').");
        long perCellOut = (name == "y") ? EmuPerRowApprox : EmuPerColApprox;
        return (int)(emu / perCellOut);
    }
}

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Pass a bare non-negative integer cell index: x=2.
  2. Or pass a unit-qualified offset: x='1in', x='2cm'.
  3. For cell-reference anchoring use anchor='B2' instead of x/y.
  4. Avoid unsupported units (% em ex rem vw vh) and check suffix spelling.

Example fix

// before
shape x=abc
// after
shape x=2
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { ParseAnchorOriginCell(value, "x"); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected an integer cell index"))
{
    // not a cell index or unit-qualified offset; surface a user error
}

Prevention

When it happens

Trigger: Passing x='abc', x='B2' (cell references belong on anchor=, not x/y), x='50%', or x='5em'. The inner EmuConverter exception is wrapped and re-thrown with this clearer message.

Common situations: Confusing the x/y origin property with anchor=; using percentage or font-relative units; typos in unit suffixes; expecting percentage-based positioning.

Related errors


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