{"record":{"id":"27abc1d36e323f5f","repo":"iOfficeAI/OfficeCLI","slug":"pivottabledefinition-is-missing-pivotfields","errorCode":null,"errorMessage":"pivotTableDefinition is missing <pivotFields>","messagePattern":"pivotTableDefinition is missing <pivotFields>","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Definition.cs","lineNumber":1506,"sourceCode":"    // under pivotCacheDefinition, and Excel rejects the file as schema-\n    // invalid). The cacheField/@formula attribute alone fully expresses\n    // the calculated field; Excel rebuilds the column live on open.\n    //\n    // No records are written for calculated fields (databaseField=\"0\"),\n    // matching the date-group-derived pattern — Excel computes the column\n    // live from the formula when the workbook opens.\n    internal static void ApplyCalculatedFields(\n        PivotCacheDefinition cacheDef,\n        PivotTableDefinition pivotDef,\n        Dictionary<string, string> properties)\n    {\n        var specs = ParseCalculatedFieldSpecs(properties);\n        if (specs.Count == 0) return;\n\n        var cacheFields = cacheDef.GetFirstChild<CacheFields>()\n            ?? throw new InvalidOperationException(\"pivotCacheDefinition is missing <cacheFields>\");\n        var pivotFields = pivotDef.PivotFields\n            ?? throw new InvalidOperationException(\"pivotTableDefinition is missing <pivotFields>\");\n\n        // Collect existing names (in both cacheFields and calculated specs)\n        // so we can reject duplicates cleanly.\n        var existingNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);\n        foreach (var cf in cacheFields.Elements<CacheField>())\n            if (!string.IsNullOrEmpty(cf.Name?.Value))\n                existingNames.Add(cf.Name!.Value!);\n\n        // Ensure <dataFields> exists so we can append to it.\n        var dataFields = pivotDef.DataFields;\n        if (dataFields == null)\n        {\n            dataFields = new DataFields { Count = 0u };\n            pivotDef.DataFields = dataFields;\n        }\n\n        // Mirror layout-dependent attributes (compact/outline) from an existing\n        // source pivotField so the calc fields stay attribute-consistent with","sourceCodeStart":1488,"sourceCodeEnd":1524,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Definition.cs#L1488-L1524","documentation":"InvalidOperationException thrown by ApplyCalculatedFields when the pivotTableDefinition has no <pivotFields> child. Calculated fields add a <dataField> referencing a field, and the definition must already carry its <pivotFields> collection; a missing <pivotFields> root indicates the pivot definition was not fully built.","triggerScenarios":"Adding a calculated field to a pivot table whose PivotTableDefinition lacks a PivotFields element — an incompletely-constructed or corrupted pivot definition.","commonSituations":"Operating on a pivot definition that was never finalized (no field layout generated); a corrupted file missing <pivotFields>; running the calculated-field step before the definition was initialized from the cache.","solutions":["Ensure the pivot table definition is fully built (with <pivotFields> from the source columns) before adding a calculated field.","Create/refresh the pivot table layout against a populated source first.","If the file is corrupted, rebuild the pivot table definition from valid source data."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"var pivotFields = pivotDef.PivotFields;\nif (pivotFields == null) { /* do not add calculated fields; finalize the pivot definition layout first */ }","typeGuard":"static bool PivotHasFields(PivotTableDefinition pd) => pd?.PivotFields != null;","tryCatchPattern":null,"preventionTips":["Ensure the pivot definition has <pivotFields> before adding calculated fields.","Finalize the pivot layout from the source columns first.","Rebuild corrupted pivot definitions from valid source data."],"tags":["pivot-table","excel","ooxml","invalid-operation","precondition","calculated-field","officecli"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}