iOfficeAI/OfficeCLI · error · InvalidOperationException
Workbook is missing
Error message
Workbook is missing
What it means
InvalidOperationException thrown when registering a new pivotCache in workbook.xml but workbookPart.Workbook is null. This is an internal-state/precondition failure: the cache-registration step needs the <workbook> root to append a <pivotCaches> entry, and the document is missing or corrupted at that level.
Source
Thrown at src/officecli/Core/PivotTableHelper.Cache.cs:1057
// Clone the records part (if present) under the new cache part.
var origRecords = original.GetPartsOfType<PivotTableCacheRecordsPart>().FirstOrDefault();
if (origRecords != null)
{
var cloneRecords = clone.AddNewPart<PivotTableCacheRecordsPart>();
using (var src = origRecords.GetStream(FileMode.Open, FileAccess.Read))
using (var dst = cloneRecords.GetStream(FileMode.Create, FileAccess.Write))
{
src.CopyTo(dst);
}
// Re-point the cacheDef's r:id to the new records part.
if (clone.PivotCacheDefinition != null)
clone.PivotCacheDefinition.Id = clone.GetIdOfPart(cloneRecords);
}
// Register a new <pivotCache> entry in workbook.xml with a fresh cacheId.
var wb = workbookPart.Workbook
?? throw new InvalidOperationException("Workbook is missing");
var pivotCaches = wb.GetFirstChild<PivotCaches>();
if (pivotCaches == null)
{
pivotCaches = new PivotCaches();
var insertBefore = wb.GetFirstChild<WebPublishing>()
?? wb.GetFirstChild<FileRecoveryProperties>()
?? (OpenXmlElement?)wb.GetFirstChild<WebPublishObjects>();
if (insertBefore != null)
wb.InsertBefore(pivotCaches, insertBefore);
else
wb.AppendChild(pivotCaches);
}
uint newCacheId = pivotCaches.Elements<PivotCache>()
.Select(pc => pc.CacheId?.Value ?? 0u).DefaultIfEmpty(0u).Max() + 1;
pivotCaches.AppendChild(new PivotCache
{
CacheId = newCacheId,
Id = workbookPart.GetIdOfPart(clone)View on GitHub (pinned to 1ced45e900)
Solutions
- Open and repair the workbook in Excel, then retry the pivot operation.
- Verify the package is a valid xlsx with an intact workbook.xml before operating.
- Re-create the workbook from a known-good source if the structure is unrecoverable.
Defensive patterns
Strategy: type-guard
Validate before calling
var wb = workbookPart.Workbook;
if (wb == null) { /* report: workbook.xml root missing; repair or rebuild the package */ } Type guard
static bool HasWorkbook(DocumentFormat.OpenXml.Packaging.WorkbookPart p) => p?.Workbook != null;
Prevention
- Validate WorkbookPart.Workbook is non-null before pivot cache registration.
- Repair malformed xlsx packages in Excel before operating.
- Re-create the workbook from a known-good source if the root is missing.
When it happens
Trigger: Cloning/registering a pivot cache (e.g. on pivot create/copy) against a SpreadsheetDocument whose WorkbookPart.Workbook is null — a package missing its workbook.xml root or with a broken package structure.
Common situations: Operating on a corrupted or incompletely-generated workbook; a file whose workbook.xml was stripped; a malformed package assembled by another tool.
Related errors
- Worksheet missing
- pivotCacheDefinition is missing <cacheFields>
- pivotTableDefinition is missing <pivotFields>
- Invalid source range: {sourceRef}
- Column {startCol} out of range (max: XFD)
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/7bd491559edc9727.
Report an issue: GitHub.