{"record":{"id":"312a543902a165a0","repo":"PHPOffice/PhpSpreadsheet","slug":"invalid-datatype-datatype","errorCode":null,"errorMessage":"Invalid datatype: $dataType","messagePattern":"Invalid datatype: \\$dataType","errorType":"exception","errorClass":"SpreadsheetException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":361,"sourceCode":"            case DataType::TYPE_ISO_DATE:\n                $this->value = SharedDate::convertIsoDate($value);\n                $dataType = DataType::TYPE_NUMERIC;\n\n                break;\n            case DataType::TYPE_DRAWING_IN_CELL:\n                if ($value instanceof BaseDrawing) {\n                    $this->value = $value;\n                } else {\n                    throw new SpreadsheetException('Item is not a drawing');\n                }\n\n                break;\n            case DataType::TYPE_ERROR:\n                $this->value = DataType::checkErrorCode($value);\n\n                break;\n            default:\n                throw new SpreadsheetException('Invalid datatype: ' . $dataType);\n        }\n\n        // set the datatype\n        $this->dataType = $dataType;\n\n        $this->updateInCollection();\n        $cellCoordinate = $this->getCoordinate();\n        self::updateIfCellIsTableHeader($this->getParent()?->getParent(), $this, $oldValue, $value);\n        $worksheet = $this->getWorksheet();\n        $spreadsheet = $worksheet->getParent();\n        if (isset($spreadsheet) && $spreadsheet->getIndex($worksheet, true) >= 0) {\n            $originalSelected = $worksheet->getSelectedCells();\n            $activeSheetIndex = $spreadsheet->getActiveSheetIndex();\n            $style = $this->getStyle();\n            $oldQuotePrefix = $style->getQuotePrefix();\n            if ($oldQuotePrefix !== $quotePrefix) {\n                $style->setQuotePrefix($quotePrefix);\n            }","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L343-L379","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$cell->setValueExplicit($value, 'integer'); // throws: Invalid datatype: integer\n\n// after\n$cell->setValueExplicit((int) $value, DataType::TYPE_NUMERIC);","handlingStrategy":"validation","validationCode":"$allowed = [\n    DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_FORMULA,\n    DataType::TYPE_NUMERIC, DataType::TYPE_BOOL, DataType::TYPE_NULL,\n    DataType::TYPE_INLINE, DataType::TYPE_ERROR, DataType::TYPE_ISO_DATE,\n    DataType::TYPE_DRAWING_IN_CELL,\n];\nif (!in_array($dataType, $allowed, true)) {\n    throw new InvalidArgumentException(\"Unsupported datatype: $dataType\");\n}\n$cell->setValueExplicit($value, $dataType);","typeGuard":"function isValidDataType(string $type): bool\n{\n    return in_array($type, [\n        DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_FORMULA,\n        DataType::TYPE_NUMERIC, DataType::TYPE_BOOL, DataType::TYPE_NULL,\n        DataType::TYPE_INLINE, DataType::TYPE_ERROR, DataType::TYPE_ISO_DATE,\n        DataType::TYPE_DRAWING_IN_CELL,\n    ], true);\n}","tryCatchPattern":"null","preventionTips":["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"],"tags":["phpspreadsheet","datatype","invalid-enum","cell-value"],"backgroundTag":"invalid-enum-value","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}