iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {colSheetName}

Error message

Sheet not found: {colSheetName}

What it means

AddCol resolves the parent path's first segment as the worksheet name and looks it up via FindWorksheet. If no worksheet by that name exists (case-insensitive), it throws before any column work. This mirrors AddRow/AddCell's sheet-not-found guard so a column Add against a missing sheet fails cleanly rather than writing into nothing.

Source

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

                if (newRow != null && !newRow.Elements<Cell>().Any())
                {
                    var sd = newRow.Parent as SheetData;
                    var rIdx = newRow.RowIndex?.Value;
                    newRow.Remove();
                    if (sd != null && rIdx.HasValue)
                        _rowIndex?.GetValueOrDefault(sd)?.Remove(rIdx.Value);
                }
            }
            throw;
        }
    }

    private string AddCol(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var colSegments = parentPath.TrimStart('/').Split('/', 2);
        var colSheetName = colSegments[0];
        var colWorksheet = FindWorksheet(colSheetName)
            ?? throw new ArgumentException($"Sheet not found: {colSheetName}");

        // Resolve --before / --after anchors, mirroring AddRow. Anchor must
        // be /<sheetName>/col[L] in the same sheet; --before takes the
        // anchor's slot, --after lands one column to the right.
        int? index = position?.Index;
        if (index == null && position != null && (position.After != null || position.Before != null))
        {
            int FindAnchorColIndex(string anchorPath)
            {
                var aSegs = anchorPath.TrimStart('/').Split('/', 2);
                if (aSegs.Length < 2)
                    throw new ArgumentException(
                        $"Anchor must be a column path like /{colSheetName}/col[L], got: {anchorPath}");
                if (!aSegs[0].Equals(colSheetName, StringComparison.OrdinalIgnoreCase))
                    throw new ArgumentException(
                        $"Anchor sheet '{aSegs[0]}' must match target sheet '{colSheetName}'");
                var am = Regex.Match(aSegs[1], @"^col\[([A-Za-z]+)\]$", RegexOptions.IgnoreCase);
                if (!am.Success)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Confirm the sheet exists (Get/Query on the workbook root) before adding a column.
  2. Create the sheet first with Add("/","sheet",null,{["name"]=sheetName}) if it should exist.
  3. Check for typos/whitespace in the sheet-name segment.

Example fix

// before
handler.Add("/SheetX", "col", null, new() { ["name"] = "D" });
// after
EnsureSheet(handler, "SheetX");
handler.Add("/SheetX", "col", null, new() { ["name"] = "D" });
Defensive patterns

Strategy: validation

Validate before calling

string sheet = colPath.TrimStart('/').Split('/', 2)[0];
if (h.Query("/").All(n => !n.Name.Equals(sheet, StringComparison.OrdinalIgnoreCase)))
    throw new ArgumentException($"Sheet '{sheet}' does not exist.");
h.Add(colPath, "col", pos, props);

Try / catch

try { h.Add(colPath, "col", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* create the sheet or correct the name */ }

Prevention

When it happens

Trigger: Add("/Nope","col",pos,props); Add("/Sheet1","col",...) where Sheet1 was deleted/renamed; a typo in the sheet name segment.

Common situations: Hardcoded sheet names drifting after a rename; replaying a column-add batch recorded against one workbook onto another with different sheet names; a templated path where the sheet variable is empty.

Related errors


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