{"record":{"id":"be72888736fa8eae","repo":"iOfficeAI/OfficeCLI","slug":"target-sheet-not-found-tgtsegments-0","errorCode":null,"errorMessage":"Target sheet not found: {tgtSegments[0]}","messagePattern":"Target sheet not found: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":287,"sourceCode":"        var elementRef = segments[1];\n        var sheetData = GetSheet(worksheet).GetFirstChild<SheetData>()\n            ?? throw new ArgumentException(\"Sheet has no data\");\n\n        // Determine the target sheet's SheetData. The result path is built from\n        // the resolved target SHEET (below), NOT the raw --to: a row/col/cell\n        // lives directly under a sheet, so a non-sheet --to like /Sheet1/row[2]\n        // must not leak into the result path (it used to produce a doubled\n        // /Sheet1/row[2]/row[3]). Only the sheet segment of --to is meaningful.\n        SheetData targetSheetData;\n        if (string.IsNullOrEmpty(targetParentPath))\n        {\n            targetSheetData = sheetData;\n        }\n        else\n        {\n            var tgtSegments = targetParentPath.TrimStart('/').Split('/', 2);\n            var tgtWorksheet = FindWorksheet(tgtSegments[0])\n                ?? throw new ArgumentException($\"Target sheet not found: {tgtSegments[0]}\");\n            targetSheetData = GetSheet(tgtWorksheet).GetFirstChild<SheetData>()\n                ?? throw new ArgumentException(\"Target sheet has no data\");\n        }\n\n        // Find and move the row\n        var rowMatch = Regex.Match(elementRef, @\"^row\\[(\\d+)\\]$\");\n        if (rowMatch.Success)\n        {\n            var rowIdx = int.Parse(rowMatch.Groups[1].Value);\n            // Try ordinal lookup first (Nth row element), then fall back to RowIndex\n            var allRows = sheetData.Elements<Row>().ToList();\n            var row = (rowIdx >= 1 && rowIdx <= allRows.Count ? allRows[PathIndex.ToArrayIndex(rowIdx)] : null)\n                ?? sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex?.Value == (uint)rowIdx)\n                ?? throw new ArgumentException($\"Row {rowIdx} not found\");\n\n            // Resolve --before / --after anchors to a 0-based document-order\n            // position in the target sheet. Anchor must be /<TargetSheet>/row[K].\n            // Resolved BEFORE removing the moved row so the anchor is found by","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L269-L305","documentation":"Thrown for a row move when targetParentPath (--to) is non-empty but its first segment does not name an existing worksheet (FindWorksheet on tgtSegments[0] returned null). The handler resolves the destination SheetData by sheet name, so an unknown target sheet aborts before any row is detached.","triggerScenarios":"Move(\"/Sheet1/row[2]\", \"/Nope\", position) where 'Nope' is not a sheet; target sheet renamed/deleted; --to path malformed so the first segment is wrong.","commonSituations":"Cross-sheet move to a sheet that was removed; typo in --to; copy-paste of an old path; assuming a sheet exists because it appears in a cached view.","solutions":["Confirm the target sheet exists (handler.Get(\"/\", depth:1)) before the cross-sheet move.","Create the target sheet first via Add if it should exist.","Pass null/empty targetParentPath to move within the same sheet."],"exampleFix":"// before\nh.Move(\"/Sheet1/row[2]\", \"/Dest\", InsertPosition.AtIndex(0)); // 'Dest' missing\n// after\nh.Add(\"/\", \"sheet\", null, new(){{\"name\",\"Dest\"}});\nh.Move(\"/Sheet1/row[2]\", \"/Dest\", InsertPosition.AtIndex(0));","handlingStrategy":"validation","validationCode":"if (!string.IsNullOrEmpty(targetParentPath))\n{\n    var tgt = targetParentPath.TrimStart('/').Split('/', 2)[0];\n    var sheets = handler.Get(\"/\", depth: 1).Children.Select(c => c.Name).ToList();\n    if (!sheets.Any(s => s.Equals(tgt, StringComparison.OrdinalIgnoreCase)))\n        throw new InvalidOperationException($\"Target sheet '{tgt}' does not exist\");\n}","typeGuard":null,"tryCatchPattern":"try { handler.Move(src, target, pos); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Target sheet not found\"))\n{ /* create or choose a valid target sheet, then retry */ }","preventionTips":["Validate the target sheet name with handler.Get(\"/\", depth:1).","Pass null/empty targetParentPath for same-sheet moves.","Create the target sheet via Add before a cross-sheet move."],"tags":["excel","openxml","move","row-move","target-sheet","not-found"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}