iOfficeAI/OfficeCLI · error · System.ArgumentException

Parent path must be /SheetName/CellRef for adding a run

Error message

Parent path must be /SheetName/CellRef for adding a run

What it means

Thrown by AddRun when the parentPath does not contain at least two segments after trimming the leading slash. A rich-text run must be attached to a specific cell, so the path must be /SheetName/CellRef (e.g. /Sheet1/A1). Splitting on '/' with a limit of 2 yields fewer than 2 segments only when the path is just a sheet name with no cell reference.

Source

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

            {
                newCol.Collapsed = addColCollapsed.Equals("true", StringComparison.OrdinalIgnoreCase)
                    || addColCollapsed == "1" || addColCollapsed.Equals("yes", StringComparison.OrdinalIgnoreCase);
            }
            if (existingCol == null)
                columns.AppendChild(newCol);
        }

        SaveWorksheet(colWorksheet);
        return $"/{colSheetName}/col[{insertColName}]";
    }

    private string AddRun(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        // Add a rich text run to a cell: parentPath = /SheetName/CellRef
        var runSegments = parentPath.TrimStart('/').Split('/', 2);
        if (runSegments.Length < 2)
            throw new ArgumentException("Parent path must be /SheetName/CellRef for adding a run");
        var runSheetName = runSegments[0];
        var runCellRef = runSegments[1].ToUpperInvariant();
        var runWorksheet = FindWorksheet(runSheetName)
            ?? throw new ArgumentException($"Sheet not found: {runSheetName}");
        var runSheetData = GetSheet(runWorksheet).GetFirstChild<SheetData>()
            ?? GetSheet(runWorksheet).AppendChild(new SheetData());
        var runCell = FindOrCreateCell(runSheetData, runCellRef);

        var runWbPart = _doc.WorkbookPart
            ?? throw new InvalidOperationException("Workbook not found");
        var runSstPart = runWbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault()
            ?? runWbPart.AddNewPart<SharedStringTablePart>();
        SharedStringTable runSst;
        if (runSstPart.SharedStringTable != null)
            runSst = runSstPart.SharedStringTable;
        else
        {
            runSst = new SharedStringTable();

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Append the target cell reference to the path: /SheetName/A1.
  2. If you meant to set the whole cell as rich text (not add a run to an existing cell), use type=richtext with runs=... on a cell path instead.
  3. Construct the path from a verified sheet name and a valid A1-style cell reference.

Example fix

// before
handler.Add("/Sheet1", "run", pos, props);

// after
handler.Add("/Sheet1/A1", "run", pos, props);
Defensive patterns

Strategy: validation

Validate before calling

string parent = "/Sheet1/A1";
var segs = parent.TrimStart('/').Split('/', 2);
if (segs.Length < 2 || string.IsNullOrEmpty(segs[1]))
    throw new InvalidOperationException("Run parent path must be /Sheet/CellRef");
handler.Add(parent, "run", pos, props);

Type guard

static bool IsCellPath(string path)
{
    var segs = path.TrimStart('/').Split('/', 2);
    return segs.Length >= 2 && !string.IsNullOrEmpty(segs[1])
        && System.Text.RegularExpressions.Regex.IsMatch(segs[1], @"^[A-Za-z]+[0-9]+$");
}

Prevention

When it happens

Trigger: Calling Add with type=run (or richtext routed here) and parentPath="/Sheet1" or parentPath="/Sheet1/". Also triggered by a bare path like "Sheet1" with no cell. Any path whose second segment is empty after the split still passes (Length is 2), so this specific throw needs a path that is a single segment.

Common situations: User omits the cell reference, intending to add a run to a whole sheet or the first cell. Passing a sheet-scoped path that was copied from a sheet-level command (like add col or add rowbreak). Reusing a parentPath variable built for a different element type.

Related errors


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