/** * 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 PHPExcel_Cell_DataType::TYPE_NULL; } elseif ($pValue === '') { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif ($pValue instanceof PHPExcel_RichText) { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif ($pValue[0] === '=') { return PHPExcel_Cell_DataType::TYPE_FORMULA; } elseif (is_bool($pValue)) { return PHPExcel_Cell_DataType::TYPE_BOOL; } elseif (preg_match('/^\\-?[0-9]*\\.?[0-9]*$/', $pValue)) { return PHPExcel_Cell_DataType::TYPE_NUMERIC; } elseif (array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) { return PHPExcel_Cell_DataType::TYPE_ERROR; } else { return PHPExcel_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 $xfIndex The XF format index * @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, PHPExcel_Cell_DataType::getErrorCodes())) { // Error value $num = pack('CCCvCv', 0x2, 0x0, self::_mapErrorCode($calculatedValue), 0x0, 0x0, 0xffff); } elseif ($calculatedValue === '') { // 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 ($formula[0] == '=') { $formula = substr($formula, 1); } 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 (PHPExcel_Exception $e) { // do nothing } }
/** * 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 PHPExcel_Cell_DataType::TYPE_NULL; } elseif ($pValue === '') { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif ($pValue instanceof PHPExcel_RichText) { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif ($pValue {0} === '=' && strlen ( $pValue ) > 1) { return PHPExcel_Cell_DataType::TYPE_FORMULA; } elseif (is_bool ( $pValue )) { return PHPExcel_Cell_DataType::TYPE_BOOL; } elseif (is_float ( $pValue ) || is_int ( $pValue )) { return PHPExcel_Cell_DataType::TYPE_NUMERIC; } elseif (preg_match ( '/^\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue )) { return PHPExcel_Cell_DataType::TYPE_NUMERIC; } elseif (is_string ( $pValue ) && array_key_exists ( $pValue, PHPExcel_Cell_DataType::getErrorCodes () )) { return PHPExcel_Cell_DataType::TYPE_ERROR; } else { return PHPExcel_Cell_DataType::TYPE_STRING; } }
/** * DataType for value * * @param mixed $pValue * @return string */ public static function dataTypeForValue($pValue = null) { // Match the value against a few data types if ($pValue === null) { return PHPExcel_Cell_DataType::TYPE_NULL; } elseif ($pValue === '') { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif ($pValue instanceof PHPExcel_RichText) { return PHPExcel_Cell_DataType::TYPE_INLINE; } elseif ($pValue[0] === '=' && strlen($pValue) > 1) { return PHPExcel_Cell_DataType::TYPE_FORMULA; } elseif (is_bool($pValue)) { return PHPExcel_Cell_DataType::TYPE_BOOL; } elseif (is_float($pValue) || is_int($pValue)) { return PHPExcel_Cell_DataType::TYPE_NUMERIC; } elseif (preg_match('/^[\\+\\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\\-\\+]?[0-2]?\\d{1,3})?$/', $pValue)) { $tValue = ltrim($pValue, '+-'); if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') { return PHPExcel_Cell_DataType::TYPE_STRING; } elseif (strpos($pValue, '.') === false && $pValue > PHP_INT_MAX) { return PHPExcel_Cell_DataType::TYPE_STRING; } return PHPExcel_Cell_DataType::TYPE_NUMERIC; } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) { return PHPExcel_Cell_DataType::TYPE_ERROR; } return PHPExcel_Cell_DataType::TYPE_STRING; }