iOfficeAI/OfficeCLI · error · ArgumentException

Invalid baseTimeUnit '{value}': expected days, months, or ye

Error message

Invalid baseTimeUnit '{value}': expected days, months, or years.

What it means

The baseTimeUnit setter applies to date axes (C.DateAxis) only. The value is trimmed and lowercased, then matched against days/months/years (with singular aliases day/month/year). Any other value sets btuVal to null and throws. The new C.BaseTimeUnit element is inserted in CT_DateAx schema order (before MajorUnit if present, else appended).

Source

Thrown at src/officecli/Core/Chart/ChartHelper.Axis.cs:560

                    }
                    directlyHandled.Add(key);
                    break;
                }

                case "basetimeunit":
                {
                    // Date-axis base time unit round-trip (days/months/years).
                    if (targetAxis is C.DateAxis daxBtu)
                    {
                        var btuVal = value.Trim().ToLowerInvariant() switch
                        {
                            "days" or "day" => (C.TimeUnitValues?)C.TimeUnitValues.Days,
                            "months" or "month" => C.TimeUnitValues.Months,
                            "years" or "year" => C.TimeUnitValues.Years,
                            _ => null,
                        };
                        if (btuVal == null)
                            throw new ArgumentException($"Invalid baseTimeUnit '{value}': expected days, months, or years.");
                        daxBtu.RemoveAllChildren<C.BaseTimeUnit>();
                        var btuEl = new C.BaseTimeUnit { Val = btuVal.Value };
                        // CT_DateAx: …auto?, lblOffset?, baseTimeUnit?, majorUnit?…
                        var btuBefore = (OpenXmlElement?)daxBtu.GetFirstChild<C.MajorUnit>();
                        if (btuBefore != null) daxBtu.InsertBefore(btuEl, btuBefore);
                        else daxBtu.AppendChild(btuEl);
                    }
                    directlyHandled.Add(key);
                    break;
                }

                case "ticklabelpos":
                case "ticklabelposition":
                {
                    // CONSISTENCY(chart/axis-role-write): legacy SetChartProperties
                    // tickLabelPos sweeps every ValueAxis + CategoryAxis. Role-scoped
                    // write must only mutate the resolved axis. (R43-4)
                    if (targetAxis is OpenXmlCompositeElement axTlp)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use days, months, or years (or singular day, month, year): basetimeunit=months.
  2. For finer granularity than days, note that OOXML does not support it on date axes.
  3. Ensure the target axis is a date axis (C.DateAxis) — this setting has no effect on category or value axes.

Example fix

// before
basetimeunit=weeks
// after
basetimeunit=days
// or
basetimeunit=months
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<string> ValidBaseTimeUnits = new(StringComparer.OrdinalIgnoreCase)
{ "days", "day", "months", "month", "years", "year" };

static bool IsValidBaseTimeUnit(string value)
    => ValidBaseTimeUnits.Contains(value.Trim());

Try / catch

try { axisSetter.Apply("basetimeunit", value); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Invalid baseTimeUnit"))
{
    Console.Error.WriteLine($"{ex.Message}\nValid: days, months, years.");
}

Prevention

When it happens

Trigger: Setting basetimeunit to an unrecognized value on a date axis: 'weeks', 'quarters', 'hours', 'seconds'. The target axis must be a C.DateAxis for this code path; other axis types skip it.

Common situations: Using 'weeks' or 'quarters' — OOXML ST_TimeUnit only supports days, months, years. Using 'h' or 'min' (time-based units not in the schema). Applying to a non-date axis (category or value axis) where it is silently ignored.

Related errors


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