iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {cfNewSheetName}

Error message

Sheet not found: {cfNewSheetName}

What it means

Thrown by AddCfExtended when the first segment of parentPath does not resolve to a worksheet. AddCfExtended handles the extended CF family (top10/aboveAverage/uniqueValues/duplicateValues/containsText/dateOccurring/blanks/errors/beginsWith/endsWith). The sheet segment is split from parentPath and looked up case-insensitively.

Source

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

            SequenceOfReferences = new ListValue<StringValue>(
                cisSqref.Split(' ').Select(s => new StringValue(s)))
        };

        var cisWsElement = GetSheet(cisWorksheet);
        InsertConditionalFormatting(cisWsElement, cisCf);

        SaveWorksheet(cisWorksheet);
        var cisCfCount = cisWsElement.Elements<ConditionalFormatting>().Count();
        return $"/{cisSheetName}/cf[{cisCfCount}]";
    }

    private string AddCfExtended(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var cfNewSegments = parentPath.TrimStart('/').Split('/', 2);
        var cfNewSheetName = cfNewSegments[0];
        var cfNewWorksheet = FindWorksheet(cfNewSheetName)
            ?? throw new ArgumentException($"Sheet not found: {cfNewSheetName}");
        // R22-2: path-tail range is the fallback before the hardcoded default.
        var cfNewPathRange = cfNewSegments.Length > 1 && !string.IsNullOrEmpty(cfNewSegments[1]) ? cfNewSegments[1] : "A1:A10";
        var cfNewSqref = ValidateSqref(properties.GetValueOrDefault("sqref") ?? properties.GetValueOrDefault("range") ?? properties.GetValueOrDefault("ref", cfNewPathRange), "ref");
        var cfNewPriority = NextCfPriority(GetSheet(cfNewWorksheet));

        ConditionalFormattingRule cfNewRule;
        var typeLower = type.ToLowerInvariant();
        // For cfextended dispatch, the actual requested sub-type is in
        // properties["type"] (the user-facing switch; the outer `type`
        // variable is literal "cfextended" here).
        if (typeLower == "cfextended")
            typeLower = (properties.GetValueOrDefault("type", "") ?? "").ToLowerInvariant();

        switch (typeLower)
        {
            case "topn":
            {
                // Accept `rank=` (OOXML attribute name), `top=`/`bottomN=` (legacy

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Confirm the worksheet name and use it as segment[0].
  2. Create the sheet first.
  3. Re-enumerate sheets to get current names.

Example fix

// before: 'Metrics' sheet missing
add /Metrics/A1:A10 cf type=topn value=5
// after
add-sheet Metrics
add /Metrics/A1:A10 cf type=topn value=5
Defensive patterns

Strategy: validation

Validate before calling

var sheet = parentPath.TrimStart('/').Split('/', 2)[0];
if (FindWorksheet(sheet) is null)
    throw new ArgumentException($"Sheet '{sheet}' not found.");

Type guard

static bool SheetExists(string? sheetName, IEnumerable<string> known)
    => sheetName is not null && known.Contains(sheetName, StringComparer.OrdinalIgnoreCase);

Try / catch

try { return Add(path, "cfextended", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* correct sheet, retry */ throw; }

Prevention

When it happens

Trigger: Calling Add with parentPath '/<sheet>/...' and any cfextended-routed type (topn, aboveaverage, uniquevalues, duplicatevalues, containstext, dateoccurring, belowaverage, containsblanks, notcontainsblanks, containserrors, notcontainserrors, contains, notcontains, beginswith, endswith) where <sheet> is not found.

Common situations: Renamed/deleted sheet; typo; path built from untrusted input without sheet validation; using a chart-sheet name where a worksheet is required.

Related errors


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