{"record":{"id":"38c0874d795e350b","repo":"PHPOffice/PhpSpreadsheet","slug":"incorrect-number-of-arguments-in-function-functio","errorCode":null,"errorMessage":"Incorrect number of arguments in function $function() ","messagePattern":"Incorrect number of arguments in function \\$function\\(\\) ","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/Xls/Parser.php","lineNumber":1551,"sourceCode":"                    $this->advance(); // eat the \",\" or \";\"\n                } else {\n                    throw new WriterException(\"Syntax error: comma expected in function $function, arg #{$num_args}\");\n                }\n                $result2 = $this->condition();\n                $result = $this->createTree('arg', $result, $result2);\n            } else { // first argument\n                $result2 = $this->condition();\n                $result = $this->createTree('arg', '', $result2);\n            }\n            ++$num_args;\n        }\n        if (!isset($this->functions[$function])) {\n            throw new WriterException(\"Function $function() doesn't exist\");\n        }\n        $args = $this->functions[$function][1];\n        // If fixed number of args eg. TIME($i, $j, $k). Check that the number of args is valid.\n        if (($args >= 0) && ($args != $num_args)) {\n            throw new WriterException(\"Incorrect number of arguments in function $function() \");\n        }\n\n        $result = $this->createTree($function, $result, $num_args);\n        $this->advance(); // eat the \")\"\n\n        return $result;\n    }\n\n    /**\n     * Creates a tree. In fact an array which may have one or two arrays (sub-trees)\n     * as elements.\n     *\n     * @param mixed $value the value of this node\n     * @param mixed $left the left array (sub-tree) or a final node\n     * @param mixed $right the right array (sub-tree) or a final node\n     *\n     * @return mixed[] A tree\n     */","sourceCodeStart":1533,"sourceCodeEnd":1569,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/Xls/Parser.php#L1533-L1569","documentation":"Each entry in the Xls writer parser's $functions table declares an argument count; a negative value means variable arity (e.g. SUM). When the parsed argument count differs from the declared fixed count (e.g. TIME takes exactly 3), save() to .xls throws. The check runs after parsing completes, so the message names the offending function.","triggerScenarios":"Formulas such as '=TIME(12,30)' (2 args supplied, 3 required), '=NOW(1)' (0 required), or a stray/missing comma that shifts the argument count, saved through the Xls writer.","commonSituations":"Hand-built formula strings with a missing or extra argument; refactoring formulas that relied on optional arguments the BIFF8 parser treats as fixed; pasting formulas between locales where ';' vs ',' separators change the count.","solutions":["Check the function's signature and pass the exact required number of arguments","Look for a stray or missing comma inside the reported function call","Verify the formula in Excel itself - if Excel also rejects it, fix the formula","Save as Xlsx or write the precomputed value if the arity cannot be matched"],"exampleFix":"// before\n$sheet->setCellValue('A1', '=TIME(12,30)');\n\n// after\n$sheet->setCellValue('A1', '=TIME(12,30,0)');","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    IOFactory::createWriter($spreadsheet, 'Xls')->save($path);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Writer\\Exception $e) {\n    if (str_contains($e->getMessage(), 'Incorrect number of arguments')) {\n        // message includes the function name; surface it for a targeted fix\n        error_log('Xls export arity error: ' . $e->getMessage());\n        throw new ExportValidationException($e->getMessage(), 0, $e);\n    }\n    throw $e;\n}","preventionTips":["For fixed-arity functions (TIME 3, DATE 3, IF 3, ROUND 2), count commas at the top nesting level in generated formulas","Validate formulas by evaluating them with the Calculation engine before export; arity errors usually surface there too","Avoid trailing/duplicate commas like '=TIME(12,30,)' in template strings"],"tags":["phpspreadsheet","xls","formula","arguments","writer"],"backgroundTag":"invalid-function-arguments","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}