{"record":{"id":"4a3f342d1612abe7","repo":"iOfficeAI/OfficeCLI","slug":"cannot-swap-elements-across-different-sheets","errorCode":null,"errorMessage":"Cannot swap elements across different sheets","messagePattern":"Cannot swap elements across different sheets","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Handlers/Excel/ExcelHandler.Add.cs","lineNumber":690,"sourceCode":"            foreach (var cell in row.Elements<Cell>())\n            {\n                if (cell.CellReference?.Value == null) continue;\n                var (col, _) = ParseCellReference(cell.CellReference.Value);\n                cell.CellReference = $\"{col}{newIdx}\";\n            }\n            newIdx++;\n        }\n    }\n\n    public (string NewPath1, string NewPath2) Swap(string path1, string path2)\n    {\n        // Parse both paths: /SheetName/row[N]\n        var seg1 = path1.TrimStart('/').Split('/', 2);\n        var seg2 = path2.TrimStart('/').Split('/', 2);\n        if (seg1.Length < 2 || seg2.Length < 2)\n            throw new ArgumentException(\"Swap requires element paths (e.g. /Sheet1/row[1])\");\n        if (seg1[0] != seg2[0])\n            throw new ArgumentException(\"Cannot swap elements across different sheets\");\n\n        var sheetName = seg1[0];\n        var worksheet = FindWorksheet(sheetName)\n            ?? throw new ArgumentException($\"Sheet not found: {sheetName}\");\n        var sheetData = GetSheet(worksheet).GetFirstChild<SheetData>()\n            ?? throw new ArgumentException(\"Sheet has no data\");\n\n        var rowMatch1 = Regex.Match(seg1[1], @\"^row\\[(\\d+)\\]$\");\n        var rowMatch2 = Regex.Match(seg2[1], @\"^row\\[(\\d+)\\]$\");\n        if (!rowMatch1.Success || !rowMatch2.Success)\n            throw new ArgumentException(\"Swap only supports row[N] elements in Excel\");\n\n        var allRows = sheetData.Elements<Row>().ToList();\n        var idx1 = int.Parse(rowMatch1.Groups[1].Value);\n        var idx2 = int.Parse(rowMatch2.Groups[1].Value);\n        var row1 = (idx1 >= 1 && idx1 <= allRows.Count ? allRows[idx1 - 1] : null)\n            ?? throw new ArgumentException($\"Row {idx1} not found\");\n        var row2 = (idx2 >= 1 && idx2 <= allRows.Count ? allRows[idx2 - 1] : null)","sourceCodeStart":672,"sourceCodeEnd":708,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Handlers/Excel/ExcelHandler.Add.cs#L672-L708","documentation":"Thrown by Swap when the two paths' sheet segments differ (seg1[0] != seg2[0]). Swap only exchanges rows within a single sheet; it does not transport a row across sheets, so a cross-sheet pair is rejected before any worksheet is opened.","triggerScenarios":"Swap(\"/Sheet1/row[1]\", \"/Sheet2/row[1]\"); one path from each of two sheets; sheet rename making previously-equal segments differ.","commonSituations":"User expects Swap to move a row between sheets (it cannot); copy-paste of paths from different sheets; refactoring that changed one sheet but not the other.","solutions":["Ensure both row paths share the same sheet segment.","To relocate a row across sheets, use Move with targetParentPath instead of Swap.","Derive both paths from the same sheetName variable."],"exampleFix":"// before\nh.Swap(\"/Sheet1/row[1]\", \"/Sheet2/row[3]\"); // different sheets\n// after\nh.Move(\"/Sheet1/row[1]\", \"/Sheet2\", InsertPosition.AtIndex(3)); // relocate across sheets via Move","handlingStrategy":"validation","validationCode":"var s1 = path1.TrimStart('/').Split('/', 2)[0];\nvar s2 = path2.TrimStart('/').Split('/', 2)[0];\nif (!s1.Equals(s2, StringComparison.OrdinalIgnoreCase))\n    throw new ArgumentException($\"Swap is single-sheet only ('{s1}' vs '{s2}'); use Move to relocate across sheets\");","typeGuard":"static bool SameSheet(string a, string b) =>\n    a.TrimStart('/').Split('/', 2)[0]\n     .Equals(b.TrimStart('/').Split('/', 2)[0], StringComparison.OrdinalIgnoreCase);","tryCatchPattern":null,"preventionTips":["Derive both swap paths from the same sheetName variable.","Use Move with targetParentPath for cross-sheet row relocation.","Re-check sheet equality after any rename before swapping."],"tags":["excel","openxml","swap","cross-sheet","validation"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}