{"record":{"id":"28a21145cd3975fd","repo":"iOfficeAI/OfficeCLI","slug":"anchor-sheet-asegs-0-must-match-source-sheet","errorCode":null,"errorMessage":"Anchor sheet '{aSegs[0]}' must match source sheet '{sheetName}'","messagePattern":"Anchor sheet '(.+?)' must match source sheet '(.+?)'","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":438,"sourceCode":"        {\n            var srcColLetter = colMatch.Groups[1].Value.ToUpperInvariant();\n            var srcColIdx = ColumnNameToIndex(srcColLetter);\n\n            // Resolve target. Default behavior (no position): append after the\n            // last used column.\n            int? targetColIdx = null;\n            if (position?.Index.HasValue == true)\n                targetColIdx = position.Index!.Value;\n            else if (position?.Before != null || position?.After != null)\n            {\n                int FindAnchorColIdx(string anchorPath)\n                {\n                    var aSegs = anchorPath.TrimStart('/').Split('/', 2);\n                    if (aSegs.Length < 2)\n                        throw new ArgumentException(\n                            $\"Anchor must be a col path like /{sheetName}/col[L], got: {anchorPath}\");\n                    if (!aSegs[0].Equals(sheetName, StringComparison.OrdinalIgnoreCase))\n                        throw new ArgumentException(\n                            $\"Anchor sheet '{aSegs[0]}' must match source sheet '{sheetName}'\");\n                    var am = Regex.Match(aSegs[1], @\"^col\\[([A-Za-z]+)\\]$\", RegexOptions.IgnoreCase);\n                    if (!am.Success)\n                        throw new ArgumentException(\n                            $\"Anchor must be a col path like /{sheetName}/col[L], got: {anchorPath}\");\n                    return ColumnNameToIndex(am.Groups[1].Value.ToUpperInvariant());\n                }\n                if (position.Before != null) targetColIdx = FindAnchorColIdx(position.Before);\n                else targetColIdx = FindAnchorColIdx(position.After!) + 1;\n            }\n            else\n            {\n                // Append after last used column.\n                int maxCol = 1;\n                foreach (var r in sheetData.Elements<Row>())\n                    foreach (var c in r.Elements<Cell>())\n                        if (c.CellReference?.Value != null)\n                            maxCol = Math.Max(maxCol, ColumnNameToIndex(ParseCellReference(c.CellReference.Value).Column));","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L420-L456","documentation":"Thrown by the column-anchor resolver when the anchor's sheet segment does not equal the source sheet (aSegs[0] != sheetName). Column moves are within-sheet only, so the anchor column must belong to the same sheet being modified; a cross-sheet column anchor is rejected.","triggerScenarios":"Moving columns in Sheet1 but passing InsertPosition.AfterElement(\"/Sheet2/col[A]\"); anchor copied from another sheet; sheet rename leaving the anchor stale.","commonSituations":"Refactor that changed which sheet is operated on without updating the anchor; templated anchor string from a different sheet.","solutions":["Set the anchor's sheet to the same sheet as the moved column.","Build the anchor from the live sheetName variable.","Use InsertPosition.AtIndex(n) to avoid sheet coupling."],"exampleFix":"// before\nh.Move(\"/Sheet1/col[B]\", null, InsertPosition.BeforeElement(\"/Sheet2/col[A]\"));\n// after\nh.Move(\"/Sheet1/col[B]\", null, InsertPosition.BeforeElement(\"/Sheet1/col[A]\"));","handlingStrategy":"validation","validationCode":"var sheetName = sourcePath.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(sheetName, StringComparison.OrdinalIgnoreCase))\n        throw new InvalidOperationException($\"Column anchor sheet '{aSheet}' must equal source '{sheetName}'\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep column anchors in the same sheet as the moved column.","Derive the anchor sheet from the same sheetName used for the move.","Use AtIndex(n) to avoid sheet-name coupling."],"tags":["excel","openxml","move","col-move","anchor","cross-sheet"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}