{"record":{"id":"13a506d9beab3d4b","repo":"iOfficeAI/OfficeCLI","slug":"before-anchor-not-found-position-before","errorCode":null,"errorMessage":"Before anchor not found: {position.Before}","messagePattern":"Before anchor not found: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":206,"sourceCode":"\n            // Resolve after/before anchor BEFORE removing sheetEl.\n            static string ExtractAnchorSheetName(string raw) =>\n                (raw.StartsWith(\"/\") ? raw[1..] : raw).Split('/', 2)[0];\n\n            Sheet? afterAnchor = null, beforeAnchor = null;\n            if (position?.After != null)\n            {\n                var anchorName = ExtractAnchorSheetName(position.After);\n                afterAnchor = sheets.Elements<Sheet>().FirstOrDefault(s =>\n                    string.Equals(s.Name?.Value, anchorName, StringComparison.OrdinalIgnoreCase))\n                    ?? throw new ArgumentException($\"After anchor not found: {position.After}\");\n            }\n            else if (position?.Before != null)\n            {\n                var anchorName = ExtractAnchorSheetName(position.Before);\n                beforeAnchor = sheets.Elements<Sheet>().FirstOrDefault(s =>\n                    string.Equals(s.Name?.Value, anchorName, StringComparison.OrdinalIgnoreCase))\n                    ?? throw new ArgumentException($\"Before anchor not found: {position.Before}\");\n            }\n            else if (index == null)\n            {\n                throw new ArgumentException(\"One of --index, --after, or --before is required when moving a sheet\");\n            }\n\n            // Self-move guard: moving a sheet after/before itself is a no-op.\n            // Removing first detaches sheetEl, then InsertAfterSelf/InsertBeforeSelf\n            // throws \"parent is null\" and the sheet is lost (data loss).\n            if (ReferenceEquals(afterAnchor, sheetEl) || ReferenceEquals(beforeAnchor, sheetEl))\n                return $\"/{sheetName}\";\n\n            // localSheetId on <definedName> is a 0-based position into\n            // <sheets>; capture the pre-move order so scoped names can be\n            // remapped to the sheets' new positions after the reorder.\n            var preMoveOrder = sheets.Elements<Sheet>().ToList();\n\n            sheetEl.Remove();","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L188-L224","documentation":"Thrown when moving a whole sheet with --before where the anchor sheet named in position.Before is not in the <sheets> catalog. Same guard as the --after case: the handler needs a real sibling to insert before, and an unknown anchor name stops the move.","triggerScenarios":"Move(\"/Sheet1\", null, InsertPosition.BeforeElement(\"/Missing\")) where 'Missing' is absent; misspelled anchor; anchor sheet removed after the path was built.","commonSituations":"Stale anchor path; renamed target sheet; referring to a hidden/very-hidden sheet name that differs from the catalog entry.","solutions":["Verify the anchor sheet name against the live <sheets> list before calling Move.","Fall back to InsertPosition.AtIndex(n) for an absolute target.","Re-read the path from the current workbook if sheets were edited in another session."],"exampleFix":"// before\nh.Move(\"/Sheet1\", null, InsertPosition.BeforeElement(\"/Finale\")); // 'Finale' typo for 'Final'\n// after\nh.Move(\"/Sheet1\", null, InsertPosition.BeforeElement(\"/Final\"));","handlingStrategy":"validation","validationCode":"string AnchorSheet(string raw) => (raw.StartsWith(\"/\") ? raw[1..] : raw).Split('/', 2)[0];\nvar sheets = handler.Get(\"/\", depth: 1).Children.Select(c => c.Name).ToList();\nif (pos?.Before is { } b && !sheets.Any(s => s.Equals(AnchorSheet(b), StringComparison.OrdinalIgnoreCase)))\n    throw new InvalidOperationException($\"--before anchor sheet '{b}' does not exist\");","typeGuard":null,"tryCatchPattern":"try { handler.Move(\"/Sheet1\", null, InsertPosition.BeforeElement(anchor)); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Before anchor not found\"))\n{ /* list sheets and let the user pick a valid before-anchor */ }","preventionTips":["Confirm the before-anchor sheet exists in the live <sheets> list.","Re-read anchor names after any sheet rename/delete.","Use AtIndex(n) to avoid anchor lookups entirely."],"tags":["excel","openxml","move","sheet-reorder","anchor","not-found"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}