iOfficeAI/OfficeCLI · error · ArgumentException

Invalid '{propertyName}' value '{value}'. Expected an intege

Error message

Invalid '{propertyName}' value '{value}'. Expected an integer.

What it means

Thrown by SafeParseInt when the string cannot be parsed as an int (invariant culture). It is a thin wrapper over int.TryParse that converts the silent failure into an ArgumentException naming the property and offending value. Used for integer-valued OOXML attributes supplied as strings.

Source

Thrown at src/officecli/Core/ParseHelpers.cs:421

        if (string.IsNullOrWhiteSpace(raw)) return null;
        raw = raw.Trim();
        if (int.TryParse(raw, NumberStyles.Integer, CultureInfo.InvariantCulture, out var i))
            return i;
        // Accept a decimal string ("0.0", "9440.0", "12.5") and truncate.
        if (double.TryParse(raw, NumberStyles.Float, CultureInfo.InvariantCulture, out var d)
            && !double.IsNaN(d) && !double.IsInfinity(d)
            && d >= int.MinValue && d <= int.MaxValue)
            return (int)d;
        return null;
    }

    /// <summary>
    /// Safely parse a string as int, throwing ArgumentException with a clear message on failure.
    /// </summary>
    public static int SafeParseInt(string value, string propertyName)
    {
        if (!int.TryParse(value, CultureInfo.InvariantCulture, out var result))
            throw new ArgumentException($"Invalid '{propertyName}' value '{value}'. Expected an integer.");
        return result;
    }

    /// <summary>
    /// Parse a "start:end" character-range spec into 0-based, half-open offsets.
    /// Colon separator mirrors the officecli range convention (Excel A1:B2).
    /// Shared by the pptx and docx run-range formatting paths so the two never
    /// diverge (CONSISTENCY(char-range)).
    /// </summary>
    public static (int Start, int End) ParseCharRange(string spec)
    {
        var parts = spec.Split(':');
        if (parts.Length != 2
            || !int.TryParse(parts[0].Trim(), CultureInfo.InvariantCulture, out var start)
            || !int.TryParse(parts[1].Trim(), CultureInfo.InvariantCulture, out var end))
            throw new ArgumentException(
                $"Invalid range '{spec}'. Expected 'start:end' with 0-based integer " +
                "character offsets (e.g. '6:11').");

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Pass a whole-number string with no separators (e.g. "100").
  2. If you have a double, truncate first: ((int)Math.Round(d)).ToString(CultureInfo.InvariantCulture).
  3. Validate with int.TryParse before the call if unknown should not throw.

Example fix

// before
SafeParseInt(value.ToString(), "indent"); // value == 1.5 -> "1.5"

// after
var i = (int)Math.Round(value);
SafeParseInt(i.ToString(CultureInfo.InvariantCulture), "indent");
Defensive patterns

Strategy: validation

Validate before calling

if (!int.TryParse(s, CultureInfo.InvariantCulture, out _))
    s = ((int)Math.Round(double.Parse(s, CultureInfo.InvariantCulture))).ToString(CultureInfo.InvariantCulture);

Type guard

static bool IsIntString(string s) => int.TryParse(s, CultureInfo.InvariantCulture, out _);

Try / catch

try { n = SafeParseInt(s, name); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected an integer"))
{ n = 0; }

Prevention

When it happens

Trigger: Passing "1.5", "abc", "", or "1_000" to a property that expects an integer. A float value stringified where an int was required.

Common situations: A computed value that became fractional and was stringified; locale formatting inserting a comma; a JSON number parsed as a string with a decimal point.

Related errors


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