iOfficeAI/OfficeCLI · error · System.ArgumentException

Unknown dataBar direction '{dbDir}'. Valid: leftToRight, rig

Error message

Unknown dataBar direction '{dbDir}'. Valid: leftToRight, rightToLeft, context.

What it means

Thrown by AddDataBar when the optional 'direction' property is supplied on a dataBar conditional format. Direction sets whether bars grow left-to-right, right-to-left, or per-RTL context of the cell. The raw value is run through SchemaKeyNormalizer.Normalize (strips punctuation/casing) before matching, so the comparison keys are the normalized forms 'lefttoright', 'righttoleft', 'context'. An unknown value is rejected because the X14.DataBarDirectionValues enum has only those three members.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Cf.cs:237

        if (properties.TryGetValue("maxLength", out var dbMaxLenStr)
            && uint.TryParse(dbMaxLenStr, out var dbMaxLenParsed))
            dbMaxLength = dbMaxLenParsed;

        var x14DataBar = new X14.DataBar
        {
            MinLength = dbMinLength,
            MaxLength = dbMaxLength,
            AxisPosition = dbAxisPosVal
        };
        if (properties.TryGetValue("direction", out var dbDir))
        {
            var dirNorm = SchemaKeyNormalizer.Normalize(dbDir);
            x14DataBar.Direction = dirNorm switch
            {
                "lefttoright" or "ltr" => X14.DataBarDirectionValues.LeftToRight,
                "righttoleft" or "rtl" => X14.DataBarDirectionValues.RightToLeft,
                "context" => X14.DataBarDirectionValues.Context,
                _ => throw new ArgumentException(
                    $"Unknown dataBar direction '{dbDir}'. Valid: leftToRight, rightToLeft, context.")
            };
        }
        var x14MinCfvo = new X14.ConditionalFormattingValueObject
        {
            Type = minVal != null
                ? X14.ConditionalFormattingValueObjectTypeValues.Numeric
                : X14.ConditionalFormattingValueObjectTypeValues.AutoMin
        };
        if (minVal != null) x14MinCfvo.Append(new DocumentFormat.OpenXml.Office.Excel.Formula(minVal));
        x14DataBar.Append(x14MinCfvo);
        var x14MaxCfvo = new X14.ConditionalFormattingValueObject
        {
            Type = maxVal != null
                ? X14.ConditionalFormattingValueObjectTypeValues.Numeric
                : X14.ConditionalFormattingValueObjectTypeValues.AutoMax
        };
        if (maxVal != null) x14MaxCfvo.Append(new DocumentFormat.OpenXml.Office.Excel.Formula(maxVal));

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Set direction to leftToRight (alias ltr), rightToLeft (alias rtl), or context.
  2. Omit the direction property if you want Excel's default (context).

Example fix

// before: direction=reverse
add /Sheet1/A1:A10 cf type=databar direction=reverse
// after: direction=rightToLeft
add /Sheet1/A1:A10 cf type=databar direction=rightToLeft
Defensive patterns

Strategy: validation

Validate before calling

var valid = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{ "leftToRight", "ltr", "rightToLeft", "rtl", "context" };
if (properties.TryGetValue("direction", out var d) && !valid.Contains(d))
    throw new ArgumentException($"direction '{d}' invalid.");

Type guard

static readonly HashSet<string> DataBarDirections = new(StringComparer.OrdinalIgnoreCase)
{ "leftToRight", "ltr", "rightToLeft", "rtl", "context" };
static bool IsValidDataBarDirection(string? s) => s is null || DataBarDirections.Contains(s);

Try / catch

try { return Add(path, "databar", pos, props); }
catch (ArgumentException ex) when (ex.Message.Contains("dataBar direction"))
{ props.Remove("direction"); return Add(path, "databar", pos, props); }

Prevention

When it happens

Trigger: Calling Add with type=databar and a direction= property whose normalized form is not lefttoright/ltr, righttoleft/rtl, or context. Example: direction=horizontal or direction=normal.

Common situations: Passing a UI-oriented label like 'horizontal' or 'forward'; confusing direction with axisPosition; passing direction=rtl expecting it to mean right-to-left when SchemaKeyNormalizer maps it correctly (so the real cause is usually a different string like direction=reverse).

Related errors


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