iOfficeAI/OfficeCLI · error · ArgumentException

slicer[{slIdx}] not found on sheet '{sheetNameFromPath}'

Error message

slicer[{slIdx}] not found on sheet '{sheetNameFromPath}'

What it means

Thrown for /Sheet/slicer[N] when TryFindSlicerByIndex finds no slicer at that 1-based index on the sheet. Slicers are cross-referenced through their cache parts and can live in the worksheet's SlicerList or a timeline cache; a missing or invalid index (including slicer[0]) yields this not-found error.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.cs:771

            var ptIdx = int.Parse(pivotMatch.Groups[1].Value);
            var pivotParts = worksheet.PivotTableParts.ToList();
            if (ptIdx < 1 || ptIdx > pivotParts.Count)
                throw new ArgumentException($"PivotTable index {ptIdx} out of range (1-{pivotParts.Count})");

            var pivotPart = pivotParts[ptIdx - 1];
            var ptNode = new DocumentNode { Path = path, Type = "pivottable" };
            if (pivotPart.PivotTableDefinition != null)
                PivotTableHelper.ReadPivotTableProperties(pivotPart.PivotTableDefinition, ptNode, pivotPart);
            return ptNode;
        }

        // Slicer path: /Sheet1/slicer[N]
        var slicerMatch = Regex.Match(cellRef, @"^slicer\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (slicerMatch.Success)
        {
            var slIdx = int.Parse(slicerMatch.Groups[1].Value);
            if (!TryFindSlicerByIndex(worksheet, slIdx, out var slicerElem, out var slicerCache) || slicerElem == null)
                throw new ArgumentException($"slicer[{slIdx}] not found on sheet '{sheetNameFromPath}'");
            var slNode = new DocumentNode { Path = path, Type = "slicer" };
            ReadSlicerProperties(slicerElem, slicerCache, slNode);
            return slNode;
        }

        // OLE object path: /Sheet1/ole[N]
        // CONSISTENCY(ole-alias): "oleobject" mirrors Add's case switch
        var oleMatch = Regex.Match(cellRef, @"^(?:ole|oleobject|object|embed)\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (oleMatch.Success)
        {
            var oleIdx = int.Parse(oleMatch.Groups[1].Value);
            var oleList = CollectOleNodesForSheet(sheetNameFromPath, worksheet);
            if (oleIdx < 1 || oleIdx > oleList.Count)
                throw new ArgumentException($"OLE object {oleIdx} not found at /{sheetNameFromPath} (available: {oleList.Count}).");
            return oleList[oleIdx - 1];
        }

        // Comment path: /Sheet1/comment[N]

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index within the slicer count.
  2. Confirm slicers exist on the sheet before indexing.
  3. try/catch(ArgumentException) and degrade to 'no slicer'.

Example fix

// before
var sl = handler.Get("/Sheet1/slicer[3]"); // throws if <3 slicers

// after
try { var sl = handler.Get("/Sheet1/slicer[3]"); }
catch (ArgumentException) { /* slicer index invalid */ }
Defensive patterns

Strategy: try-catch

Type guard

static int? ElementIndex(string cellRef, string element)
{
    var m = Regex.Match(cellRef, $@"^{Regex.Escape(element)}\[(\d+)$", RegexOptions.IgnoreCase);
    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;
}

Try / catch

try { return handler.Get("/Sheet1/slicer[3]"); }
catch (ArgumentException) { /* slicer index invalid */ return null; }

Prevention

When it happens

Trigger: handler.Get("/Sheet1/slicer[3]") on a sheet with fewer than 3 slicers (or none). slicer[0].

Common situations: Hard-coded slicer index after slicers were deleted. Sheet has timelines but no slicers (or vice versa). Zero-based indexing.

Related errors


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