PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.1.0/Classes/PHPExcel/Cell.php

#
PHP | 291 lines | 111 code | 32 blank | 148 comment | 15 complexity | 8718b0419c6d99f46417dc0d08df013e MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 PHPExcel, Maarten Balliauw
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel
  23. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/gpl.txt GPL
  25. */
  26. /** PHPExcel_Cell_DataType */
  27. require_once 'PHPExcel/Cell/DataType.php';
  28. /**
  29. * PHPExcel_Cell
  30. *
  31. * @category PHPExcel
  32. * @package PHPExcel
  33. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  34. */
  35. class PHPExcel_Cell
  36. {
  37. /**
  38. * Column of the cell
  39. *
  40. * @var string
  41. */
  42. private $_column;
  43. /**
  44. * Row of the cell
  45. *
  46. * @var int
  47. */
  48. private $_row;
  49. /**
  50. * Value of the cell
  51. *
  52. * @var mixed
  53. */
  54. private $_value;
  55. /**
  56. * Type of the cell data
  57. *
  58. * @var string
  59. */
  60. private $_dataType;
  61. /**
  62. * Create a new Cell
  63. *
  64. * @param string $pColumn
  65. * @param int $pRow
  66. * @param mixed $pValue
  67. * @param string $pDataType
  68. */
  69. public function __construct($pColumn = 'A', $pRow = 1, $pValue = null, $pDataType = null)
  70. {
  71. // Initialise cell coordinate
  72. $this->_column = strtoupper($pColumn);
  73. $this->_row = $pRow;
  74. // Initialise cell value
  75. $this->_value = $pValue;
  76. if (!is_null($pDataType)) {
  77. $this->_dataType = $pDataType;
  78. }
  79. }
  80. /**
  81. * Get cell coordinate column
  82. *
  83. * @return string
  84. */
  85. public function getColumn()
  86. {
  87. return strtoupper($this->_column);
  88. }
  89. /**
  90. * Get cell coordinate row
  91. *
  92. * @return int
  93. */
  94. public function getRow()
  95. {
  96. return $this->_row;
  97. }
  98. /**
  99. * Get cell coordinate
  100. *
  101. * @return string
  102. */
  103. public function getCoordinate()
  104. {
  105. return $this->getColumn() . $this->getRow();
  106. }
  107. /**
  108. * Get cell value
  109. *
  110. * @return mixed
  111. */
  112. public function getValue()
  113. {
  114. return $this->_value;
  115. }
  116. /**
  117. * Set cell value
  118. *
  119. * This clears the cell formula.
  120. *
  121. * @param mixed $pValue
  122. * @param bool $pUpdateDataType
  123. */
  124. public function setValue($pValue = null, $pUpdateDataType = true)
  125. {
  126. $this->_value = $pValue;
  127. if ($pUpdateDataType) {
  128. $this->_dataType = PHPExcel_Cell_DataType::dataTypeForValue($pValue);
  129. }
  130. }
  131. /**
  132. * Get caluclated cell value
  133. *
  134. * @return mixed
  135. */
  136. public function getCalculatedValue()
  137. {
  138. return $this->_value;
  139. }
  140. /**
  141. * Get cell data type
  142. *
  143. * @return string
  144. */
  145. public function getDataType()
  146. {
  147. return $this->_dataType;
  148. }
  149. /**
  150. * Set cell data type
  151. *
  152. * @param string $pDataType
  153. */
  154. public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  155. {
  156. $this->_dataType = $pDataType;
  157. }
  158. /**
  159. * Coordinate from string
  160. *
  161. * @param string $pCoordinateString
  162. * @return array Array containing column and row (indexes 0 and 1)
  163. */
  164. public static function coordinateFromString($pCoordinateString = 'A1')
  165. {
  166. // Column
  167. $column = '';
  168. // Row
  169. $row = '';
  170. // Calculate column
  171. for ($i = 0; $i < strlen($pCoordinateString); $i++) {
  172. if (!is_numeric(substr($pCoordinateString, $i, 1))) {
  173. $column .= strtoupper(substr($pCoordinateString, $i, 1));
  174. } else {
  175. $row = substr($pCoordinateString, $i);
  176. break;
  177. }
  178. }
  179. // Return array
  180. return array($column, $row);
  181. }
  182. /**
  183. * Columnindex from string
  184. *
  185. * @param string $pString
  186. * @return int Column index (base 1 !!!)
  187. * @throws Exception
  188. */
  189. public static function columnIndexFromString($pString = 'A')
  190. {
  191. // Convert to uppercase
  192. $pString = strtoupper($pString);
  193. // Convert column to integer
  194. if (strlen($pString) == 1) {
  195. $result = 0;
  196. $result += (ord(substr($pString, 0, 1)) - 65);
  197. $result += 1;
  198. return $result;
  199. } else if (strlen($pString) == 2) {
  200. $result = 0;
  201. $result += ( (1 + (ord(substr($pString, 0, 1)) - 65) ) * 26);
  202. $result += (ord(substr($pString, 1, 2)) - 65);
  203. $result += 1;
  204. return $result;
  205. } else {
  206. throw new Exception("Column string index can not be longer than 2 characters.");
  207. }
  208. }
  209. /**
  210. * String from columnindex
  211. *
  212. * @param int $pColumnIndex Column index
  213. * @return string
  214. */
  215. public static function stringFromColumnIndex($pColumnIndex = 0)
  216. {
  217. // Convert column to string
  218. $returnValue = '';
  219. // Determine column string
  220. if ($pColumnIndex <= 25) {
  221. $returnValue = chr(65 + $pColumnIndex);
  222. } else {
  223. $iRemainder = number_format(($pColumnIndex / 26), 0) - 1;
  224. $returnValue = chr(65 + $iRemainder) . PHPExcel_Cell::stringFromColumnIndex( ($pColumnIndex % 26) );
  225. }
  226. // Return
  227. return $returnValue;
  228. }
  229. /**
  230. * Compare 2 cells
  231. *
  232. * @param PHPExcel_Cell $a Cell a
  233. * @param PHPExcel_Cell $a Cell b
  234. * @return int Result of comparison (always -1 or 1, never zero!)
  235. */
  236. public static function compareCells($a, $b)
  237. {
  238. if ($a->getRow() < $b->getRow()) {
  239. return -1;
  240. } elseif ($a->getRow() > $b->getRow()) {
  241. return 1;
  242. } elseif (PHPExcel_Cell::columnIndexFromString($a->getColumn()) < PHPExcel_Cell::columnIndexFromString($b->getColumn())) {
  243. return -1;
  244. } else {
  245. return 1;
  246. }
  247. }
  248. /**
  249. * Duplicate object
  250. *
  251. * Duplicates the current object, also duplicating referenced objects (deep cloning).
  252. * Standard PHP clone does not copy referenced objects!
  253. *
  254. * @return PHPExcel_Cell
  255. */
  256. public function duplicate() {
  257. return unserialize(serialize($this));
  258. }
  259. }