iOfficeAI/OfficeCLI · error · ArgumentException
Anchor row {anchorRowIdx} not found in {targetSheetName}
Error message
Anchor row {anchorRowIdx} not found in {targetSheetName} What it means
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.
Source
Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.cs:330
{
int FindAnchorRowPos(string anchorPath)
{
var aSegs = anchorPath.TrimStart('/').Split('/', 2);
if (aSegs.Length < 2)
throw new ArgumentException(
$"Anchor must be a row path like /{targetSheetName}/row[K], got: {anchorPath}");
if (!aSegs[0].Equals(targetSheetName, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(
$"Anchor sheet '{aSegs[0]}' must match target sheet '{targetSheetName}'");
var am = Regex.Match(aSegs[1], @"^row\[(\d+)\]$");
if (!am.Success)
throw new ArgumentException(
$"Anchor must be a row path like /{targetSheetName}/row[K], got: {anchorPath}");
var anchorRowIdx = uint.Parse(am.Groups[1].Value);
var pos = targetSheetData.Elements<Row>().ToList()
.FindIndex(r => r.RowIndex?.Value == anchorRowIdx);
if (pos < 0)
throw new ArgumentException($"Anchor row {anchorRowIdx} not found in {targetSheetName}");
return pos;
}
if (position.Before != null) targetIndex = FindAnchorRowPos(position.Before);
else targetIndex = FindAnchorRowPos(position.After!) + 1;
}
// If the moved row sits before the anchor in the same sheet,
// removing it shifts everything (including the anchor) up by one.
// Adjust the resolved target index so it still points at the
// intended slot in post-remove document order.
if (targetIndex.HasValue && targetSheetData == sheetData)
{
var srcPos = sheetData.Elements<Row>().ToList().IndexOf(row);
if (srcPos >= 0 && srcPos < targetIndex.Value)
targetIndex = targetIndex.Value - 1;
}
// Snapshot every row's old RowIndex (per sheet) so we can buildView on GitHub (pinned to 1ced45e900)
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.
Example fix
// before
h.Move("/Src/row[1]", "/Dst", InsertPosition.AfterElement("/Dst/row[9]")); // Dst has no row 9
// after
h.Move("/Src/row[1]", "/Dst", InsertPosition.AfterElement("/Dst/row[2]")); // Dst has row 2 Defensive patterns
Strategy: validation
Validate before calling
var am = System.Text.RegularExpressions.Regex.Match(anchor.Split('/', 2)[1], @"^row\[(\d+)\]$");
var k = uint.Parse(am.Groups[1].Value);
var targetRows = handler.Get(targetSheet, depth: 2).Children.Where(c => c.Type == "row").ToList();
if (!targetRows.Any(r => r.Name.EndsWith($"[{k}]") /* or match the stored index */ ))
throw new InvalidOperationException($"Anchor row {k} not present in {targetSheet}"); Try / catch
try { handler.Move(src, target, pos); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Anchor row") && ex.Message.Contains("not found"))
{ /* pick an anchor row that exists in the target sheet */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Anchor row removed in a prior edit; referencing a visually-present but un-stored empty row; target sheet smaller than the source.
Related errors
- After anchor not found: {position.After}
- Before anchor not found: {position.Before}
- Target sheet not found: {tgtSegments[0]}
- Row {rowIdx} not found
- Anchor must be a row path like /{targetSheetName}/row[K], go
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/7a001328be28cc62.
Report an issue: GitHub.