/** * DataType for value * * @param mixed $pValue * @return int */ public static function dataTypeForValue($pValue = null) { // Match the value against a few data types if (is_null($pValue)) { return Cell_DataType::TYPE_NULL; } elseif ($pValue === '') { return Cell_DataType::TYPE_STRING; } elseif ($pValue instanceof RichText) { return Cell_DataType::TYPE_STRING; } elseif ($pValue[0] === '=' && strlen($pValue) > 1) { return Cell_DataType::TYPE_FORMULA; } elseif (is_bool($pValue)) { return Cell_DataType::TYPE_BOOL; } elseif (is_float($pValue) || is_int($pValue)) { return Cell_DataType::TYPE_NUMERIC; } elseif (preg_match('/^\\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue)) { return Cell_DataType::TYPE_NUMERIC; } elseif (is_string($pValue) && array_key_exists($pValue, Cell_DataType::getErrorCodes())) { return Cell_DataType::TYPE_ERROR; } else { return Cell_DataType::TYPE_STRING; } }
/** * Write a formula to the specified row and column (zero indexed). * The textual representation of the formula is passed to the parser in * Parser.php which returns a packed binary string. * * Returns 0 : normal termination * -1 : formula errors (bad formula) * -2 : row or column out of range * * @param integer $row Zero indexed row * @param integer $col Zero indexed column * @param string $formula The formula text string * @param mixed $format The optional XF format * @param mixed $calculatedValue Calculated value * @return integer */ private function _writeFormula($row, $col, $formula, $xfIndex, $calculatedValue) { $record = 0x6; // Record identifier // Initialize possible additional value for STRING record that should be written after the FORMULA record? $stringValue = null; // calculated value if (isset($calculatedValue)) { // Since we can't yet get the data type of the calculated value, // we use best effort to determine data type if (is_bool($calculatedValue)) { // Boolean value $num = pack('CCCvCv', 0x1, 0x0, (int) $calculatedValue, 0x0, 0x0, 0xffff); } elseif (is_int($calculatedValue) || is_float($calculatedValue)) { // Numeric value $num = pack('d', $calculatedValue); } elseif (is_string($calculatedValue)) { if (array_key_exists($calculatedValue, Cell_DataType::getErrorCodes())) { // Error value $num = pack('CCCvCv', 0x2, 0x0, $this->_mapErrorCode($calculatedValue), 0x0, 0x0, 0xffff); } elseif ($calculatedValue === '' && $this->_BIFF_version == 0x600) { // Empty string (and BIFF8) $num = pack('CCCvCv', 0x3, 0x0, 0x0, 0x0, 0x0, 0xffff); } else { // Non-empty string value (or empty string BIFF5) $stringValue = $calculatedValue; $num = pack('CCCvCv', 0x0, 0x0, 0x0, 0x0, 0x0, 0xffff); } } else { // We are really not supposed to reach here $num = pack('d', 0x0); } } else { $num = pack('d', 0x0); } $grbit = 0x3; // Option flags $unknown = 0x0; // Must be zero // Strip the '=' or '@' sign at the beginning of the formula string if (preg_match("/^=/", $formula)) { $formula = preg_replace("/(^=)/", "", $formula); } else { // Error handling $this->_writeString($row, $col, 'Unrecognised character for formula'); return -1; } // Parse the formula using the parser in Parser.php try { $error = $this->_parser->parse($formula); $formula = $this->_parser->toReversePolish(); $formlen = strlen($formula); // Length of the binary string $length = 0x16 + $formlen; // Length of the record data $header = pack("vv", $record, $length); $data = pack("vvv", $row, $col, $xfIndex) . $num . pack("vVv", $grbit, $unknown, $formlen); $this->_append($header . $data . $formula); // Append also a STRING record if necessary if ($stringValue !== null) { $this->_writeStringRecord($stringValue); } return 0; } catch (Exception $e) { // do nothing } }
/** * Set cell value (with explicit data type given) * * @param mixed $pValue Value * @param string $pDataType Explicit data type * @return Cell * @throws Exception */ public function setValueExplicit($pValue = null, $pDataType = Cell_DataType::TYPE_STRING) { // set the value according to data type switch ($pDataType) { case Cell_DataType::TYPE_STRING: case Cell_DataType::TYPE_NULL: case Cell_DataType::TYPE_INLINE: $this->_value = Cell_DataType::checkString($pValue); break; case Cell_DataType::TYPE_NUMERIC: $this->_value = (double) $pValue; break; case Cell_DataType::TYPE_FORMULA: $this->_value = (string) $pValue; break; case Cell_DataType::TYPE_BOOL: $this->_value = (bool) $pValue; break; case Cell_DataType::TYPE_ERROR: $this->_value = Cell_DataType::checkErrorCode($pValue); break; default: throw new Exception('Invalid datatype: ' . $pDataType); break; } // set the datatype $this->_dataType = $pDataType; $this->notifyCacheController(); return $this; }