{"record":{"id":"e0d3bd58a2d9d566","repo":"iOfficeAI/OfficeCLI","slug":"sheet-not-found-sheetname","errorCode":null,"errorMessage":"Sheet not found: {sheetName}","messagePattern":"Sheet not found: (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs","lineNumber":174,"sourceCode":"        if (sheet.State != null && sheet.State.Value != SheetStateValues.Visible) return false;\n        if (sheet.Id?.Value == null) return false;\n        if (workbookPart.GetPartById(sheet.Id.Value) is not WorksheetPart wsp) return false;\n        var ws = wsp.Worksheet;\n        if (ws == null) return false;\n        var sheetData = ws.GetFirstChild<SheetData>();\n        if (sheetData != null && sheetData.Elements<Row>().Any()) return false;\n        var props = ws.GetFirstChild<SheetProperties>();\n        if (props?.GetFirstChild<TabColor>() != null) return false;\n        if (ws.Descendants<AutoFilter>().Any()) return false;\n        return true;\n    }\n\n    private string AddRow(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var segments = parentPath.TrimStart('/').Split('/', 2);\n        var sheetName = segments[0];\n        var worksheet = FindWorksheet(sheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {sheetName}\");\n        var sheetData = GetSheet(worksheet).GetFirstChild<SheetData>()\n            ?? GetSheet(worksheet).AppendChild(new SheetData());\n\n        // Resolve --before / --after anchors (same shape as Excel CopyFrom):\n        // anchor must be /<sheetName>/row[K] in the same sheet.\n        // CONSISTENCY(zero-based-index): per project convention, position.Index\n        // is 0-based across all formats (--index 0 = head, --index 1 = before\n        // 2nd slot). xlsx Row uses a 1-based RowIndex internally, so +1 here\n        // and let the existing branch keep treating `index` as a 1-based row\n        // number (which is also what the anchor branch below produces).\n        int? index = position?.Index.HasValue == true ? position!.Index!.Value + 1 : (int?)null;\n        if (index == null && position != null && (position.After != null || position.Before != null))\n        {\n            int FindAnchorRow(string anchorPath)\n            {\n                var aSegs = anchorPath.TrimStart('/').Split('/', 2);\n                if (aSegs.Length < 2)\n                    throw new ArgumentException(","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs#L156-L192","documentation":"Thrown by ExcelHandler.AddRow when the sheet name extracted from the parent path does not match any worksheet in the workbook. The parent path is split on '/' to extract the sheet name as the first segment, and FindWorksheet returns null when no worksheet has that name. This is the row-insertion equivalent of a missing-sheet error.","triggerScenarios":"Calling Add with a path like '/NonexistentSheet/row' where 'NonexistentSheet' does not exist. The parent path is expected to be '/<sheetName>' for row insertion. Also triggered if the sheet name has a typo or casing mismatch (FindWorksheet resolution behavior).","commonSituations":"A batch replay where an 'add sheet' command was supposed to create the sheet first but failed; a typo in the sheet name; a path constructed dynamically where the sheet-name variable was empty or wrong; referencing a sheet that was deleted in a concurrent modification.","solutions":["Verify the sheet name exists: list sheets with a query on '/' first.","Check the path structure: it should be '/<sheetName>' for adding rows.","Ensure any preceding 'add sheet' command in the batch succeeded.","If the sheet name is computed, add a null/empty check before constructing the path."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Verify the sheet exists before adding rows\nif (handler.FindWorksheet(sheetName) == null)\n    throw new InvalidOperationException($\"Sheet '{sheetName}' does not exist.\");\nhandler.Add($\"/{sheetName}\", \"row\", position, properties);","typeGuard":null,"tryCatchPattern":"try\n{\n    handler.Add($\"/{sheetName}\", \"row\", position, properties);\n}\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Sheet not found\"))\n{\n    // Sheet does not exist — create it first or report the error\n    logger.LogError(\"Sheet '{Sheet}' not found. Create it first.\", sheetName);\n    throw;\n}","preventionTips":["Verify the sheet name exists before adding rows to it.","In batch operations, ensure 'add sheet' precedes 'add row' for the same sheet.","Check for typos and casing in sheet names.","Use the query command to enumerate sheets when in doubt."],"tags":["excel","add-row","sheet-not-found","path-resolution"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}