iOfficeAI/OfficeCLI · error · ArgumentException

Cell {runCellRef} is not a rich text cell

Error message

Cell {runCellRef} is not a rich text cell

What it means

Thrown in the run[N] path when the target cell exists but is not a rich-text cell: its DataType is not SharedString, or its CellValue is not an integer shared-string index. Rich-text runs only exist for cells that point into the SharedStringTable, so a number, date, boolean, inline-string, or blank cell cannot have addressable runs.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Query.cs:930

            var target = GenericXmlQuery.NavigateByPath(GetSheet(worksheet), xmlSegments);
            if (target == null)
                return new DocumentNode { Path = path, Type = "error", Text = $"Element not found: {cellRef}" };
            return GenericXmlQuery.ElementToNode(target, path, depth);
        }

        // Handle /SheetName/A1/run[N] (rich text run direct access)
        var runGetMatch = Regex.Match(cellRef, @"^([A-Z]+\d+)/run\[(\d+)\]$", RegexOptions.IgnoreCase);
        if (runGetMatch.Success)
        {
            var runCellRef = runGetMatch.Groups[1].Value.ToUpperInvariant();
            var runIdx = int.Parse(runGetMatch.Groups[2].Value);
            ParseCellReference(runCellRef);
            var runCell = FindCell(data, runCellRef);
            if (runCell == null)
                throw new ArgumentException($"Cell {runCellRef} not found");
            if (runCell.DataType?.Value != CellValues.SharedString ||
                !int.TryParse(runCell.CellValue?.Text, out var sstIdx))
                throw new ArgumentException($"Cell {runCellRef} is not a rich text cell");
            var sstPart = _doc.WorkbookPart?.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
            var ssi = sstPart?.SharedStringTable?.Elements<SharedStringItem>().ElementAtOrDefault(sstIdx);
            if (ssi == null) throw new ArgumentException($"SharedString entry {sstIdx} not found");
            var runs = ssi.Elements<Run>().ToList();
            if (runIdx < 1 || runIdx > runs.Count)
                throw new ArgumentException($"Run index {runIdx} out of range (1-{runs.Count})");
            return RunToNode(runs[PathIndex.ToArrayIndex(runIdx)], $"/{sheetNameFromPath}/{runCellRef}/run[{runIdx}]");
        }

        if (cellRef.Contains(':'))
        {
            // Range — validate both endpoints
            var rangeParts = cellRef.Split(':');
            ParseCellReference(rangeParts[0]);
            if (rangeParts.Length > 1) ParseCellReference(rangeParts[1]);
            return GetCellRange(sheetNameFromPath, data, cellRef, depth, worksheet);
        }
        else

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Read the cell directly (get /Sheet1/A1) and check its type/value before requesting runs.
  2. Use a tool/macro to convert the cell to rich text (it must become a SharedString with Run children) before indexing runs.
  3. Target a cell that is genuinely rich text — typically one with mixed font runs.

Example fix

// before
get /Sheet1/A1/run[1]   // A1 is the number 42
// after
get /Sheet1/A1            // read the plain value instead
Defensive patterns

Strategy: type-guard

Validate before calling

# only index runs of shared-string (rich) cells
node = doc.get('/Sheet1/A1')
# the envelope exposes the cell type; rich text comes from SharedString cells
if node.get('type') not in ('sharedstring', 'rich') and not node.get('isRichText'):
    raise SystemExit('cell is not rich text — read its value directly')
doc.get('/Sheet1/A1/run[1]')

Type guard

def is_rich_text_cell(node):
    """A cell is rich-text only if it is a shared-string cell."""
    return bool(node.get('isRichText')) or node.get('type') in ('sharedstring', 'rich')

Try / catch

null

Prevention

When it happens

Trigger: get /Sheet1/A1/run[1] where A1 holds a number (123), a formula result, a date, a boolean, or an inline string. Any cell whose DataType != SharedString or whose value is non-integer hits this guard.

Common situations: Assuming all text cells are rich text (many are plain shared strings with a single run, but numeric/formula cells are not); a file where formatting was cleared so the cell reverted to a plain value.

Related errors


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