{"record":{"id":"7a001328be28cc62","repo":"iOfficeAI/OfficeCLI","slug":"anchor-row-anchorrowidx-not-found-in-targetshee","errorCode":null,"errorMessage":"Anchor row {anchorRowIdx} not found in {targetSheetName}","messagePattern":"Anchor row (.+?) not found in (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":330,"sourceCode":"            {\n                int FindAnchorRowPos(string anchorPath)\n                {\n                    var aSegs = anchorPath.TrimStart('/').Split('/', 2);\n                    if (aSegs.Length < 2)\n                        throw new ArgumentException(\n                            $\"Anchor must be a row path like /{targetSheetName}/row[K], got: {anchorPath}\");\n                    if (!aSegs[0].Equals(targetSheetName, StringComparison.OrdinalIgnoreCase))\n                        throw new ArgumentException(\n                            $\"Anchor sheet '{aSegs[0]}' must match target sheet '{targetSheetName}'\");\n                    var am = Regex.Match(aSegs[1], @\"^row\\[(\\d+)\\]$\");\n                    if (!am.Success)\n                        throw new ArgumentException(\n                            $\"Anchor must be a row path like /{targetSheetName}/row[K], got: {anchorPath}\");\n                    var anchorRowIdx = uint.Parse(am.Groups[1].Value);\n                    var pos = targetSheetData.Elements<Row>().ToList()\n                        .FindIndex(r => r.RowIndex?.Value == anchorRowIdx);\n                    if (pos < 0)\n                        throw new ArgumentException($\"Anchor row {anchorRowIdx} not found in {targetSheetName}\");\n                    return pos;\n                }\n                if (position.Before != null) targetIndex = FindAnchorRowPos(position.Before);\n                else targetIndex = FindAnchorRowPos(position.After!) + 1;\n            }\n\n            // If the moved row sits before the anchor in the same sheet,\n            // removing it shifts everything (including the anchor) up by one.\n            // Adjust the resolved target index so it still points at the\n            // intended slot in post-remove document order.\n            if (targetIndex.HasValue && targetSheetData == sheetData)\n            {\n                var srcPos = sheetData.Elements<Row>().ToList().IndexOf(row);\n                if (srcPos >= 0 && srcPos < targetIndex.Value)\n                    targetIndex = targetIndex.Value - 1;\n            }\n\n            // Snapshot every row's old RowIndex (per sheet) so we can build","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L312-L348","documentation":"Thrown by the row-anchor resolver when the anchor row K exists syntactically but no <Row> with RowIndex == K is found in the target sheet (FindIndex returned -1). The anchor row must be a real, stored row in the destination sheet so its document-order position can be computed.","triggerScenarios":"Anchor '/Dst/row[7]' in a target sheet that has no row 7 (sparse/empty rows not materialized); row deleted after the anchor path was built; ordinal vs RowIndex confusion.","commonSituations":"Anchor row removed in a prior edit; referencing a visually-present but un-stored empty row; target sheet smaller than the source.","solutions":["Enumerate the target sheet's rows and pick an anchor index that exists.","Use InsertPosition.AtIndex(n) if you only need an absolute slot.","If the target lacks the row, seed it first or choose a different anchor."],"exampleFix":"// before\nh.Move(\"/Src/row[1]\", \"/Dst\", InsertPosition.AfterElement(\"/Dst/row[9]\")); // Dst has no row 9\n// after\nh.Move(\"/Src/row[1]\", \"/Dst\", InsertPosition.AfterElement(\"/Dst/row[2]\")); // Dst has row 2","handlingStrategy":"validation","validationCode":"var am = System.Text.RegularExpressions.Regex.Match(anchor.Split('/', 2)[1], @\"^row\\[(\\d+)\\]$\");\nvar k = uint.Parse(am.Groups[1].Value);\nvar targetRows = handler.Get(targetSheet, depth: 2).Children.Where(c => c.Type == \"row\").ToList();\nif (!targetRows.Any(r => r.Name.EndsWith($\"[{k}]\") /* or match the stored index */ ))\n    throw new InvalidOperationException($\"Anchor row {k} not present in {targetSheet}\");","typeGuard":null,"tryCatchPattern":"try { handler.Move(src, target, pos); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Anchor row\") && ex.Message.Contains(\"not found\"))\n{ /* pick an anchor row that exists in the target sheet */ }","preventionTips":["Enumerate the target sheet's rows before choosing an anchor index.","Use AtIndex(n) when you only need a slot.","Seed target sheets that lack the desired anchor row."],"tags":["excel","openxml","move","row-move","anchor","not-found"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}