iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {spkSheetName}

Error message

Sheet not found: {spkSheetName}

What it means

Thrown by AddSparkline when the first segment of the parent path does not match any worksheet — the same FindWorksheet null-coalescing pattern as error 540. No sparkline XML is constructed before the lookup; this is a precondition check on the host sheet. The path tail (an optional F1-style cell) is parsed only after the sheet is resolved.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Drawings.cs:963

            if (shpWorksheet.DrawingsPart != null) shpWorksheet.DeletePart(shpWorksheet.DrawingsPart);
            SaveWorksheet(shpWorksheet);
            throw;
        }
    }

    private string AddSlicer(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        return AddSlicer(parentPath, properties);
    }

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

        // CONSISTENCY(canonical-key): 'location'/'dataRange' are canonical;
        // 'cell'/'range'/'data' retained as legacy aliases.
        // R12a: also accept the host cell from the parent path tail
        // (e.g. `add /Sheet1/F1 sparkline --prop dataRange=A1:E1`), mirroring
        // how cell/cf Add derive their target from the path. Explicit
        // location=/cell= still wins.
        var spkPathTail = spkSegments.Length > 1
            && Regex.IsMatch(spkSegments[1], @"^[A-Z]+\d+$", RegexOptions.IgnoreCase)
            ? spkSegments[1].ToUpperInvariant() : null;
        var spkCell = properties.GetValueOrDefault("location")
            ?? properties.GetValueOrDefault("cell")
            ?? spkPathTail
            ?? throw new ArgumentException("Sparkline requires 'location' (or 'cell') property (e.g. F1)");
        var spkRange = properties.GetValueOrDefault("dataRange")
            ?? properties.GetValueOrDefault("datarange")
            ?? properties.GetValueOrDefault("range")
            ?? properties.GetValueOrDefault("data")

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the sheet name against the workbook's actual sheets (FindWorksheet is case-insensitive).
  2. Use a path whose first segment is an existing sheet, e.g. `/Sheet1/F1 sparkline` or `/Sheet1 sparkline` with location=.

Example fix

// before
add ./book.xlsx /Dashbord sparkline --prop location=F1 --prop dataRange=A1:E1
// after
add ./book.xlsx /Sheet1 sparkline --prop location=F1 --prop dataRange=A1:E1
Defensive patterns

Strategy: validation

Validate before calling

// Same sheet-existence check as error 540.
if (!SheetExists(wbp, sheetName))
    throw new InvalidOperationException($"Sheet '{sheetName}' not found");

Type guard

static bool SheetExists(WorkbookPart wbp, string sheetName)
    => wbp.Workbook.Sheets.Elements<Sheet>()
        .Any(s => s.Name.Value.Equals(sheetName, StringComparison.OrdinalIgnoreCase));

Try / catch

try { handler.AddSparkline(...); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* list sheets, do not retry with the same name */ }

Prevention

When it happens

Trigger: Calling `add /WrongSheet sparkline --prop location=F1 --prop dataRange=A1:E1` where `WrongSheet` is absent, renamed, or misspelled. Also when the path is just `/` or empty so the leading segment is blank.

Common situations: Sheet renamed in the workbook but the script still references the old name; wrong file opened; path built by string concatenation with an empty sheet variable.

Related errors


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