PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 423 lines | 178 code | 46 blank | 199 comment | 34 complexity | a77e4e6e0262d1d93b61fd3d2e4e3bc8 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 library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 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/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_Cell_DataType */
  28. require_once 'PHPExcel/Cell/DataType.php';
  29. /** PHPExcel_Cell_DataValidation */
  30. require_once 'PHPExcel/Cell/DataValidation.php';
  31. /** PHPExcel_Worksheet */
  32. require_once 'PHPExcel/Worksheet.php';
  33. /** PHPExcel_Calculation */
  34. require_once 'PHPExcel/Calculation.php';
  35. /**
  36. * PHPExcel_Cell
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel
  40. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Cell
  43. {
  44. /**
  45. * Column of the cell
  46. *
  47. * @var string
  48. */
  49. private $_column;
  50. /**
  51. * Row of the cell
  52. *
  53. * @var int
  54. */
  55. private $_row;
  56. /**
  57. * Value of the cell
  58. *
  59. * @var mixed
  60. */
  61. private $_value;
  62. /**
  63. * Type of the cell data
  64. *
  65. * @var string
  66. */
  67. private $_dataType;
  68. /**
  69. * Data validation
  70. *
  71. * @var PHPExcel_Cell_DataValidation
  72. */
  73. private $_dataValidation;
  74. /**
  75. * Parent worksheet
  76. *
  77. * @var PHPExcel_Worksheet
  78. */
  79. private $_parent;
  80. /**
  81. * Create a new Cell
  82. *
  83. * @param string $pColumn
  84. * @param int $pRow
  85. * @param mixed $pValue
  86. * @param string $pDataType
  87. * @param PHPExcel_Worksheet $pSheet
  88. * @throws Exception
  89. */
  90. public function __construct($pColumn = 'A', $pRow = 1, $pValue = null, $pDataType = null, $pSheet = null)
  91. {
  92. // Initialise cell coordinate
  93. $this->_column = strtoupper($pColumn);
  94. $this->_row = $pRow;
  95. // Initialise cell value
  96. $this->_value = $pValue;
  97. // Set datatype?
  98. if (!is_null($pDataType)) {
  99. $this->_dataType = $pDataType;
  100. }
  101. // Set worksheet?
  102. if ($pSheet instanceof PHPExcel_Worksheet) {
  103. $this->_parent = $pSheet;
  104. } else {
  105. throw new Exception("Invalid parent worksheet passed.");
  106. }
  107. }
  108. /**
  109. * Get cell coordinate column
  110. *
  111. * @return string
  112. */
  113. public function getColumn()
  114. {
  115. return strtoupper($this->_column);
  116. }
  117. /**
  118. * Get cell coordinate row
  119. *
  120. * @return int
  121. */
  122. public function getRow()
  123. {
  124. return $this->_row;
  125. }
  126. /**
  127. * Get cell coordinate
  128. *
  129. * @return string
  130. */
  131. public function getCoordinate()
  132. {
  133. return $this->getColumn() . $this->getRow();
  134. }
  135. /**
  136. * Get cell value
  137. *
  138. * @return mixed
  139. */
  140. public function getValue()
  141. {
  142. return $this->_value;
  143. }
  144. /**
  145. * Set cell value
  146. *
  147. * This clears the cell formula.
  148. *
  149. * @param mixed $pValue
  150. * @param bool $pUpdateDataType
  151. */
  152. public function setValue($pValue = null, $pUpdateDataType = true)
  153. {
  154. $this->_value = $pValue;
  155. if ($pUpdateDataType) {
  156. $this->_dataType = PHPExcel_Cell_DataType::dataTypeForValue($pValue);
  157. }
  158. }
  159. /**
  160. * Get caluclated cell value
  161. *
  162. * @return mixed
  163. */
  164. public function getCalculatedValue()
  165. {
  166. if ($this->_dataType != PHPExcel_Cell_DataType::TYPE_FORMULA) {
  167. return $this->_value;
  168. } else {
  169. return PHPExcel_Calculation::getInstance()->calculate($this);
  170. }
  171. }
  172. /**
  173. * Get cell data type
  174. *
  175. * @return string
  176. */
  177. public function getDataType()
  178. {
  179. return $this->_dataType;
  180. }
  181. /**
  182. * Set cell data type
  183. *
  184. * @param string $pDataType
  185. */
  186. public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  187. {
  188. $this->_dataType = $pDataType;
  189. }
  190. /**
  191. * Has Data validation?
  192. *
  193. * @return boolean
  194. */
  195. public function hasDataValidation()
  196. {
  197. return !is_null($this->_dataValidation);
  198. }
  199. /**
  200. * Get Data validation
  201. *
  202. * @return PHPExcel_Cell_DataValidation
  203. */
  204. public function getDataValidation()
  205. {
  206. if (is_null($this->_dataValidation)) {
  207. $this->_dataValidation = new PHPExcel_Cell_DataValidation($this);
  208. }
  209. return $this->_dataValidation;
  210. }
  211. /**
  212. * Set Data validation
  213. *
  214. * @param PHPExcel_Cell_DataValidation $pDataValidation
  215. * @throws Exception
  216. */
  217. public function setDataValidation($pDataValidation = null)
  218. {
  219. if ($pDataValidation instanceof PHPExcel_Cell_DataValidation) {
  220. $this->_dataValidation = $pDataValidation;
  221. $this->_dataValidation->setParent($this);
  222. } else {
  223. throw new Exception("Invalid PHPExcel_Cell_DataValidation object passed.");
  224. }
  225. }
  226. /**
  227. * Get parent
  228. *
  229. * @return PHPExcel_Worksheet
  230. */
  231. public function getParent() {
  232. return $this->_parent;
  233. }
  234. /**
  235. * Coordinate from string
  236. *
  237. * @param string $pCoordinateString
  238. * @return array Array containing column and row (indexes 0 and 1)
  239. * @throws Exception
  240. */
  241. public static function coordinateFromString($pCoordinateString = 'A1')
  242. {
  243. if (eregi(':', $pCoordinateString)) {
  244. throw new Exception('Cell coordinate string can not be a range of cells.');
  245. } else if (eregi('\$', $pCoordinateString)) {
  246. throw new Exception('Cell coordinate string must not be absolute.');
  247. } else if ($pCoordinateString == '') {
  248. throw new Exception('Cell coordinate can not be zero-length string.');
  249. } else {
  250. // Column
  251. $column = '';
  252. // Row
  253. $row = '';
  254. // Calculate column
  255. for ($i = 0; $i < strlen($pCoordinateString); $i++) {
  256. if (!is_numeric(substr($pCoordinateString, $i, 1))) {
  257. $column .= strtoupper(substr($pCoordinateString, $i, 1));
  258. } else {
  259. $row = substr($pCoordinateString, $i);
  260. break;
  261. }
  262. }
  263. // Return array
  264. return array($column, $row);
  265. }
  266. }
  267. /**
  268. * Make string coordinate absolute
  269. *
  270. * @param string $pCoordinateString
  271. * @return string Absolute coordinate
  272. * @throws Exception
  273. */
  274. public static function absoluteCoordinate($pCoordinateString = 'A1')
  275. {
  276. if (!eregi(':', $pCoordinateString)) {
  277. // Return value
  278. $returnValue = '';
  279. // Create absolute coordinate
  280. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCoordinateString);
  281. $returnValue = '$' . $column . '$' . $row;
  282. // Return
  283. return $returnValue;
  284. } else {
  285. throw new Exception("Coordinate string should not be a cell range.");
  286. }
  287. }
  288. /**
  289. * Split range into coordinate strings
  290. *
  291. * @param string $pRange
  292. * @return array Array containg two coordinate strings
  293. */
  294. public static function splitRange($pRange = 'A1:A1')
  295. {
  296. return explode(':', $pRange);
  297. }
  298. /**
  299. * Columnindex from string
  300. *
  301. * @param string $pString
  302. * @return int Column index (base 1 !!!)
  303. * @throws Exception
  304. */
  305. public static function columnIndexFromString($pString = 'A')
  306. {
  307. // Convert to uppercase
  308. $pString = strtoupper($pString);
  309. // Convert column to integer
  310. if (strlen($pString) == 1) {
  311. $result = 0;
  312. $result += (ord(substr($pString, 0, 1)) - 65);
  313. $result += 1;
  314. return $result;
  315. } else if (strlen($pString) == 2) {
  316. $result = 0;
  317. $result += ( (1 + (ord(substr($pString, 0, 1)) - 65) ) * 26);
  318. $result += (ord(substr($pString, 1, 2)) - 65);
  319. $result += 1;
  320. return $result;
  321. } else {
  322. throw new Exception("Column string index can not be longer than 2 characters.");
  323. }
  324. }
  325. /**
  326. * String from columnindex
  327. *
  328. * @param int $pColumnIndex Column index (base 0 !!!)
  329. * @return string
  330. */
  331. public static function stringFromColumnIndex($pColumnIndex = 0)
  332. {
  333. // Convert column to string
  334. $returnValue = '';
  335. // Determine column string
  336. if ($pColumnIndex < 26) {
  337. $returnValue = chr(65 + $pColumnIndex);
  338. } else {
  339. $iRemainder = (int)($pColumnIndex / 26) -1;
  340. $returnValue = PHPExcel_Cell::stringFromColumnIndex( $iRemainder ).chr(65 + $pColumnIndex%26) ;
  341. }
  342. // Return
  343. return $returnValue;
  344. }
  345. /**
  346. * Compare 2 cells
  347. *
  348. * @param PHPExcel_Cell $a Cell a
  349. * @param PHPExcel_Cell $a Cell b
  350. * @return int Result of comparison (always -1 or 1, never zero!)
  351. */
  352. public static function compareCells($a, $b)
  353. {
  354. if ($a->getRow() < $b->getRow()) {
  355. return -1;
  356. } elseif ($a->getRow() > $b->getRow()) {
  357. return 1;
  358. } elseif (PHPExcel_Cell::columnIndexFromString($a->getColumn()) < PHPExcel_Cell::columnIndexFromString($b->getColumn())) {
  359. return -1;
  360. } else {
  361. return 1;
  362. }
  363. }
  364. /**
  365. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  366. */
  367. public function __clone() {
  368. $vars = get_object_vars($this);
  369. foreach ($vars as $key => $value) {
  370. if (is_object($value)) {
  371. $this->$key = clone $value;
  372. } else {
  373. $this->$key = $value;
  374. }
  375. }
  376. }
  377. }