iOfficeAI/OfficeCLI · error · ArgumentException

Sparkline[{spkIndex}] not found in sheet '{sheetNameFromPath

Error message

Sparkline[{spkIndex}] not found in sheet '{sheetNameFromPath}'

What it means

Thrown for /Sheet/sparkline[N] when GetSparklineGroup returns no group at that 1-based index. Sparkline groups live in the worksheet's extLst under x14:sparklineGroups; with no groups on the sheet, any index (including sparkline[0]) is rejected.

Source

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

            var ccf = tCol.CalculatedColumnFormula?.Text;
            if (!string.IsNullOrEmpty(ccf)) tcNode.Format["formula"] = ccf;
            return tcNode;
        }

        // Cell reference: A1 or range A1:D10
        // Check if it's a cell reference or a generic XML path
        var firstPart = cellRef.Split('/')[0].Split('[')[0];
        bool isCellRef = System.Text.RegularExpressions.Regex.IsMatch(firstPart, @"^[A-Z]+\d+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

        if (!isCellRef)
        {
            // Handle sparkline[N] path segment
            var spkMatch = Regex.Match(cellRef, @"^sparkline\[(\d+)\]$", RegexOptions.IgnoreCase);
            if (spkMatch.Success)
            {
                var spkIndex = int.Parse(spkMatch.Groups[1].Value);
                var spkGroup = GetSparklineGroup(worksheet, spkIndex)
                    ?? throw new ArgumentException($"Sparkline[{spkIndex}] not found in sheet '{sheetNameFromPath}'");
                return SparklineGroupToNode(sheetNameFromPath, spkGroup, spkIndex);
            }

            // Handle picture[N] path segment
            var picMatch = Regex.Match(cellRef, @"^picture\[(\d+)\]$", RegexOptions.IgnoreCase);
            if (picMatch.Success)
            {
                var picIndex = int.Parse(picMatch.Groups[1].Value);
                // GetPictureNode returns null for out-of-range indices (incl.
                // picture[0]); the bare `!` leaked a NullReferenceException as
                // an opaque internal_error instead of the not-found message
                // every sibling element type produces.
                return GetPictureNode(sheetNameFromPath, worksheet, picIndex, path)
                    ?? throw new ArgumentException(
                        $"Picture[{picIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).");
            }

            // Handle shape[N] path segment

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index within the sparkline-group count.
  2. Confirm the sheet has sparkline groups before indexing.
  3. try/catch(ArgumentException) and degrade to 'no sparkline'.

Example fix

// before
var spk = handler.Get("/Sheet1/sparkline[1]"); // throws if no groups

// after
try { var spk = handler.Get("/Sheet1/sparkline[1]"); }
catch (ArgumentException) { /* no sparkline group at that index */ }
Defensive patterns

Strategy: try-catch

Type guard

static int? ElementIndex(string cellRef, string element)
{
    var m = Regex.Match(cellRef, $@"^{Regex.Escape(element)}\[(\d+)$", RegexOptions.IgnoreCase);
    return m.Success && int.TryParse(m.Groups[1].Value, out var i) ? i : null;
}

Try / catch

try { return handler.Get("/Sheet1/sparkline[1]"); }
catch (ArgumentException) { /* no sparkline group at that index */ return null; }

Prevention

When it happens

Trigger: handler.Get("/Sheet1/sparkline[1]") on a sheet with no sparkline groups. sparkline[0]. An index beyond the group count.

Common situations: Assuming sparklines exist on a sheet that has none. Hard-coded index after sparklines were removed. Zero-based indexing.

Related errors


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