iOfficeAI/OfficeCLI · error · ArgumentException

Invalid margin: empty value.

Error message

Invalid margin: empty value.

What it means

A print-margin value passed to ParseMarginInches was null, empty, or whitespace. PageMargins are stored in inches in the OOXML schema, so the parser needs a non-empty value to convert (it accepts '1in', '2.5cm', '72pt', '10mm', or a bare number in inches).

Source

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

            if (r1 > r2) (r1, r2) = (r2, r1);
            if (r2 - r1 + 1 > 1024)
                throw new ArgumentException(
                    $"Row span {cellRef} covers {r2 - r1 + 1} rows (limit 1024). Narrow the span or address rows individually as row[N].");
            var rows = new List<string>();
            for (var i = r1; i <= r2; i++) rows.Add($"row[{i}]");
            return rows;
        }
        return null;
    }

    /// <summary>
    /// Parse a print-margin value into inches (PageMargins schema unit).
    /// Accepts "1in", "2.5cm", "1.27cm", "72pt", "10mm", or a bare number (inches).
    /// </summary>
    internal static double ParseMarginInches(string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("Invalid margin: empty value.");
        var v = value.Trim().ToLowerInvariant();
        double num;
        if (v.EndsWith("in"))
        {
            num = double.Parse(v[..^2].Trim(), System.Globalization.CultureInfo.InvariantCulture);
            return num;
        }
        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"))

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Provide a value: a bare number (inches) or a unit-suffixed string ('1in', '2.5cm', '72pt', '10mm').
  2. Skip the setter entirely when the margin field is unset, so Excel's default applies.
  3. Validate non-empty at the config boundary.

Example fix

// before
ws.PageMargins.Left = ParseMarginInches(marginCfg.Left);   // marginCfg.Left == ""

// after
if (!string.IsNullOrWhiteSpace(marginCfg.Left))
    ws.PageMargins.Left = ParseMarginInches(marginCfg.Left);
Defensive patterns

Strategy: validation

Validate before calling

static double? TryParseMarginInches(string? value) =>
    string.IsNullOrWhiteSpace(value) ? (double?)null : ParseMarginInches(value);

Try / catch

try { ws.PageMargins.Left = ParseMarginInches(raw); }
catch (ArgumentException ex) when (ex.Message.Contains("empty value")) {
    // leave Excel's default margin in place
}

Prevention

When it happens

Trigger: Calling a page-margin setter with string.Empty, " ", or null. The string.IsNullOrWhiteSpace guard throws before any unit parsing.

Common situations: An optional config field omitted but still forwarded to the setter; a profile/template with a blank margin; UI field left empty and not filtered.

Related errors


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