{"record":{"id":"560b55c35f7126a4","repo":"PHPOffice/PhpSpreadsheet","slug":"you-must-specify-a-formula-value-for-a-named-formu","errorCode":null,"errorMessage":"You must specify a Formula value for a Named Formula","messagePattern":"You must specify a Formula value for a Named Formula","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/NamedFormula.php","lineNumber":21,"sourceCode":"namespace PhpOffice\\PhpSpreadsheet;\n\nuse PhpOffice\\PhpSpreadsheet\\Worksheet\\Worksheet;\n\nclass NamedFormula extends DefinedName\n{\n    /**\n     * Create a new Named Formula.\n     */\n    public function __construct(\n        string $name,\n        ?Worksheet $worksheet = null,\n        ?string $formula = null,\n        bool $localOnly = false,\n        ?Worksheet $scope = null\n    ) {\n        // Validate data\n        if (!isset($formula)) {\n            throw new Exception('You must specify a Formula value for a Named Formula');\n        }\n        parent::__construct($name, $worksheet, $formula, $localOnly, $scope);\n    }\n\n    /**\n     * Get the formula value.\n     */\n    public function getFormula(): string\n    {\n        return $this->value;\n    }\n\n    /**\n     * Set the formula value.\n     */\n    public function setFormula(string $formula): self\n    {\n        if (!empty($formula)) {","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/NamedFormula.php#L3-L39","documentation":"A NamedFormula is PhpSpreadsheet's counterpart of an Excel defined name whose value is a formula. Its constructor enforces that a formula string is supplied; because the check is !isset($formula), only null is rejected — an empty string technically passes. The throw means the third constructor argument was omitted or explicitly null.","triggerScenarios":"new NamedFormula('Vat') with no formula argument; passing a nullable lookup straight through, e.g. new NamedFormula('Vat', $sheet, $config['formula'] ?? null); factory code that builds named formulas from YAML/DB rows where the formula key is missing.","commonSituations":"Generating defined names from configuration files or database rows with optional columns; migrating from the older generic DefinedName API to the split NamedRange/NamedFormula classes.","solutions":["Supply the formula as the third constructor argument, e.g. new NamedFormula('Vat', $sheet, '=SUM(A1:A9)')","Null-check or default the source value before constructing","If the formula is genuinely optional, skip creating the NamedFormula or use a NamedRange with a default range instead"],"exampleFix":"// before\nnew NamedFormula('Vat', $sheet, $row['formula'] ?? null); // null -> exception\n\n// after\n$formula = $row['formula'] ?? null;\nif ($formula !== null) {\n    $spreadsheet->addNamedFormula(new NamedFormula('Vat', $sheet, $formula));\n}","handlingStrategy":"validation","validationCode":"$formula = $config['formula'] ?? null;\nif ($formula === null) {\n    throw new InvalidArgumentException(\"Named formula '{$name}' requires a non-null formula\");\n}\n$spreadsheet->addNamedFormula(new NamedFormula($name, $sheet, $formula));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat the formula as a required field in any config-driven defined-name builder","Coalesce nullable sources ($row['formula'] ?? '=0') at the boundary so null never reaches the constructor","Distinguish early: ranges go to NamedRange, formulas to NamedFormula"],"tags":["named-formula","defined-name","constructor","validation"],"backgroundTag":"missing-required-constructor-argument","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}