iOfficeAI/OfficeCLI · error · ArgumentException

Picture[{picIndex}] not found in sheet '{sheetNameFromPath}'

Error message

Picture[{picIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).

What it means

Thrown for /Sheet/picture[N] when GetPictureNode returns null for that 1-based index (out of range, including picture[0]). Previously a bare '!' dereferenced the null and leaked an opaque NullReferenceException; now it surfaces a clear not-found message consistent with sibling element types.

Source

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

            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
            var shpMatch = Regex.Match(cellRef, @"^shape\[(\d+)\]$", RegexOptions.IgnoreCase);
            if (shpMatch.Success)
            {
                var shpIndex = int.Parse(shpMatch.Groups[1].Value);
                // Same null-leak as picture[N] above.
                return GetShapeNode(sheetNameFromPath, worksheet, shpIndex, path)
                    ?? throw new ArgumentException(
                        $"Shape[{shpIndex}] not found in sheet '{sheetNameFromPath}' (indices are 1-based).");
            }


            // If it looks like it could be a malformed cell reference (digits only, etc.), reject it
            if (Regex.IsMatch(cellRef, @"^\d+$"))
                throw new ArgumentException($"Invalid cell reference: '{cellRef}'. Expected format like 'A1', 'B2'.");

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index within the picture count.
  2. Confirm pictures exist on the sheet before indexing.
  3. try/catch(ArgumentException) and degrade to 'no picture'.

Example fix

// before
var pic = handler.Get("/Sheet1/picture[5]"); // throws if <5 pictures

// after
try { var pic = handler.Get("/Sheet1/picture[5]"); }
catch (ArgumentException) { /* picture index invalid */ }
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/picture[5]"); }
catch (ArgumentException) { /* picture index invalid */ return null; }

Prevention

When it happens

Trigger: handler.Get("/Sheet1/picture[5]") on a sheet with fewer than 5 pictures. picture[0]. A sheet with no pictures.

Common situations: Hard-coded picture index after images were added or removed. Zero-based indexing. Wrong sheet.

Related errors


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