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

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

#
PHP | 487 lines | 209 code | 57 blank | 221 comment | 36 complexity | 1a8868d6e1a6385c7a4c2a3f2cd8d112 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
  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, PHPExcel_Worksheet $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. $this->_parent = $pSheet;
  103. }
  104. /**
  105. * Get cell coordinate column
  106. *
  107. * @return string
  108. */
  109. public function getColumn()
  110. {
  111. return strtoupper($this->_column);
  112. }
  113. /**
  114. * Get cell coordinate row
  115. *
  116. * @return int
  117. */
  118. public function getRow()
  119. {
  120. return $this->_row;
  121. }
  122. /**
  123. * Get cell coordinate
  124. *
  125. * @return string
  126. */
  127. public function getCoordinate()
  128. {
  129. return $this->getColumn() . $this->getRow();
  130. }
  131. /**
  132. * Get cell value
  133. *
  134. * @return mixed
  135. */
  136. public function getValue()
  137. {
  138. return $this->_value;
  139. }
  140. /**
  141. * Set cell value
  142. *
  143. * This clears the cell formula.
  144. *
  145. * @param mixed $pValue
  146. * @param bool $pUpdateDataType
  147. */
  148. public function setValue($pValue = null, $pUpdateDataType = true)
  149. {
  150. $this->_value = $pValue;
  151. if ($pUpdateDataType) {
  152. $this->_dataType = PHPExcel_Cell_DataType::dataTypeForValue($pValue);
  153. }
  154. }
  155. /**
  156. * Get caluclated cell value
  157. *
  158. * @return mixed
  159. */
  160. public function getCalculatedValue()
  161. {
  162. if ($this->_dataType != PHPExcel_Cell_DataType::TYPE_FORMULA) {
  163. return $this->_value;
  164. } else {
  165. return PHPExcel_Calculation::getInstance()->calculate($this);
  166. }
  167. }
  168. /**
  169. * Get cell data type
  170. *
  171. * @return string
  172. */
  173. public function getDataType()
  174. {
  175. return $this->_dataType;
  176. }
  177. /**
  178. * Set cell data type
  179. *
  180. * @param string $pDataType
  181. */
  182. public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  183. {
  184. $this->_dataType = $pDataType;
  185. }
  186. /**
  187. * Has Data validation?
  188. *
  189. * @return boolean
  190. */
  191. public function hasDataValidation()
  192. {
  193. return !is_null($this->_dataValidation);
  194. }
  195. /**
  196. * Get Data validation
  197. *
  198. * @return PHPExcel_Cell_DataValidation
  199. */
  200. public function getDataValidation()
  201. {
  202. if (is_null($this->_dataValidation)) {
  203. $this->_dataValidation = new PHPExcel_Cell_DataValidation($this);
  204. }
  205. return $this->_dataValidation;
  206. }
  207. /**
  208. * Set Data validation
  209. *
  210. * @param PHPExcel_Cell_DataValidation $pDataValidation
  211. * @throws Exception
  212. */
  213. public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation = null)
  214. {
  215. $this->_dataValidation = $pDataValidation;
  216. $this->_dataValidation->setParent($this);
  217. }
  218. /**
  219. * Get parent
  220. *
  221. * @return PHPExcel_Worksheet
  222. */
  223. public function getParent() {
  224. return $this->_parent;
  225. }
  226. /**
  227. * Is cell in a specific range?
  228. *
  229. * @param string $pRange Cell range (e.g. A1:A1)
  230. * @return boolean
  231. */
  232. public function isInRange($pRange = 'A1:A1')
  233. {
  234. // Uppercase coordinate
  235. $pRange = strtoupper($pRange);
  236. // Extract range
  237. $rangeA = '';
  238. $rangeB = '';
  239. if (strpos($pRange, ':') === false) {
  240. $rangeA = $pRange;
  241. $rangeB = $pRange;
  242. } else {
  243. list($rangeA, $rangeB) = explode(':', $pRange);
  244. }
  245. // Calculate range outer borders
  246. $rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
  247. $rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
  248. // Translate column into index
  249. $rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]) - 1;
  250. $rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]) - 1;
  251. // Translate properties
  252. $myColumn = PHPExcel_Cell::columnIndexFromString($this->getColumn()) - 1;
  253. $myRow = $this->getRow();
  254. // Verify if cell is in range
  255. return (
  256. ($rangeStart[0] <= $myColumn && $rangeEnd[0] >= $myColumn) &&
  257. ($rangeStart[1] <= $myRow && $rangeEnd[1] >= $myRow)
  258. );
  259. }
  260. /**
  261. * Coordinate from string
  262. *
  263. * @param string $pCoordinateString
  264. * @return array Array containing column and row (indexes 0 and 1)
  265. * @throws Exception
  266. */
  267. public static function coordinateFromString($pCoordinateString = 'A1')
  268. {
  269. if (eregi(':', $pCoordinateString)) {
  270. throw new Exception('Cell coordinate string can not be a range of cells.');
  271. } else if (eregi('\$', $pCoordinateString)) {
  272. throw new Exception('Cell coordinate string must not be absolute.');
  273. } else if ($pCoordinateString == '') {
  274. throw new Exception('Cell coordinate can not be zero-length string.');
  275. } else {
  276. // Column
  277. $column = '';
  278. // Row
  279. $row = '';
  280. // Calculate column
  281. for ($i = 0; $i < strlen($pCoordinateString); $i++) {
  282. if (!is_numeric(substr($pCoordinateString, $i, 1))) {
  283. $column .= strtoupper(substr($pCoordinateString, $i, 1));
  284. } else {
  285. $row = substr($pCoordinateString, $i);
  286. break;
  287. }
  288. }
  289. // Return array
  290. return array($column, $row);
  291. }
  292. }
  293. /**
  294. * Make string coordinate absolute
  295. *
  296. * @param string $pCoordinateString
  297. * @return string Absolute coordinate
  298. * @throws Exception
  299. */
  300. public static function absoluteCoordinate($pCoordinateString = 'A1')
  301. {
  302. if (!eregi(':', $pCoordinateString)) {
  303. // Return value
  304. $returnValue = '';
  305. // Create absolute coordinate
  306. list($column, $row) = PHPExcel_Cell::coordinateFromString($pCoordinateString);
  307. $returnValue = '$' . $column . '$' . $row;
  308. // Return
  309. return $returnValue;
  310. } else {
  311. throw new Exception("Coordinate string should not be a cell range.");
  312. }
  313. }
  314. /**
  315. * Split range into coordinate strings
  316. *
  317. * @param string $pRange
  318. * @return array Array containg two coordinate strings
  319. */
  320. public static function splitRange($pRange = 'A1:A1')
  321. {
  322. return explode(':', $pRange);
  323. }
  324. /**
  325. * Calculate range dimension
  326. *
  327. * @param string $pRange Cell range (e.g. A1:A1)
  328. * @return array Range dimension (width, height)
  329. */
  330. public static function rangeDimension($pRange = 'A1:A1')
  331. {
  332. // Uppercase coordinate
  333. $pRange = strtoupper($pRange);
  334. // Extract range
  335. $rangeA = '';
  336. $rangeB = '';
  337. if (strpos($pRange, ':') === false) {
  338. $rangeA = $pRange;
  339. $rangeB = $pRange;
  340. } else {
  341. list($rangeA, $rangeB) = explode(':', $pRange);
  342. }
  343. // Calculate range outer borders
  344. $rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
  345. $rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
  346. // Translate column into index
  347. $rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]);
  348. $rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]);
  349. return array( ($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
  350. }
  351. /**
  352. * Columnindex from string
  353. *
  354. * @param string $pString
  355. * @return int Column index (base 1 !!!)
  356. * @throws Exception
  357. */
  358. public static function columnIndexFromString($pString = 'A')
  359. {
  360. // Convert to uppercase
  361. $pString = strtoupper($pString);
  362. // Convert column to integer
  363. if (strlen($pString) == 1) {
  364. $result = 0;
  365. $result += (ord(substr($pString, 0, 1)) - 65);
  366. $result += 1;
  367. return $result;
  368. } else if (strlen($pString) == 2) {
  369. $result = 0;
  370. $result += ( (1 + (ord(substr($pString, 0, 1)) - 65) ) * 26);
  371. $result += (ord(substr($pString, 1, 2)) - 65);
  372. $result += 1;
  373. return $result;
  374. } else {
  375. throw new Exception("Column string index can not be longer than 2 characters.");
  376. }
  377. }
  378. /**
  379. * String from columnindex
  380. *
  381. * @param int $pColumnIndex Column index (base 0 !!!)
  382. * @return string
  383. */
  384. public static function stringFromColumnIndex($pColumnIndex = 0)
  385. {
  386. // Convert column to string
  387. $returnValue = '';
  388. // Determine column string
  389. if ($pColumnIndex < 26) {
  390. $returnValue = chr(65 + $pColumnIndex);
  391. } else {
  392. $iRemainder = (int)($pColumnIndex / 26) -1;
  393. $returnValue = PHPExcel_Cell::stringFromColumnIndex( $iRemainder ).chr(65 + $pColumnIndex%26) ;
  394. }
  395. // Return
  396. return $returnValue;
  397. }
  398. /**
  399. * Compare 2 cells
  400. *
  401. * @param PHPExcel_Cell $a Cell a
  402. * @param PHPExcel_Cell $a Cell b
  403. * @return int Result of comparison (always -1 or 1, never zero!)
  404. */
  405. public static function compareCells($a, $b)
  406. {
  407. if ($a->getRow() < $b->getRow()) {
  408. return -1;
  409. } elseif ($a->getRow() > $b->getRow()) {
  410. return 1;
  411. } elseif (PHPExcel_Cell::columnIndexFromString($a->getColumn()) < PHPExcel_Cell::columnIndexFromString($b->getColumn())) {
  412. return -1;
  413. } else {
  414. return 1;
  415. }
  416. }
  417. /**
  418. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  419. */
  420. public function __clone() {
  421. $vars = get_object_vars($this);
  422. foreach ($vars as $key => $value) {
  423. if (is_object($value)) {
  424. $this->$key = clone $value;
  425. } else {
  426. $this->$key = $value;
  427. }
  428. }
  429. }
  430. }