{"record":{"id":"c782b09de9fa4c2b","repo":"iOfficeAI/OfficeCLI","slug":"sheet-not-found-tblsheetname","errorCode":null,"errorMessage":"Sheet not found: {tblSheetName}","messagePattern":"Sheet not found: (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs","lineNumber":1064,"sourceCode":"                        : fop == FilterOperatorValues.NotEqual ? \"notEquals\"\n                        : fop == FilterOperatorValues.GreaterThan ? \"gt\"\n                        : fop == FilterOperatorValues.GreaterThanOrEqual ? \"gte\"\n                        : fop == FilterOperatorValues.LessThan ? \"lt\"\n                        : fop == FilterOperatorValues.LessThanOrEqual ? \"lte\"\n                        : \"equals\";\n                    node.Format[prefix + op] = val;\n                }\n            }\n        }\n    }\n\n    private string AddTable(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var index = position?.Index;\n        var tblSegments = parentPath.TrimStart('/').Split('/', 2);\n        var tblSheetName = tblSegments[0];\n        var tblWorksheet = FindWorksheet(tblSheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {tblSheetName}\");\n\n        var rangeRef = (properties.GetValueOrDefault(\"ref\") ?? properties.GetValueOrDefault(\"range\")\n            ?? throw new ArgumentException(\"Property 'ref' or 'range' is required for table\")).ToUpperInvariant();\n\n        // T4 — reject a new table whose ref overlaps any existing table on\n        // the same sheet. Excel silently corrupts the file otherwise.\n        foreach (var existingTdp in tblWorksheet.TableDefinitionParts)\n        {\n            var existing = existingTdp.Table;\n            if (existing?.Reference?.Value is not string existingRef) continue;\n            if (RangesOverlap(rangeRef, existingRef))\n                throw new ArgumentException(\n                    $\"Table ref overlaps existing table '{existing.Name?.Value ?? existing.DisplayName?.Value}' ({existingRef})\");\n        }\n\n\n        var existingTableIds = _doc.WorkbookPart!.WorksheetParts\n            .SelectMany(wp => wp.TableDefinitionParts)","sourceCodeStart":1046,"sourceCodeEnd":1082,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Tables.cs#L1046-L1082","documentation":"Thrown by AddTable when the first segment of the parent path (the sheet name) does not resolve to an existing worksheet via FindWorksheet. A table must live on a concrete sheet, so an unknown sheet name cannot host the new table. The handler rejects it up front instead of creating an orphan part.","triggerScenarios":"Calling Add('/MissingSheet/table', ...) or Add('/Sheet 1/table', ...) when no worksheet by that exact name exists, including whitespace, case, or renamed-sheet mismatches.","commonSituations":"Sheet was renamed or deleted between calls, a sheet name with spaces was not quoted correctly, or the caller used a 1-based index where a name was expected.","solutions":["List sheets first and copy the exact name (query the workbook's sheet list).","Quote sheet names containing spaces exactly as they appear in the workbook.","If you intended a new sheet, add it first with --type sheet, then add the table to it."],"exampleFix":"// before\nadd /Q1Report/table --prop ref=A1:D10\n// after (verify sheet exists; create if needed)\nadd /sheet --prop name=Q1Report\nadd /Q1Report/table --prop ref=A1:D10","handlingStrategy":"validation","validationCode":"// Confirm the host sheet exists before adding a table to it.\nvar sheetName = parentPath.TrimStart('/').Split('/', 2)[0];\nif (handler.FindWorksheet(sheetName) is null)\n    throw new InvalidOperationException($\"Cannot add table: sheet '{sheetName}' not found.\");","typeGuard":null,"tryCatchPattern":"try { handler.Add(parentPath, \"table\", null, props); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Sheet not found\"))\n{ /* list sheets and retry with the correct name */ }","preventionTips":["Query the workbook's sheet list once and cache it when scripting many adds.","Normalize sheet names (trim, exact case) before constructing parent paths.","Create the host sheet first in idempotent setup scripts."],"tags":["excel","table","validation","sheet-lookup"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}