iOfficeAI/OfficeCLI · error · ArgumentException

Invalid margin value: '{value}' (use 1in, 2cm, 10mm, 72pt, o

Error message

Invalid margin value: '{value}' (use 1in, 2cm, 10mm, 72pt, or bare inches)

What it means

Thrown by ExcelHandler.ParseMarginInches when a print-margin value does not end in a recognized unit suffix (in/cm/mm/pt) and is not parseable as a bare decimal number (inches). It is a guard at the Set/add path before the value is written into a PageMargins element, which itself only stores inches.

Source

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

        }
        if (v.EndsWith("cm"))
        {
            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);
            return num / 2.54;
        }
        if (v.EndsWith("mm"))
        {
            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);
            return num / 25.4;
        }
        if (v.EndsWith("pt"))
        {
            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);
            return num / 72.0;
        }
        // Bare number = inches
        if (!double.TryParse(v, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out num))
            throw new ArgumentException($"Invalid margin value: '{value}' (use 1in, 2cm, 10mm, 72pt, or bare inches)");
        return num;
    }

    // Build an <xdr:pic> element with an initial Transform2D, applying any
    // user-supplied rotation/flip props. Keeps the Add.cs path readable.
    // CONSISTENCY(scheme-color): Map a scheme-color name
    // ("accent1"-"accent6", "lt1"/"dk1", "lt2"/"dk2", "bg1"/"tx1", "bg2"/"tx2",
    // "hlink", "folHlink") to the OOXML theme index used by TabColor.Theme,
    // color.Theme on fonts, etc. Returns null for non-scheme inputs — callers
    // then fall back to srgbClr (hex) handling.
    internal static uint? ExcelSchemeColorNameToThemeIndex(string s) =>
        s?.Trim().ToLowerInvariant() switch
        {
            "lt1" or "bg1" or "light1" or "background1" => 0u,
            "dk1" or "tx1" or "dark1" or "text1" => 1u,
            "lt2" or "bg2" or "light2" or "background2" => 2u,
            "dk2" or "tx2" or "dark2" or "text2" => 3u,
            "accent1" => 4u,

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use one of the supported forms: a bare number (inches), or a value suffixed with 'in', 'cm', 'mm', or 'pt' (e.g. margin.left=0.5in, margin.left=1.27cm, margin.left=10mm, margin.left=36pt).
  2. If you need pixels, convert first: pixels/96 inches (e.g. 96px = 1in) since 'px' is NOT accepted by this margin helper.
  3. Ensure the value uses a dot decimal separator regardless of locale (invariant culture parsing).
  4. Trim stray whitespace and confirm the value is non-empty before the call.

Example fix

// before
set /Sheet1 --prop margin.left=2px
set /Sheet1 --prop margin.left=1,5cm

// after (px is not supported -> convert to inches; use dot decimal)
set /Sheet1 --prop margin.left=0.0208in   // 2px / 96
set /Sheet1 --prop margin.left=1.5cm
Defensive patterns

Strategy: validation

Validate before calling

static readonly Regex MarginRe = new(@"^\s*(\d+(\.\d+)?|\.\d+)\s*(in|cm|mm|pt)?\s*$", RegexOptions.IgnoreCase);
static bool IsValidMargin(string v) => !string.IsNullOrWhiteSpace(v) && MarginRe.IsMatch(v);
// NOTE: 'px','em','rem','%' are NOT accepted by ParseMarginInches.

Try / catch

try { var inches = ExcelHandler.ParseMarginInches(value); }
catch (ArgumentException ex) when (ex.Message.Contains("Invalid margin"))
{ /* surface to user with the accepted-unit list */ }

Prevention

When it happens

Trigger: Calling set on a sheet/workbook path with --prop margin.left= (or top/right/bottom/header/footer) where the value is null, contains an unsupported unit (px, pc, em, %), uses a comma decimal separator, or is otherwise non-numeric (e.g. 'margin.left=', 'margin.left=2px', 'margin.left=1,5cm').

Common situations: Locale pitfalls (a user in a comma-decimal locale types '2,5cm'), AI assistants defaulting to CSS units ('px','em'), trailing whitespace inside an empty string token, and copy-paste of '12.7mm' where 'mm' is supported but a typo like '12.7mn' is not.

Related errors


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