iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {isSheetName}

Error message

Sheet not found: {isSheetName}

What it means

Thrown by AddIconSet when the first segment of parentPath (the sheet name) does not resolve to any worksheet via FindWorksheet (case-insensitive). The icon set rule needs a target worksheet element to attach the <conditionalFormatting> child to; without one, there is no attachment point.

Source

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

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

        var csWsElement = GetSheet(csWorksheet);
        InsertConditionalFormatting(csWsElement, csCf);

        SaveWorksheet(csWorksheet);
        var csCfCount = csWsElement.Elements<ConditionalFormatting>().Count();
        return $"/{csSheetName}/cf[{csCfCount}]";
    }

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

        // CONSISTENCY(cf-sqref): three-level fallback matches dataBar/formulacf branches.
        // R22-2: path-tail range is the fallback before the hardcoded default.
        var isPathRange = isSegments.Length > 1 && !string.IsNullOrEmpty(isSegments[1]) ? isSegments[1] : "A1:A10";
        var isSqref = ValidateSqref(properties.GetValueOrDefault("sqref") ?? properties.GetValueOrDefault("range") ?? properties.GetValueOrDefault("ref", isPathRange), "ref");
        var iconSetName = properties.GetValueOrDefault("iconset") ?? properties.GetValueOrDefault("icons", "3TrafficLights1");
        var reverse = properties.TryGetValue("reverse", out var revVal) && IsTruthy(revVal);
        var showValue = !properties.TryGetValue("showvalue", out var svVal) || IsTruthy(svVal);

        var iconSetVal = ParseIconSetValues(iconSetName);

        var iconSet = new IconSet { IconSetValue = iconSetVal };
        if (reverse) iconSet.Reverse = true;
        if (!showValue) iconSet.ShowValue = false;

        // Add threshold values based on icon count
        var iconCount = GetIconCount(iconSetName);
        for (int i = 0; i < iconCount; i++)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the sheet name exists in the workbook and use it exactly as segment[0] of the path.
  2. Create the sheet first if it is missing.
  3. Confirm the path format is '/SheetName/Range' so the split puts the name in segment[0].

Example fix

// before: sheet 'Summary' missing
add /Summary/A1:A10 iconset iconset=3TrafficLights1
// after: ensure sheet exists
add-sheet Summary
add /Summary/A1:A10 iconset iconset=3TrafficLights1
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, "iconset", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* list sheets, let user correct path, retry */ throw; }

Prevention

When it happens

Trigger: Calling Add with parentPath '/<sheet>/...' and type=iconset (or cf type=iconset) where <sheet> is not a worksheet in the workbook. Triggered identically by a typo, a stale name, or a mis-split path.

Common situations: Referencing a sheet that was deleted; case mismatch is tolerated but a genuine name difference is not; using the chart-sheet or macro-sheet label instead of a worksheet name.

Related errors


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