iOfficeAI/OfficeCLI · error · System.ArgumentException

Sheet not found: {cbSheetName}

Error message

Sheet not found: {cbSheetName}

What it means

Thrown by AddColBreak when FindWorksheet(cbSheetName) returns null. The sheet name is the first segment of parentPath after trimming the leading slash. There is no path-length precondition before this check, so an empty or single-segment path whose first segment is not a worksheet also lands here.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs:1370

        if (properties.TryGetValue("manual", out var rbMan))
            rbBreak.ManualPageBreak = IsTruthy(rbMan);
        rowBreaks.AppendChild(rbBreak);
        rowBreaks.Count = (uint)rowBreaks.Elements<Break>().Count();
        rowBreaks.ManualBreakCount = rowBreaks.Count;
        SaveWorksheet(rbWorksheet);

        var rbIdx = rowBreaks.Elements<Break>().ToList()
            .FindIndex(b => b.Id?.Value == rbRowIdx) + 1;
        return $"/{rbSheetName}/rowbreak[{rbIdx}]";
    }

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

        var cbColStr = properties.GetValueOrDefault("col") ?? properties.GetValueOrDefault("column")
            ?? properties.GetValueOrDefault("index")
            ?? throw new ArgumentException("'col' property is required for colbreak");
        // Accept both numeric index (e.g. "3") and column letter (e.g. "C")
        var cbColIdx = uint.TryParse(cbColStr, out var cbNumVal)
            ? cbNumVal
            : (uint)ColumnNameToIndex(cbColStr.ToUpperInvariant());
        // Same schema Min/Max guard as rowbreak: 0 / beyond-XFD ids write
        // invalid OOXML that only surfaces at validate/open time.
        if (cbColIdx < 1 || cbColIdx > 16384)
            throw new ArgumentException(
                $"Invalid 'col' value: '{cbColStr}'. Column breaks must be between 1 and 16384 (A-XFD).");

        var colBreaks = cbWs.GetFirstChild<ColumnBreaks>();
        if (colBreaks == null)
        {

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the sheet name with a Get on / and use it verbatim.
  2. Create the sheet if it does not yet exist.
  3. Ensure the path begins with /<exactSheetName>.

Example fix

// before
handler.Add("/OldName", "colbreak", null, new() { ["col"] = "C" });

// after
handler.Add("/Sheet1", "colbreak", null, new() { ["col"] = "C" });
Defensive patterns

Strategy: validation

Validate before calling

string sheet = "Sheet1";
var sheets = handler.Query("/");
if (!sheets.Any(s => string.Equals(s.Name, sheet, StringComparison.OrdinalIgnoreCase)))
    throw new InvalidOperationException($"Sheet not found: {sheet}");
handler.Add("/" + sheet, "colbreak", null, props);

Type guard

static bool SheetExists(IExcelHandler h, string sheet)
    => h.Query("/").Any(s => string.Equals(s.Name, sheet, StringComparison.OrdinalIgnoreCase));

Try / catch

try { handler.Add(parentPath, "colbreak", null, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* create the sheet or report the missing name */ }

Prevention

When it happens

Trigger: Add type=colbreak with parentPath="/BadSheet" or a path whose sheet segment does not match any worksheet. Routed here from the break dispatcher when col/column properties are present. A typo or stale sheet name triggers it.

Common situations: Sheet renamed or removed after the path was captured. Script reuses a sheet name constant from a different workbook. Leading/trailing whitespace in the sheet segment.

Related errors


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