iOfficeAI/OfficeCLI · error · InvalidOperationException

Worksheet missing

Error message

Worksheet missing

What it means

InvalidOperationException thrown in PivotTableHelper.ReadSourceData when the supplied WorksheetPart has a null Worksheet. This is an internal-state/precondition failure: the pivot cache builder was handed a worksheet part whose underlying Worksheet DOM object is missing, so it cannot read the source cell range.

Source

Thrown at src/officecli/Core/PivotTableHelper.Cache.cs:258

    private static string MonthShortName(int month)
        => month switch
        {
            1  => "Jan", 2  => "Feb", 3  => "Mar", 4  => "Apr",
            5  => "May", 6  => "Jun", 7  => "Jul", 8  => "Aug",
            9  => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec",
            _  => month.ToString(System.Globalization.CultureInfo.InvariantCulture),
        };

    private static string CapitalizeFirst(string s)
        => string.IsNullOrEmpty(s) ? s : char.ToUpperInvariant(s[0]) + s.Substring(1);

    // ==================== Source Data Reader ====================

    private static (string[] headers, List<string[]> columnData, uint?[] columnStyleIds) ReadSourceData(
        WorksheetPart sourceSheet, string sourceRef)
    {
        var ws = sourceSheet.Worksheet ?? throw new InvalidOperationException("Worksheet missing");
        var sheetData = ws.GetFirstChild<SheetData>();
        if (sheetData == null) return (Array.Empty<string>(), new List<string[]>(), Array.Empty<uint?>());

        // Parse range "A1:D100"
        var parts = sourceRef.Replace("$", "").Split(':');
        if (parts.Length != 2) throw new ArgumentException($"Invalid source range: {sourceRef}");

        var (startCol, startRow) = ParseCellRef(parts[0]);
        var (endCol, endRow) = ParseCellRef(parts[1]);

        var startColIdx = ColToIndex(startCol);
        var endColIdx = ColToIndex(endCol);
        // R6-3: reject columns beyond Excel's hard max (XFD = 16384). Previously
        // XFE / XFZ / ZZZZ silently parsed into oversized indices, produced a
        // giant colCount, and either crashed deep in the renderer or wrote an
        // invalid source range into the cache.
        const int ExcelMaxColumn = 16384; // XFD
        if (startColIdx > ExcelMaxColumn)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the source worksheet exists and is a valid sheet in the workbook before creating/refreshing the pivot table.
  2. Open and repair the workbook in Excel, then retry.
  3. Confirm sourceRef targets an existing, populated sheet (not a deleted/renamed one).
Defensive patterns

Strategy: type-guard

Validate before calling

var ws = sourceSheet.Worksheet;
if (ws == null) { /* report: source worksheet part is missing its <worksheet>; repair file */ }

Type guard

static bool HasValidWorksheet(DocumentFormat.OpenXml.Packaging.WorksheetPart p) => p?.Worksheet != null;

Prevention

When it happens

Trigger: Creating/refreshing a pivot table (or computing per-column style ids) against a WorksheetPart whose .Worksheet is null — typically a corrupted, empty, or not-yet-loaded sheet part inside the workbook package.

Common situations: Operating on a workbook where the source sheet was deleted/corrupted but the part reference remains; an incompletely-saved file; pointing sourceRef at a sheet whose part exists but contains no <worksheet> root.

Related errors


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