iOfficeAI/OfficeCLI · error · ArgumentException

Unrecognized cell parent path segment '{cellSegments[1]}'. E

Error message

Unrecognized cell parent path segment '{cellSegments[1]}'. Expected a cell reference (e.g. A2), cell[A2], or row[N].

What it means

AddCell's parent path may carry a tail segment that is a bare cell-ref (e.g. A2), cell[<ref>] (e.g. cell[A2]), or row[N] (e.g. row[5]). Any other tail shape (r[2], foo[2], xyz[5]) matches none of the three accepted forms, leaves both the cell-ref and row-index unresolved, and previously caused a silent snap to row 1. This guard rejects it explicitly so data is not mislaced without warning. A bare /Sheet1 (no tail) is unaffected — that is a legitimate auto-append target.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Cells.cs:380

        {
            var rowPathMatch = Regex.Match(cellSegments[1], @"^row\[(\d+)\]$", RegexOptions.IgnoreCase);
            if (rowPathMatch.Success)
                rowIndexFromPath = uint.Parse(rowPathMatch.Groups[1].Value);
        }

        // BUG-R2: an unrecognized parent path tail (e.g. r[2], foo[2], xyz[5])
        // matched none of the three accepted forms above (bare cell-ref,
        // cell[<ref>], row[N]), leaving both cellRefFromPath and rowIndexFromPath
        // null. The auto-assign branch below then silently snapped to row 1,
        // misplacing data without warning. Reject it instead. (Bare /Sheet1 with
        // cellSegments.Length == 1 is unaffected — that's a legitimate
        // auto-append target.) No r[N] alias is added: Get/Query don't support
        // r[N], so accepting it on Add would break Add/Get symmetry.
        if (cellSegments.Length > 1
            && cellRefFromPath == null
            && rowIndexFromPath == null)
        {
            throw new ArgumentException(
                $"Unrecognized cell parent path segment '{cellSegments[1]}'. " +
                $"Expected a cell reference (e.g. A2), cell[A2], or row[N].");
        }

        string cellRef;
        // BUG-R36-B1: when --prop arrayformula= is supplied with --prop ref=A1:C3,
        // the range is the spill region, not a single cell address. Detect it and
        // resolve cellRef to the top-left so FindOrCreateCell doesn't reject the
        // colon. The full range is still passed through to arrayformula below via
        // properties["ref"].
        string? arrayFormulaRefRange = null;
        if (properties.ContainsKey("ref"))
        {
            cellRef = properties["ref"];
            if (cellRef.Contains(':') && properties.ContainsKey("arrayformula"))
            {
                arrayFormulaRefRange = cellRef;
                var topLeft = cellRef.Split(':', 2)[0];

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use one of the three accepted tail forms: a bare cell reference (/Sheet1/A2), cell[<ref>] (/Sheet1/cell[A2]), or row[N] (/Sheet1/row[5]).
  2. If you want auto-append within a specific row, use /Sheet1/row[N] and let AddCell pick the next free column.
  3. For a bare sheet auto-append target, omit the tail entirely: /Sheet1.

Example fix

// before
handler.Add("/Sheet1/r[2]", "cell", null, new() { ["value"] = "x" });
// after
handler.Add("/Sheet1/row[2]", "cell", null, new() { ["value"] = "x" });
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidCellParentTail(string path)
{
    var seg = path.TrimStart('/').Split('/', 2);
    if (seg.Length == 1) return true; // bare sheet = auto-append
    var t = seg[1];
    return Regex.IsMatch(t, @"^[A-Z]+\d+$", RegexOptions.IgnoreCase)
        || Regex.IsMatch(t, @"^cell\[[A-Z]+\d+\]$", RegexOptions.IgnoreCase)
        || Regex.IsMatch(t, @"^row\[\d+]$", RegexOptions.IgnoreCase);
}
if (!IsValidCellParentTail(parentPath)) throw new ArgumentException("Bad cell parent tail.");

Type guard

static bool IsAcceptedCellTail(string t) =>
    Regex.IsMatch(t, @"^[A-Z]+\d+$", RegexOptions.IgnoreCase)
    || Regex.IsMatch(t, @"^cell\[[A-Z]+\d+\]$", RegexOptions.IgnoreCase)
    || Regex.IsMatch(t, @"^row\[\d+]$", RegexOptions.IgnoreCase);

Try / catch

try { h.Add(parentPath, "cell", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Unrecognized cell parent path segment"))
{ /* rewrite the tail to an accepted form */ }

Prevention

When it happens

Trigger: Add("/Sheet1/r[2]","cell",...); parentPath tail like "foo[5]", "xyz[2]", "cell5", or "row" (no brackets); any bracketed form whose prefix is not cell/row and whose content is not a bare A1 ref.

Common situations: Confusing row[N] with an r[N] alias (Get/Query do not support r[N], so Add does not either to keep symmetry); copy-pasting a path from another element type; templating code that builds the tail from a variable that produced an unexpected shape.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/2b1a23d55de435a7. Report an issue: GitHub.