iOfficeAI/OfficeCLI · error · ArgumentException

labelFilter field '{fieldName}' not found in source headers

Error message

labelFilter field '{fieldName}' not found in source headers

What it means

Thrown by ParseLabelFilterSpec when the field name (first segment of the labelFilter spec) does not match any header in the source data's headers array. The lookup uses Array.FindIndex with ordinal case-sensitive comparison, so the field name must match a source header exactly (same spelling and case).

Source

Thrown at src/officecli/Core/PivotTableHelper.cs:845

        { FieldIdx = fieldIdx; OpType = opType; Needle = needle; Match = match; }
    }

    private static LabelFilterSpec? ParseLabelFilterSpec(
        string[] headers, Dictionary<string, string> properties)
    {
        if (!properties.TryGetValue("labelFilter", out var spec) || string.IsNullOrEmpty(spec))
            return null;
        var parts = spec.Split(':', 3);
        if (parts.Length != 3)
            throw new ArgumentException(
                $"labelFilter must be 'field:type:value', got: '{spec}'");
        var fieldName = parts[0].Trim();
        var opType = parts[1].Trim().ToLowerInvariant();
        var needle = parts[2];

        int fieldIdx = Array.FindIndex(headers, h => string.Equals(h, fieldName, StringComparison.Ordinal));
        if (fieldIdx < 0)
            throw new ArgumentException($"labelFilter field '{fieldName}' not found in source headers");

        Func<string, bool> match = opType switch
        {
            "contains" => v => v != null && v.IndexOf(needle, StringComparison.Ordinal) >= 0,
            "doesnotcontain" => v => v == null || v.IndexOf(needle, StringComparison.Ordinal) < 0,
            "beginswith" => v => v != null && v.StartsWith(needle, StringComparison.Ordinal),
            "endswith" => v => v != null && v.EndsWith(needle, StringComparison.Ordinal),
            "equals" => v => string.Equals(v, needle, StringComparison.Ordinal),
            "notequals" => v => !string.Equals(v, needle, StringComparison.Ordinal),
            _ => throw new ArgumentException(
                $"labelFilter type must be one of contains/doesNotContain/beginsWith/endsWith/equals/notEquals, got: '{opType}'"),
        };
        return new LabelFilterSpec(fieldIdx, opType, needle, match);
    }

    /// <summary>
    /// Apply a parsed labelFilter spec to columnData in place (drop
    /// non-matching rows). Used by the render path's data shaping when

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the exact field name and case against the source headers (Get the source headers first).
  2. Match the case exactly — 'Region' not 'region' if the header is capitalized.
  3. If the field was renamed, use the current header name.
  4. Confirm the field exists in the source range the pivot reads from.

Example fix

// before — header is 'Region' (capital R)
Set pivot labelFilter='region:contains:North'
// after
Set pivot labelFilter='Region:contains:North'
Defensive patterns

Strategy: validation

Validate before calling

// Case-sensitive check before applying the filter
int fieldIdx = Array.FindIndex(headers, h => string.Equals(h, fieldName, StringComparison.Ordinal));
if (fieldIdx < 0)
    throw new ArgumentException($"labelFilter field '{fieldName}' not found. Available: {string.Join(", ", headers)}");

Type guard

static bool FieldExistsInHeaders(string[] headers, string fieldName) =>
    Array.Exists(headers, h => string.Equals(h, fieldName, StringComparison.Ordinal));

Prevention

When it happens

Trigger: Calling labelFilter='Regon:contains:North' (typo in field name); labelFilter='region:contains:North' (wrong case — headers are case-sensitive); labelFilter='Sales Amount:contains:High' when the header is 'Amount'. The field must exist in the current source headers.

Common situations: Case mismatch (the comparison is Ordinal/case-sensitive); typo in the field name; referencing a field that was renamed or is in a different source range; after source narrowing (error 345 path) the field no longer exists.

Related errors


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