{"record":{"id":"680c5e06d2bb6c90","repo":"iOfficeAI/OfficeCLI","slug":"calculatedfield-name-requires-a-non-empty-form","errorCode":null,"errorMessage":"calculatedField '{name}' requires a non-empty formula","messagePattern":"calculatedField '(.+?)' requires a non-empty formula","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/PivotTableHelper.Definition.cs","lineNumber":1536,"sourceCode":"        {\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\n        // the rest of the table. Excel rejects a pivotTable where some\n        // pivotFields declare compact=\"0\" outline=\"0\" but later ones omit them.\n        var templatePf = pivotFields.Elements<PivotField>().FirstOrDefault();\n        bool templateCompactFalse = templatePf?.Compact?.Value == false;\n        bool templateOutlineFalse = templatePf?.Outline?.Value == false;\n\n        foreach (var (name, formula) in specs)\n        {\n            if (string.IsNullOrWhiteSpace(name))\n                throw new ArgumentException(\"calculatedField requires a non-empty name\");\n            if (string.IsNullOrWhiteSpace(formula))\n                throw new ArgumentException($\"calculatedField '{name}' requires a non-empty formula\");\n            if (existingNames.Contains(name))\n                throw new ArgumentException(\n                    $\"calculatedField '{name}' collides with an existing field name\");\n            existingNames.Add(name);\n\n            // 1. cacheField\n            var cleanFormula = formula.TrimStart('=').Trim();\n            var cacheField = new CacheField\n            {\n                Name = name,\n                Formula = cleanFormula,\n                DatabaseField = false,\n                NumberFormatId = 0u\n            };\n            cacheFields.AppendChild(cacheField);\n\n            // New field index = position of the freshly-appended cacheField.\n            var newFieldIdx = (uint)(cacheFields.Elements<CacheField>().Count() - 1);","sourceCodeStart":1518,"sourceCodeEnd":1554,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/PivotTableHelper.Definition.cs#L1518-L1554","documentation":"Thrown in the same calculated-field loop when the name is valid but the formula is null, empty, or whitespace. A calculated field with no formula is meaningless to Excel and would produce a corrupt cacheField, so the helper rejects it before appending anything to the package. Note the cleaner does TrimStart('=') later — that step does not excuse an empty input, it only normalizes a leading '='.","triggerScenarios":"calculatedField=\"Margin:\" (bare prop with nothing after colon), calculatedFields=[{\"name\":\"Margin\",\"formula\":\"\"}], or a formula that is only whitespace like calculatedField=\"Margin:   \".","commonSituations":"Formula string built from a missing config value; user copy-paste that dropped the formula half; a generated spec where the formula variable was never assigned.","solutions":["Provide the formula after the colon: calculatedField=Margin:=Sales-Cost","In JSON, ensure 'formula' is a non-empty expression: [{\"name\":\"Margin\",\"formula\":\"=Sales-Cost\"}]","Validate that formula has real content (not just '=' or spaces) before calling the helper"],"exampleFix":"// before\ncalculatedField=\"Margin:\"\n// after\ncalculatedField=\"Margin:=Sales-Cost\"","handlingStrategy":"validation","validationCode":"var specs = rawSpecs.Where(s => !string.IsNullOrWhiteSpace(s.Formula)).ToList();\nif (rawSpecs.Count != specs.Count)\n    throw new InvalidOperationException(\"One or more calculated field formulas are empty\");","typeGuard":"static bool IsValidCalcFormula(string? formula) =>\n    !string.IsNullOrWhiteSpace(formula);","tryCatchPattern":"try { AddCalculatedFields(props); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"non-empty formula\"))\n{ /* prompt user for the formula body */ }","preventionTips":["Confirm every spec has a non-empty formula in your own layer","Treat a formula that is just '=' or whitespace as invalid","Use strongly-typed records with non-nullable fields for name and formula"],"tags":["pivottable","calculated-field","argument-validation","input-parsing"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}