PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Invalid datatype: $dataType
Error message
Invalid datatype: $dataType
What it means
setValueExplicit() switches on the datatype string and only accepts the DataType constants: 's'/'str' (string), 'f' (formula), 'n' (numeric), 'b' (bool), 'null', 'inlineStr', 'e' (error), 'd' (ISO date), 'drawingCell'. Any other string reaches the default arm and throws, echoing the offending value. There is no dynamic type registration - the set is closed.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:361
case DataType::TYPE_ISO_DATE:
$this->value = SharedDate::convertIsoDate($value);
$dataType = DataType::TYPE_NUMERIC;
break;
case DataType::TYPE_DRAWING_IN_CELL:
if ($value instanceof BaseDrawing) {
$this->value = $value;
} else {
throw new SpreadsheetException('Item is not a drawing');
}
break;
case DataType::TYPE_ERROR:
$this->value = DataType::checkErrorCode($value);
break;
default:
throw new SpreadsheetException('Invalid datatype: ' . $dataType);
}
// set the datatype
$this->dataType = $dataType;
$this->updateInCollection();
$cellCoordinate = $this->getCoordinate();
self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);
$worksheet = $this->getWorksheet();
$spreadsheet = $worksheet->getParent();
if (isset($spreadsheet) && $spreadsheet->getIndex($worksheet, true) >= 0) {
$originalSelected = $worksheet->getSelectedCells();
$activeSheetIndex = $spreadsheet->getActiveSheetIndex();
$style = $this->getStyle();
$oldQuotePrefix = $style->getQuotePrefix();
if ($oldQuotePrefix !== $quotePrefix) {
$style->setQuotePrefix($quotePrefix);
}View on GitHub (pinned to 65b080eef4)
Solutions
- Always pass DataType::TYPE_* constants, never literals
- Use DataType::TYPE_NUMERIC for all numbers (there is no separate integer/float type) and DataType::TYPE_STRING for text
- Whitelist config-provided types by mapping them to the constants in one place
Example fix
// before $cell->setValueExplicit($value, 'integer'); // throws: Invalid datatype: integer // after $cell->setValueExplicit((int) $value, DataType::TYPE_NUMERIC);
Defensive patterns
Strategy: validation
Validate before calling
$allowed = [
DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_FORMULA,
DataType::TYPE_NUMERIC, DataType::TYPE_BOOL, DataType::TYPE_NULL,
DataType::TYPE_INLINE, DataType::TYPE_ERROR, DataType::TYPE_ISO_DATE,
DataType::TYPE_DRAWING_IN_CELL,
];
if (!in_array($dataType, $allowed, true)) {
throw new InvalidArgumentException("Unsupported datatype: $dataType");
}
$cell->setValueExplicit($value, $dataType); Type guard
function isValidDataType(string $type): bool
{
return in_array($type, [
DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_FORMULA,
DataType::TYPE_NUMERIC, DataType::TYPE_BOOL, DataType::TYPE_NULL,
DataType::TYPE_INLINE, DataType::TYPE_ERROR, DataType::TYPE_ISO_DATE,
DataType::TYPE_DRAWING_IN_CELL,
], true);
} Try / catch
null
Prevention
- Always reference DataType::TYPE_* constants instead of string literals
- Reject config-supplied type strings early by mapping them to constants in one factory method
- Numbers are TYPE_NUMERIC only - there are no integer/float datatypes
When it happens
Trigger: Hand-written type strings like 'integer', 'float', 'text', or wrong-cased 'N'/'String'; constants copied from PHPExcel-era code (e.g. 'TYPE_NUMERIC' instead of DataType::TYPE_NUMERIC); types concatenated from user config.
Common situations: Migrating old PHPExcel projects; config-driven writers where the type comes from a YAML/DB column instead of code; examples found online targeting a different spreadsheet library.
Related errors
- Invalid numeric value for datatype Numeric
- Item is not a drawing
- Invalid value $calculateDateTimeType for calculated date tim
- Unknown trend type {$trendType}
- Conditional is not a Cell Value CF Rule conditional
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/312a543902a165a0.
Report an issue: GitHub.