iOfficeAI/OfficeCLI · error · ArgumentException

Sheet '{rangeSheetName}' has no data

Error message

Sheet '{rangeSheetName}' has no data

What it means

After resolving the sheet for the chart data range, ParseDataRangeForChart reads GetSheet(ws).GetFirstChild<SheetData>(); if that element is absent (a worksheet with no <sheetData> child at all) it throws. A sheet that merely has empty rows still carries a <sheetData/>, so this specifically means the element is missing -- e.g. a freshly added sheet never written to.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Chart.cs:449

        }

        // Strip any $ signs for parsing
        var cleanRange = rangePart.Replace("$", "");
        var rangeParts = cleanRange.Split(':');
        if (rangeParts.Length != 2)
            throw new ArgumentException($"Invalid dataRange: '{dataRange}'. Expected format: 'Sheet1!A1:D5', 'A1:B3', or a defined-name");

        var (startCol, startRow) = ParseCellReference(rangeParts[0]);
        var (endCol, endRow) = ParseCellReference(rangeParts[1]);
        var startColIdx = ColumnNameToIndex(startCol);
        var endColIdx = ColumnNameToIndex(endCol);

        // Find the worksheet and read cells
        var ws = FindWorksheet(rangeSheetName)
            ?? throw new ArgumentException($"Sheet not found: {rangeSheetName}");
        var sheetData = GetSheet(ws).GetFirstChild<SheetData>();
        if (sheetData == null)
            throw new ArgumentException($"Sheet '{rangeSheetName}' has no data");

        // Build cell lookup. Track value, the originating Cell (for DataType),
        // and a "is blank" flag for cells that exist but carry no value.
        // R20-03: blank-vs-zero distinction is needed for dispBlanksAs=gap.
        // R20-04: DataType drives header detection — only string-typed
        // first-row cells are treated as series names.
        var cellLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        var cellTypeLookup = new Dictionary<string, Cell>(StringComparer.OrdinalIgnoreCase);
        var cellPresent = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        foreach (var row in sheetData.Elements<Row>())
        {
            var rowIdx = (int)(row.RowIndex?.Value ?? 0);
            if (rowIdx < startRow || rowIdx > endRow) continue;
            foreach (var cell in row.Elements<Cell>())
            {
                if (cell.CellReference?.Value != null)
                {
                    cellLookup[cell.CellReference.Value] = GetCellDisplayValue(cell);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Write data to the sheet first, then add the chart.
  2. Point dataRange at a populated sheet.
  3. Confirm the sheet has a SheetData element / at least one row before charting.

Example fix

// before
handler.AddSheet("Chart1");
handler.AddChart("Chart1", dataRange: "Chart1!A1:D5"); // no sheetData -> throw

// after
handler.AddSheet("Chart1");
handler.Set("/Chart1/A1", "Q1"); // forces <sheetData> to exist
handler.AddChart("Chart1", dataRange: "Chart1!A1:D5");
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the data sheet is populated before charting.
if (!handler.SheetHasData(dataSheetName)) // your helper: presence of <sheetData>
    throw new InvalidOperationException($"Write data to '{dataSheetName}' before adding a chart.");

Prevention

When it happens

Trigger: AddSheet then immediately AddChart before writing any cells; charting against a template sheet whose sheetData was stripped; pointing dataRange at a brand-new empty sheet.

Common situations: Programmatic creation ordering (sheet created but not populated); importing a skeleton workbook with placeholder sheets.

Related errors


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