{"record":{"id":"0d4073b4f2b75599","repo":"PHPOffice/PhpSpreadsheet","slug":"value-0d4073","errorCode":"#VALUE!","errorMessage":"#VALUE!","messagePattern":"#VALUE!","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Calculation\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Calculation/MathTrig/MatrixFunctions.php","lineNumber":34,"sourceCode":"     *\n     * @param mixed $matrixValues A matrix of values\n     */\n    private static function getMatrix(mixed $matrixValues): Matrix\n    {\n        $matrixData = [];\n        if (!is_array($matrixValues)) {\n            $matrixValues = [[$matrixValues]];\n        }\n\n        $row = 0;\n        foreach ($matrixValues as $matrixRow) {\n            if (!is_array($matrixRow)) {\n                $matrixRow = [$matrixRow];\n            }\n            $column = 0;\n            foreach ($matrixRow as $matrixCell) {\n                if ((is_string($matrixCell)) || ($matrixCell === null)) {\n                    throw new Exception(ExcelError::VALUE());\n                }\n                $matrixData[$row][$column] = $matrixCell;\n                ++$column;\n            }\n            ++$row;\n        }\n\n        return new Matrix($matrixData);\n    }\n\n    /**\n     * SEQUENCE.\n     *\n     * Generates a list of sequential numbers in an array.\n     *\n     * Excel Function:\n     *      SEQUENCE(rows,[columns],[start],[step])\n     *","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Calculation/MathTrig/MatrixFunctions.php#L16-L52","documentation":"MatrixFunctions::getMatrix() (used by MDETERM, MINVERSE and MMULT) coerces the argument into a 2x2 grid and rejects any cell that is a string or null by throwing Calculation\\Exception with ExcelError::VALUE(). Excel matrix functions require a pure numeric matrix, so a single text or blank cell makes the whole function return #VALUE!. Note that even numeric strings count as strings here.","triggerScenarios":"=MMULT(A1:B2, C1:D2) where any referenced cell contains text, a space, or is empty; =MDETERM(A1:B2) with a blank cell; ranges that include header labels; passing a PHP array containing strings/nulls to MatrixFunctions::MMULT() directly.","commonSituations":"Ranges that accidentally include header rows or annotation text; sparse matrices from database exports where missing entries are blank instead of 0; mixed-type columns where one row holds a note; formulas built dynamically from user-selected ranges that include labels.","solutions":["Restrict the range to the numeric block only, excluding headers/labels","Replace blanks in the source range with 0 (or fill them) so the matrix is fully numeric","Use =IF(COUNT(A1:B2)=4, MMULT(...), \"non-numeric matrix\") style guards or ISNUMBER checks per cell when generating formulas","When calling from PHP, array_map the matrix with a coercion that rejects/filters non-numeric entries first"],"exampleFix":"// before: A1:B2 contains a blank cell -> #VALUE!\n$sheet->getCell('E1')->setValue('=MINVERSE(A1:B2)');\n\n// after: normalize blanks to 0 first\nforeach ($sheet->rangeToArray('A1:B2', null, true, true, false) as $r => $row) {\n    foreach ($row as $c => $v) {\n        if ($v === null || is_string($v)) {\n            $sheet->getCell([$c + 1, $r + 1])->setValue(0);\n        }\n    }\n}","handlingStrategy":"validation","validationCode":"// Reject matrices with string/null cells before MMULT/MINVERSE/MDETERM\n$flat = [];\narray_walk_recursive($matrix, function ($v) use (&$flat) { $flat[] = $v; });\n$bad = array_filter($flat, fn ($v) => !is_int($v) && !is_float($v));\nif ($bad !== []) {\n    throw new InvalidArgumentException('matrix contains non-numeric cell');\n}","typeGuard":"/** @param array<int,array<int,int|float>> $m */\nfunction isNumericMatrix(array $m): bool\n{\n    foreach ($m as $row) {\n        foreach ($row as $cell) {\n            if (!is_int($cell) && !is_float($cell)) return false;\n        }\n    }\n    return true;\n}","tryCatchPattern":"try {\n    $r = MatrixFunctions::MMULT($a, $b);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Calculation\\Exception $e) {\n    $r = $e->getMessage(); // '#VALUE!'\n}","preventionTips":["Select only the numeric block of ranges; exclude header/label rows","Fill sparse matrices with 0 instead of leaving blanks","Numeric strings are still strings - cast PHP data to int/float before writing cells","Add =COUNT(range)=ROWS(range)*COLUMNS(range) sanity checks when generating matrix formulas"],"tags":["phpspreadsheet","excel-formula","matrix","mmult","minverse","mdeterm","value-error"],"backgroundTag":"excel-value-error","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}