iOfficeAI/OfficeCLI · error · ArgumentException

Invalid style: '{value}'. Valid range is 1-48.

Error message

Invalid style: '{value}'. Valid range is 1-48.

What it means

Thrown when 'style'/'styleId' parses to an integer outside 1-48 (the OOXML chart style gallery range, CT_Style/ST_StyleUByte). NOTE an atomicity caveat: chartSpace.RemoveAllChildren<C.Style>() runs BEFORE the range check, so an out-of-range value deletes the existing style and then throws, leaving the chart without a style element. SafeParseInt rejects non-integer input earlier.

Source

Thrown at src/officecli/Core/Chart/ChartHelper.Setter.cs:1322

                        if (ser is not (C.LineChartSeries or C.ScatterChartSeries or C.RadarChartSeries))
                            continue;
                        // Reuse the per-series dotted-property handler so
                        // symbol/size are preserved and schema-order insertion
                        // stays in one place.
                        HandleSeriesDottedProperty(ser, "markercolor", value);
                    }
                    break;
                }

                // ---- #4 Chart style ID ----
                case "style" or "styleid":
                {
                    chartSpace!.RemoveAllChildren<C.Style>();
                    if (!value.Equals("none", StringComparison.OrdinalIgnoreCase))
                    {
                        var styleVal = ParseHelpers.SafeParseInt(value, "style");
                        if (styleVal < 1 || styleVal > 48)
                            throw new ArgumentException($"Invalid style: '{value}'. Valid range is 1-48.");
                        chartSpace.InsertBefore(new C.Style { Val = (byte)styleVal }, chart);
                    }
                    break;
                }

                // ---- #5 Fill transparency ----
                case "transparency" or "opacity" or "alpha":
                {
                    var plotArea2 = chart.GetFirstChild<C.PlotArea>();
                    if (plotArea2 == null) { unsupported.Add(key); break; }
                    var alphaPercent = ParseHelpers.SafeParseDouble(value, key);
                    // BUGFIX (NumericBoundaryScanTests): transparency/opacity/alpha
                    // are 0-100 percent. Out-of-range input drove the computed
                    // <a:alpha val> outside [0,100000] → schema-invalid file.
                    if (double.IsNaN(alphaPercent) || double.IsInfinity(alphaPercent) || alphaPercent < 0 || alphaPercent > 100)
                        throw new ArgumentException($"Invalid {key}: '{value}'. Expected a percentage 0-100.");
                    // If key is "transparency", convert to opacity (e.g. 30% transparency = 70% opacity)
                    if (key.Equals("transparency", StringComparison.OrdinalIgnoreCase))

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Clamp style to the 1-48 range before passing (or pass 'none' to remove it).
  2. Validate the integer bounds in your own config loader.
  3. If relying on the current style, do not send an out-of-range value — it will be wiped on the throw.

Example fix

// before
SetChartProperties(part, new() { ["style"] = "0" });
// after
var style = Math.Clamp(userStyle, 1, 48);
SetChartProperties(part, new() { ["style"] = style.ToString() });
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidChartStyle(string v, out int style) =>
    (v.Equals("none", StringComparison.OrdinalIgnoreCase))
    || (int.TryParse(v, out style) && style >= 1 && style <= 48);

Type guard

static bool IsLegalStyleId(string v) =>
    v.Equals("none", StringComparison.OrdinalIgnoreCase)
    || (int.TryParse(v, out var s) && s is >= 1 and <= 48);

Try / catch

try { SetChartProperties(part, props); }
catch (ArgumentException ex) when (ex.Message.Contains("Valid range is 1-48"))
{ /* NOTE: the prior style was already deleted — re-apply a valid style */ }

Prevention

When it happens

Trigger: SetChartProperties with { ["style"] = "0" }, "50", "99", or "100". The value 'none' is special-cased to delete the style without throwing.

Common situations: UI style picker with no bounds; config referencing a 1-indexed style by a 0-indexed value; copy from a theme that exposes >48 styles.

Related errors


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