iOfficeAI/OfficeCLI · error · ArgumentException

Invalid horizontal alignment: '{value}'. Valid values: left,

Error message

Invalid horizontal alignment: '{value}'. Valid values: left, center, right, justify.

What it means

Thrown by the Excel horizontal-alignment parser when a value does not match a known token. Note the error message lists only 'left, center, right, justify' as valid, but the switch also accepts general, fill, centeracrossselection/centercontinuous, and distributed — so the message is narrower than the actual accepted set.

Source

Thrown at src/officecli/Core/ExcelStyleManager.cs:1811

    private static bool IsTruthy(string? value) =>
        ParseHelpers.IsTruthy(value);

    private static bool IsValidBooleanString(string? value) =>
        ParseHelpers.IsValidBooleanString(value);

    private static HorizontalAlignmentValues ParseHAlign(string value) =>
        value.ToLowerInvariant() switch
        {
            "left" => HorizontalAlignmentValues.Left,
            "center" => HorizontalAlignmentValues.Center,
            "right" => HorizontalAlignmentValues.Right,
            "justify" => HorizontalAlignmentValues.Justify,
            "general" => HorizontalAlignmentValues.General,
            "fill" => HorizontalAlignmentValues.Fill,
            "centeracrossselection" or "centercontinuous" => HorizontalAlignmentValues.CenterContinuous,
            "distributed" => HorizontalAlignmentValues.Distributed,
            _ => throw new ArgumentException($"Invalid horizontal alignment: '{value}'. Valid values: left, center, right, justify.")
        };

    private static VerticalAlignmentValues ParseVAlign(string value) =>
        value.ToLowerInvariant() switch
        {
            "top" => VerticalAlignmentValues.Top,
            "center" => VerticalAlignmentValues.Center,
            "bottom" => VerticalAlignmentValues.Bottom,
            "justify" => VerticalAlignmentValues.Justify,
            "distributed" => VerticalAlignmentValues.Distributed,
            _ => throw new ArgumentException($"Invalid vertical alignment: '{value}'. Valid values: top, center, bottom.")
        };
}

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use one of the actually-supported values: left, center, right, justify, general, fill, centeracrossselection (or centercontinuous), distributed.
  2. Trim and lowercase the input before passing.
  3. If you hit this with a value the message says is invalid but you expect to work, try the extended set (general/fill/distributed/centeracrossselection).

Example fix

// before
align["horizontal"] = "centre"; // British spelling not accepted
// after
align["horizontal"] = "center";
Defensive patterns

Strategy: type-guard

Validate before calling

static readonly HashSet<string> ValidHAlign = new(StringComparer.OrdinalIgnoreCase)
{ "left","center","right","justify","general","fill","centeracrossselection","centercontinuous","distributed" };
static string NormalizeHAlign(string v) => ValidHAlign.Contains(v?.Trim() ?? "") ? v.Trim().ToLowerInvariant() : throw new ArgumentException("invalid halign");

Type guard

static bool IsValidHAlign(string v) => ValidHAlign.Contains(v?.Trim() ?? "");

Try / catch

try { SetAlign(horizontal: value); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Invalid horizontal alignment"))
{ /* fallback to 'general' */ }

Prevention

When it happens

Trigger: Passing alignment/horizontal= with a value outside the switch arms. The default arm throws listing only the four most common values, which can be misleading since the parser actually accepts more.

Common situations: Typo such as 'centre' (British) or 'left '; a value the app thinks is valid but the parser's listed options don't include (e.g. 'distributed'); whitespace not trimmed.

Related errors


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