iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {csSheetName}

Error message

Sheet not found: {csSheetName}

What it means

Thrown by AddColorScale when the first segment of the parentPath (the sheet name) does not resolve to any worksheet in the open workbook. The parentPath is split on '/', segment[0] is the sheet name, and FindWorksheet does a case-insensitive lookup. This is a path-resolution failure: the color scale rule has nowhere to attach.

Source

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

        var x14Cf = new X14.ConditionalFormatting();
        x14Cf.AddNamespaceDeclaration("xm", "http://schemas.microsoft.com/office/excel/2006/main");
        x14Cf.Append(x14CfRule);
        x14Cf.Append(new DocumentFormat.OpenXml.Office.Excel.ReferenceSequence(sqref));

        EnsureWorksheetX14ConditionalFormatting(wsElement, x14Cf);

        SaveWorksheet(cfWorksheet);
        var dbCfCount = wsElement.Elements<ConditionalFormatting>().Count();
        return $"/{cfSheetName}/cf[{dbCfCount}]";
    }

    private string AddColorScale(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var csSegments = parentPath.TrimStart('/').Split('/', 2);
        var csSheetName = csSegments[0];
        var csWorksheet = FindWorksheet(csSheetName)
            ?? throw new ArgumentException($"Sheet not found: {csSheetName}");

        // CONSISTENCY(cf-sqref): three-level fallback matches dataBar/formulacf branches.
        // R22-2: path-tail range is the fallback before the hardcoded default.
        var csPathRange = csSegments.Length > 1 && !string.IsNullOrEmpty(csSegments[1]) ? csSegments[1] : "A1:A10";
        var csSqref = ValidateSqref(properties.GetValueOrDefault("sqref") ?? properties.GetValueOrDefault("range") ?? properties.GetValueOrDefault("ref", csPathRange), "ref");
        var minColor = properties.GetValueOrDefault("mincolor", "F8696B");
        var maxColor = properties.GetValueOrDefault("maxcolor", "63BE7B");
        var midColor = properties.GetValueOrDefault("midcolor");

        var normalizedMinColor = ParseHelpers.NormalizeArgbColor(minColor);
        var normalizedMaxColor = ParseHelpers.NormalizeArgbColor(maxColor);

        // CF5 — accept user-supplied midpoint (`midpoint=50`, default 50).
        // Lenient forms: "50", "50%" (percent → percentile), and prefixed
        // "percentile:50" / "percent:50" / "num:50". Anything else must be
        // rejected: an unparsed value ("50%", "percentile:50") used to land
        // verbatim in <cfvo val=>, which passes schema validation but real
        // Excel refuses the whole file (0x800A03EC).

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List the workbook's sheets (e.g. via a list/listing command) and use the exact name as segment[0] of the path.
  2. If the sheet does not exist, create it first with an add-sheet operation before adding the color scale.
  3. Ensure the parentPath is '/SheetName/Range' — the sheet name is the first segment after trimming the leading '/'.

Example fix

// before: sheet 'Data' does not exist
add /Data/A1:A10 colorscale mincolor=F8696B maxcolor=63BE7B
// after: create the sheet first, then add the rule
add-sheet Data
add /Data/A1:A10 colorscale mincolor=F8696B maxcolor=63BE7B
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. Available: {string.Join(", ", GetWorksheets().Select(w => w.Name))}");

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, "colorscale", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* prompt user to pick from available sheets, then retry */ throw; }

Prevention

When it happens

Trigger: Calling Add with parentPath like '/NonExistentSheet/A1:A10' and type=colorscale (or cf type=colorscale), where 'NonExistentSheet' is not a worksheet name in the current workbook. Also triggered when the path omits the leading '/' and the whole path collapses into segment[0].

Common situations: Sheet was renamed or deleted between sessions; typo in sheet name; using a 1-based display index instead of the name; workbook loaded from a template whose default sheet name differs (Sheet1 vs Sheet 1 vs Tabelle1).

Related errors


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