iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {cmtSheetName}

Error message

Sheet not found: {cmtSheetName}

What it means

Thrown by AddComment when FindWorksheet(cmtSheetName) returns null. The sheet name is taken from the first segment of parentPath (after trimming a leading '/'). It is the plain inline form ('Sheet not found: <name>'), not the richer SheetNotFoundException that lists available sheets, so the message gives only the missing name.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs:250

        }

        workbook.Save();

        var nrIdx = PathIndex.FromArrayIndex(definedNames.Elements<DefinedName>().ToList().IndexOf(dn));
        return $"/namedrange[{nrIdx}]";
    }

    private string AddComment(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var cmtSegments = parentPath.TrimStart('/').Split('/', 2);
        var cmtSheetName = cmtSegments[0];
        // Extract cell reference from path if present (e.g., /Sheet1/A1 -> A1)
        string? cmtRefFromPath = null;
        if (cmtSegments.Length > 1 && Regex.IsMatch(cmtSegments[1], @"^[A-Z]+\d+$", RegexOptions.IgnoreCase))
            cmtRefFromPath = cmtSegments[1];
        var cmtWorksheet = FindWorksheet(cmtSheetName)
            ?? throw new ArgumentException($"Sheet not found: {cmtSheetName}");

        var cmtRef = properties.GetValueOrDefault("ref") ?? cmtRefFromPath
            ?? throw new ArgumentException("Property 'ref' is required for comment");
        // Validate cell reference up-front; ParseCellReference rejects bad
        // syntax, out-of-range rows (>1048576), and out-of-range columns (>XFD)
        // with a clear ArgumentException — matches the validation surface
        // already enforced for cells/ranges elsewhere.
        ParseCellReference(cmtRef);
        var cmtText = properties.GetValueOrDefault("text", "");
        var cmtAuthor = properties.GetValueOrDefault("author", "Author");
        OfficeCli.Core.ParseHelpers.ValidateXmlText(cmtText, "comment text");
        OfficeCli.Core.ParseHelpers.ValidateXmlText(cmtAuthor, "comment author");

        var commentsPart = cmtWorksheet.WorksheetCommentsPart
            ?? cmtWorksheet.AddNewPart<WorksheetCommentsPart>();

        if (commentsPart.Comments == null)
        {

View on GitHub (pinned to 1ced45e900)

Solutions

  1. List the real sheet names via handler.GetDumpSheetNames() and use an exact name.
  2. Correct the parentPath first segment to match an existing sheet (matching is case-insensitive).
  3. If the sheet genuinely should exist, add it first with type "sheet".

Example fix

// before
handler.Add("/Sheet1/A1", "comment", null, new() { ["text"] = "hi" });
// no sheet 'Sheet1' (it is 'Data')
// after
handler.Add("/Data/A1", "comment", null, new() { ["text"] = "hi" });
Defensive patterns

Strategy: validation

Validate before calling

var sheet = parentPath.TrimStart('/').Split('/', 2)[0];
if (!handler.GetDumpSheetNames()
        .Any(n => n.Equals(sheet, StringComparison.OrdinalIgnoreCase)))
    throw new InvalidOperationException($"No sheet '{sheet}'. Available: " +
        string.Join(", ", handler.GetDumpSheetNames()));

Prevention

When it happens

Trigger: Call Add with type "comment"/"note" and a parentPath whose first segment is not a worksheet in the workbook, e.g. "/TypoSheet/A1".

Common situations: Sheet-name typo; the sheet was renamed or deleted between runs; leading/trailing whitespace or a path that omits the leading slash after normalization; copy-pasting a path from another workbook.

Related errors


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