PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Writer\Exception
Incorrect number of arguments in function $function()
Error message
Incorrect number of arguments in function $function()
What it means
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.
Source
Thrown at src/PhpSpreadsheet/Writer/Xls/Parser.php:1551
$this->advance(); // eat the "," or ";"
} else {
throw new WriterException("Syntax error: comma expected in function $function, arg #{$num_args}");
}
$result2 = $this->condition();
$result = $this->createTree('arg', $result, $result2);
} else { // first argument
$result2 = $this->condition();
$result = $this->createTree('arg', '', $result2);
}
++$num_args;
}
if (!isset($this->functions[$function])) {
throw new WriterException("Function $function() doesn't exist");
}
$args = $this->functions[$function][1];
// If fixed number of args eg. TIME($i, $j, $k). Check that the number of args is valid.
if (($args >= 0) && ($args != $num_args)) {
throw new WriterException("Incorrect number of arguments in function $function() ");
}
$result = $this->createTree($function, $result, $num_args);
$this->advance(); // eat the ")"
return $result;
}
/**
* Creates a tree. In fact an array which may have one or two arrays (sub-trees)
* as elements.
*
* @param mixed $value the value of this node
* @param mixed $left the left array (sub-tree) or a final node
* @param mixed $right the right array (sub-tree) or a final node
*
* @return mixed[] A tree
*/View on GitHub (pinned to 65b080eef4)
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
Example fix
// before
$sheet->setCellValue('A1', '=TIME(12,30)');
// after
$sheet->setCellValue('A1', '=TIME(12,30,0)'); Defensive patterns
Strategy: try-catch
Try / catch
try {
IOFactory::createWriter($spreadsheet, 'Xls')->save($path);
} catch (\PhpOffice\PhpSpreadsheet\Writer\Exception $e) {
if (str_contains($e->getMessage(), 'Incorrect number of arguments')) {
// message includes the function name; surface it for a targeted fix
error_log('Xls export arity error: ' . $e->getMessage());
throw new ExportValidationException($e->getMessage(), 0, $e);
}
throw $e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Syntax error: comma expected in function $function, arg #{$n
- Function $function() doesn't exist
- Unrecognized space type in tAttrSpace token
- Unrecognized attribute flag in tAttr token
- Unrecognized function in formula
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/38c0874d795e350b.
Report an issue: GitHub.