iOfficeAI/OfficeCLI · error · ArgumentException

Invalid labelPos '{value}': expected one of ctr, inBase, inE

Error message

Invalid labelPos '{value}': expected one of ctr, inBase, inEnd, outEnd, t, b, l, r, bestFit.

What it means

Thrown when 'labelPos' on a non-pie chart does not match any switch arm of the position parser. The schema enum ST_DLblPos is {ctr, inBase, inEnd, outEnd, t, b, l, r, bestFit} plus long-form aliases (center, insideEnd, etc.). Anything else is treated as a typo or fuzz garbage and rejected rather than silently mapped to a default.

Source

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

                            ? C.DataLabelPositionValues.Center
                            : C.DataLabelPositionValues.BestFit,
                        "top" or "t" => isStacked
                            ? C.DataLabelPositionValues.InsideEnd
                            : C.DataLabelPositionValues.Top,
                        "bottom" or "b" => isStacked
                            ? C.DataLabelPositionValues.InsideBase
                            : C.DataLabelPositionValues.Bottom,
                        "left" or "l" => isStacked
                            ? C.DataLabelPositionValues.InsideEnd
                            : C.DataLabelPositionValues.Left,
                        "right" or "r" => isStacked
                            ? C.DataLabelPositionValues.InsideEnd
                            : C.DataLabelPositionValues.Right,
                        // Schema enum: {ctr, inBase, inEnd, outEnd, t, b, l, r, bestFit}
                        // plus the long-form aliases handled above. Anything else
                        // is a typo or fuzz garbage — reject up front rather than
                        // silently map to BestFit/OutsideEnd and bury the bug.
                        _ => throw new ArgumentException(
                            $"Invalid labelPos '{value}': expected one of ctr, inBase, inEnd, outEnd, t, b, l, r, bestFit.")
                    };
                    var existingLabels = plotArea2.Descendants<C.DataLabels>().ToList();
                    if (existingLabels.Count == 0)
                    {
                        // Bootstrap charts often lack a c:dLbls element entirely.
                        // Without one, labelPos has nowhere to land and Get sees
                        // nothing — schema declares labelPos get:true so we must
                        // materialize the parent. Attach to the first chart-group
                        // (barChart/lineChart/pieChart/scatterChart/etc.).
                        var chartGroup = plotArea2.ChildElements.OfType<OpenXmlCompositeElement>()
                            .FirstOrDefault(e => e is C.BarChart or C.Bar3DChart
                                or C.LineChart or C.Line3DChart or C.PieChart or C.Pie3DChart
                                or C.ScatterChart or C.BubbleChart);
                        if (chartGroup != null)
                        {
                            var dLbls = new C.DataLabels();
                            // c:dLbls schema requires showLegendKey..showBubbleSize

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use one of the accepted spellings: ctr/center, inBase/insideBase/base, inEnd/insideEnd/inside, outEnd/outsideEnd/outside, t/top, b/bottom, l/left, r/right, bestFit/best/auto.
  2. Trim and ToLowerInvariant the user input before passing it in.
  3. Whitelist labelPos against a known set before calling SetChartProperties.

Example fix

// before
SetChartProperties(part, new() { ["labelPos"] = "outer" });
// after
var allowed = new[]{"ctr","inbase","inend","outend","t","b","l","r","bestfit"};
var pos = userInput.Trim().ToLowerInvariant();
if (!allowed.Contains(pos)) throw new ArgumentOutOfRangeException(nameof(pos));
SetChartProperties(part, new() { ["labelPos"] = pos });
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> LabelPos =
    new[]{"center","ctr","insidebase","inbase","base","insideend","inend","inside",
          "outsideend","outend","outside","top","t","bottom","b","left","l",
          "right","r","bestfit","best","auto"}, StringComparer.OrdinalIgnoreCase);

static bool IsValidLabelPos(string v) =>
    !string.IsNullOrWhiteSpace(v) && LabelPos.Contains(v.Trim().ToLowerInvariant());

Type guard

static bool IsKnownLabelPos(string v) =>
    LabelPos.Contains((v ?? "").Trim().ToLowerInvariant());

Try / catch

try { SetChartProperties(part, props); }
catch (ArgumentException ex) when (ex.Message.Contains("Invalid labelPos"))
{ /* log the offending value and the accepted alias list */ }

Prevention

When it happens

Trigger: SetChartProperties with { ["labelPos"] = "outer" }, "middel", "insideEnd " (trailing space is not trimmed for this key), or any token not in the accepted alias set.

Common situations: Free-text label position from a UI textbox; whitespace/case mismatch (only lowercase is matched); values copied from a non-Excel charting library (e.g. 'outer', 'middle').

Related errors


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