{"record":{"id":"00255ff4b7689b7c","repo":"iOfficeAI/OfficeCLI","slug":"anchor-sheet-asegs-0-must-match-target-sheet-00255f","errorCode":null,"errorMessage":"Anchor sheet '{aSegs[0]}' must match target sheet '{targetSheetName}'","messagePattern":"Anchor sheet '(.+?)' must match target sheet '(.+?)'","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":320,"sourceCode":"\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\n            // its current position.\n            int? targetIndex = index;\n            string targetSheetName = string.IsNullOrEmpty(targetParentPath)\n                ? sheetName\n                : targetParentPath.TrimStart('/').Split('/', 2)[0];\n            if (targetIndex == null && position != null && (position.After != null || position.Before != null))\n            {\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.","sourceCodeStart":302,"sourceCodeEnd":338,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L302-L338","documentation":"Thrown by the row-anchor resolver when the anchor's sheet segment does not match the target sheet (aSegs[0] != targetSheetName, case-insensitive). Cross-sheet row anchors are rejected: the anchor row must live in the same sheet the row is being moved into, otherwise the resolved position would be meaningless.","triggerScenarios":"Moving into Sheet2 but passing InsertPosition.AfterElement(\"/Sheet1/row[2]\"); targetParentPath names a different sheet than the anchor; anchor copied from the source sheet while moving cross-sheet.","commonSituations":"Cross-sheet move where the user kept the source sheet in the anchor; refactoring a same-sheet call to cross-sheet without updating the anchor.","solutions":["Make the anchor's sheet equal the target sheet (or omit targetParentPath for a same-sheet move and anchor within the source sheet).","Derive targetSheetName from targetParentPath and build the anchor from it.","Use InsertPosition.AtIndex(n) to avoid anchor-sheet coupling entirely."],"exampleFix":"// before\nh.Move(\"/Src/row[1]\", \"/Dst\", InsertPosition.AfterElement(\"/Src/row[2]\")); // anchor sheet != target\n// after\nh.Move(\"/Src/row[1]\", \"/Dst\", InsertPosition.AfterElement(\"/Dst/row[2]\"));","handlingStrategy":"validation","validationCode":"string targetSheet = string.IsNullOrEmpty(targetParentPath)\n    ? sourcePath.TrimStart('/').Split('/', 2)[0]\n    : targetParentPath.TrimStart('/').Split('/', 2)[0];\nforeach (var anchor in new[] { pos?.After, pos?.Before }.Where(a => a != null)!)\n{\n    var aSheet = anchor.TrimStart('/').Split('/', 2)[0];\n    if (!aSheet.Equals(targetSheet, StringComparison.OrdinalIgnoreCase))\n        throw new InvalidOperationException($\"Anchor sheet '{aSheet}' must equal target '{targetSheet}'\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute targetSheet from targetParentPath and build anchors from it.","For same-sheet moves leave targetParentPath null so the default sheet matches.","Use AtIndex(n) to sidestep anchor-sheet coupling."],"tags":["excel","openxml","move","row-move","anchor","cross-sheet"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}