iOfficeAI/OfficeCLI · error · ArgumentException

movingAvg period must be >= 2 (OOXML ST_Skip MinInclusive=2)

Error message

movingAvg period must be >= 2 (OOXML ST_Skip MinInclusive=2). Got: {order}.

What it means

Thrown by BuildTrendline (ChartHelper.SetterHelpers.cs:100) when a moving-average trendline spec (movingAvg:N) carries a period value less than 2. OOXML's ST_Skip type (the c:period element inside c:trendline) has MinInclusive=2, and Word returns error 422 on files that violate it. The pre-fix code silently accepted 0/1, producing corrupt files. When no explicit period is given the code defaults to 2 (Excel's default), so this only fires on an explicit bad value.

Source

Thrown at src/officecli/Core/Chart/ChartHelper.SetterHelpers.cs:100

                $"Invalid trendline type '{parts[0]}'. " +
                "Valid: linear, exp, log, poly, power, movingAvg. " +
                "For per-series different trendlines use seriesN.trendline keys, " +
                "not pipe-separated lists.")
                { Code = "invalid_value" }
        };
        trendline.AppendChild(new C.TrendlineType { Val = trendType });

        // Polynomial order or moving average period
        if (parts.Length > 1 && int.TryParse(parts[1], out var order))
        {
            if (trendType == C.TrendlineValues.Polynomial)
                trendline.AppendChild(new C.PolynomialOrder { Val = (byte)Math.Clamp(order, 2, 6) });
            else if (trendType == C.TrendlineValues.MovingAverage)
            {
                // OOXML ST_Skip MinInclusive=2 (c:period inside c:trendline).
                // Pre-fix code silently accepted order=0/1 which Word 422s on.
                if (order < 2)
                    throw new ArgumentException($"movingAvg period must be >= 2 (OOXML ST_Skip MinInclusive=2). Got: {order}.");
                trendline.AppendChild(new C.Period { Val = (uint)order });
            }
            else
            {
                // Treat as forward extrapolation periods
                trendline.AppendChild(new C.Forward { Val = order });
            }
        }
        // OOXML CT_Trendline requires <c:period> when trendlineType=movingAvg;
        // Word rejects the file otherwise. When no explicit period was given,
        // fall back to 2 (Excel's default for "Add Trendline → Moving Average").
        if (trendType == C.TrendlineValues.MovingAverage
            && trendline.GetFirstChild<C.Period>() == null)
        {
            trendline.AppendChild(new C.Period { Val = 2u });
        }
        // Same family for polynomial: <c:order> is required for poly trendlines.
        // Default to degree 2 (Excel's default for "Add Trendline → Polynomial").

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Set the moving-average period to 2 or higher, e.g. movingAvg:2.
  2. Omit the period entirely to get the Excel default of 2.

Example fix

// before
series1.trendline=movingAvg:1
// after
series1.trendline=movingAvg:3
Defensive patterns

Strategy: validation

Validate before calling

static int ResolveMovingAvgPeriod(string spec)
{
    var parts = spec.Split(':');
    if (parts.Length < 2) return 2; // default
    if (!int.TryParse(parts[1], out var p) || p < 2) throw new ArgumentException("movingAvg period must be >= 2");
    return p;
}

Type guard

static bool IsValidMovingAvgPeriod(string spec)
{
    var parts = spec.Split(':');
    return parts.Length < 2 || (int.TryParse(parts[1], out var p) && p >= 2);
}

Try / catch

try { /* set series1.trendline=movingAvg:N */ }
catch (ArgumentException ex) when (ex.Message.Contains("movingAvg period must be >= 2"))
{ /* coerce N to 2 or prompt user */ }

Prevention

When it happens

Trigger: Setting series1.trendline=movingAvg:0 or movingAvg:1, or passing 'moving:1' / 'movingAverage:0'.

Common situations: Assuming a moving-average period of 1 means 'every point'; passing 0 to mean 'default'; reusing a generic 'order' parameter value across polynomial and moving-average specs without adjusting the minimum.

Related errors


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