{"record":{"id":"4249afe0701a0e82","repo":"iOfficeAI/OfficeCLI","slug":"sheet-not-found-colsheetname","errorCode":null,"errorMessage":"Sheet not found: {colSheetName}","messagePattern":"Sheet not found: (.+?)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs","lineNumber":1035,"sourceCode":"                if (newRow != null && !newRow.Elements<Cell>().Any())\n                {\n                    var sd = newRow.Parent as SheetData;\n                    var rIdx = newRow.RowIndex?.Value;\n                    newRow.Remove();\n                    if (sd != null && rIdx.HasValue)\n                        _rowIndex?.GetValueOrDefault(sd)?.Remove(rIdx.Value);\n                }\n            }\n            throw;\n        }\n    }\n\n    private string AddCol(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)\n    {\n        var colSegments = parentPath.TrimStart('/').Split('/', 2);\n        var colSheetName = colSegments[0];\n        var colWorksheet = FindWorksheet(colSheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {colSheetName}\");\n\n        // Resolve --before / --after anchors, mirroring AddRow. Anchor must\n        // be /<sheetName>/col[L] in the same sheet; --before takes the\n        // anchor's slot, --after lands one column to the right.\n        int? index = position?.Index;\n        if (index == null && position != null && (position.After != null || position.Before != null))\n        {\n            int FindAnchorColIndex(string anchorPath)\n            {\n                var aSegs = anchorPath.TrimStart('/').Split('/', 2);\n                if (aSegs.Length < 2)\n                    throw new ArgumentException(\n                        $\"Anchor must be a column path like /{colSheetName}/col[L], got: {anchorPath}\");\n                if (!aSegs[0].Equals(colSheetName, StringComparison.OrdinalIgnoreCase))\n                    throw new ArgumentException(\n                        $\"Anchor sheet '{aSegs[0]}' must match target sheet '{colSheetName}'\");\n                var am = Regex.Match(aSegs[1], @\"^col\\[([A-Za-z]+)\\]$\", RegexOptions.IgnoreCase);\n                if (!am.Success)","sourceCodeStart":1017,"sourceCodeEnd":1053,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs#L1017-L1053","documentation":"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.","triggerScenarios":"Add(\"/Nope\",\"col\",pos,props); Add(\"/Sheet1\",\"col\",...) where Sheet1 was deleted/renamed; a typo in the sheet name segment.","commonSituations":"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.","solutions":["Confirm the sheet exists (Get/Query on the workbook root) before adding a column.","Create the sheet first with Add(\"/\",\"sheet\",null,{[\"name\"]=sheetName}) if it should exist.","Check for typos/whitespace in the sheet-name segment."],"exampleFix":"// before\nhandler.Add(\"/SheetX\", \"col\", null, new() { [\"name\"] = \"D\" });\n// after\nEnsureSheet(handler, \"SheetX\");\nhandler.Add(\"/SheetX\", \"col\", null, new() { [\"name\"] = \"D\" });","handlingStrategy":"validation","validationCode":"string sheet = colPath.TrimStart('/').Split('/', 2)[0];\nif (h.Query(\"/\").All(n => !n.Name.Equals(sheet, StringComparison.OrdinalIgnoreCase)))\n    throw new ArgumentException($\"Sheet '{sheet}' does not exist.\");\nh.Add(colPath, \"col\", pos, props);","typeGuard":null,"tryCatchPattern":"try { h.Add(colPath, \"col\", pos, props); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Sheet not found\"))\n{ /* create the sheet or correct the name */ }","preventionTips":["Resolve sheet names dynamically; do not hardcode.","Create the sheet before adding columns in batch replays.","Check for typos/whitespace in the sheet segment."],"tags":["excel","xlsx","col","sheet-not-found","path"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}