iOfficeAI/OfficeCLI · error · ArgumentException

Sheet not found: {chartSheetName}

Error message

Sheet not found: {chartSheetName}

What it means

Thrown by AddChart when the first segment of parentPath (the sheet name) does not resolve to a worksheet via FindWorksheet (case-insensitive). AddChart needs the worksheet both as the data source context (for dataRange/cell-reference qualification) and as the drawing anchor host. Without a resolved worksheet, chart construction cannot proceed.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Add.Chart.cs:27

using OfficeCli.Core;
using C = DocumentFormat.OpenXml.Drawing.Charts;
using Drawing = DocumentFormat.OpenXml.Drawing;
using SpreadsheetDrawing = DocumentFormat.OpenXml.Spreadsheet.Drawing;
using XDR = DocumentFormat.OpenXml.Drawing.Spreadsheet;
using CX = DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing;

namespace OfficeCli.Handlers;

// Per-element-type Add helpers for chart paths and the generic-XML default fallback. Mechanically extracted from the Add() god-method.
public partial class ExcelHandler
{
    private string AddChart(string parentPath, string type, InsertPosition? position, Dictionary<string, string> properties)
    {
        var index = position?.Index;
        var chartSegments = parentPath.TrimStart('/').Split('/', 2);
        var chartSheetName = chartSegments[0];
        var chartWorksheet = FindWorksheet(chartSheetName)
            ?? throw new ArgumentException($"Sheet not found: {chartSheetName}");

        // Parse chart data. Use TryGetValue(case-insensitive) so reads
        // are recorded by TrackingPropertyDictionary in handler-as-truth path.
        string chartType = "column";
        if (properties.TryGetValue("charttype", out var ctVal) || properties.TryGetValue("type", out ctVal))
            chartType = ctVal;
        var chartTitle = properties.GetValueOrDefault("title");

        // Support dataRange: read cell data from worksheet and build series with cell references
        string[]? categories;
        List<(string name, double[] values)> seriesData;
        string? dataRangeStr = null;
        if (properties.TryGetValue("datarange", out var dr) || properties.TryGetValue("range", out dr))
            dataRangeStr = dr;
        ChartRangeGeometry? dataRangeGeometry = null;
        if (!string.IsNullOrEmpty(dataRangeStr))
        {
            (seriesData, categories, dataRangeGeometry) = ParseDataRangeForChart(dataRangeStr, chartSheetName, properties);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use the exact worksheet name that holds the chart's source data as segment[0].
  2. Create the worksheet if it does not exist.
  3. List sheets to confirm names before the call.

Example fix

// before: 'Dashboard' sheet missing
add /Dashboard/A1 chart charttype=column title=Sales
// after
add-sheet Dashboard
add /Dashboard/A1 chart charttype=column title=Sales
Defensive patterns

Strategy: validation

Validate before calling

var sheet = parentPath.TrimStart('/').Split('/', 2)[0];
if (FindWorksheet(sheet) is null)
    throw new ArgumentException($"Sheet '{sheet}' not found.");

Type guard

static bool SheetExists(string? sheetName, IEnumerable<string> known)
    => sheetName is not null && known.Contains(sheetName, StringComparer.OrdinalIgnoreCase);

Try / catch

try { return Add(path, "chart", pos, props); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Sheet not found"))
{ /* correct sheet, retry */ throw; }

Prevention

When it happens

Trigger: Calling Add with parentPath '/<sheet>/...' and type=chart (or a chart element type) where <sheet> is not a worksheet in the workbook. The sheet name is the first path segment after trimming the leading '/'.

Common situations: Stale sheet name; typo; targeting a chart-sheet (which is not a worksheet part) instead of a data worksheet; workbook from a template with localized sheet names.

Related errors


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