iOfficeAI/OfficeCLI · error · ArgumentException

Invalid gapWidth: '{value}'. Expected integer (0-500).

Error message

Invalid gapWidth: '{value}'. Expected integer (0-500).

What it means

Thrown when 'gapWidth'/'gap' is not a parseable integer (int.TryParse fails). This is the parse-failure path; a separate range check (error 153) handles integers outside 0-500. OOXML CT_GapAmount is ST_GapAmountUShort (0-500); a non-integer string cannot be cast and is rejected up front before any gap element is mutated.

Source

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

                            var sf = new Drawing.SolidFill();
                            sf.AppendChild(BuildChartColorElement(outParts[0]));
                            outline.AppendChild(sf);
                            if (outParts.Length > 2 && !string.IsNullOrEmpty(outParts[2]))
                                outline.AppendChild(new Drawing.PresetDash { Val = ParseDashStyle(outParts[2]) });
                            // Insert ln before effectLst per DrawingML schema order
                            var effLst = spPr.GetFirstChild<Drawing.EffectList>();
                            if (effLst != null) spPr.InsertBefore(outline, effLst);
                            else spPr.AppendChild(outline);
                        }
                    }
                    break;
                }

                case "gapwidth" or "gap":
                {
                    var plotArea2 = chart.GetFirstChild<C.PlotArea>();
                    if (plotArea2 == null) { unsupported.Add(key); break; }
                    if (!int.TryParse(value, out var gw)) throw new ArgumentException($"Invalid gapWidth: '{value}'. Expected integer (0-500).");
                    // BUGFIX (NumericBoundaryScanTests): enforce the stated 0-500
                    // range. CT_GapAmount is ST_GapAmountUShort (0-500); out-of-range
                    // (incl. negatives wrapping via (ushort) cast) produced a
                    // schema-invalid c:gapWidth PowerPoint refuses to open.
                    if (gw < 0 || gw > 500) throw new ArgumentException($"Invalid gapWidth: '{value}'. Expected integer 0-500.");
                    bool gapUpdated = false;
                    foreach (var gapEl in plotArea2.Descendants<C.GapWidth>())
                    {
                        gapEl.Val = (ushort)gw;
                        gapUpdated = true;
                    }
                    if (!gapUpdated)
                    {
                        // No existing GapWidth — create one per bar/column chart element.
                        // This occurs when RebuildComboChart (applied via deferred
                        // comboTypes= prop) replaces the original barChart (which had
                        // a GapWidth seeded by BuildBarChart) with freshly constructed
                        // barChart elements that have no GapWidth child. The `foreach

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Pass gapWidth as a plain integer string (e.g. "150").
  2. Strip '%' and parse to int in your config loader.
  3. Use CultureInfo-invariant parsing if you pre-validate.

Example fix

// before
SetChartProperties(part, new() { ["gapWidth"] = "50%" });
// after
SetChartProperties(part, new() { ["gapWidth"] = "50" });
Defensive patterns

Strategy: validation

Validate before calling

static bool IsIntegerGapWidth(string v, out int gw) =>
    int.TryParse(v?.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out gw);

Type guard

static bool IsPlainInteger(string v) =>
    int.TryParse((v ?? "").Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture, out _);

Try / catch

try { SetChartProperties(part, props); }
catch (ArgumentException ex) when (ex.Message.Contains("gapWidth") && ex.Message.Contains("integer"))
{ /* strip '%', parse to int, retry */ }

Prevention

When it happens

Trigger: SetChartProperties with { ["gapWidth"] = "50%" }, "narrow", "1.5", or any non-integer token.

Common situations: Percentage suffix from a CSS-like config ("20%"); decimal gap from a ratio; localized number with a comma decimal separator.

Related errors


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