PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/PHPExcel/Classes/PHPExcel/Cell.php

https://bitbucket.org/georgikirow/ksk
PHP | 978 lines | 457 code | 115 blank | 406 comment | 70 complexity | e8f19c01982a814e788753341bcc9d9c MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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_Cell
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_Cell
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Cell
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Cell
  35. {
  36. /**
  37. * Default range variable constant
  38. *
  39. * @var string
  40. */
  41. const DEFAULT_RANGE = 'A1:A1';
  42. /**
  43. * Value binder to use
  44. *
  45. * @var PHPExcel_Cell_IValueBinder
  46. */
  47. private static $_valueBinder = NULL;
  48. /**
  49. * Value of the cell
  50. *
  51. * @var mixed
  52. */
  53. private $_value;
  54. /**
  55. * Calculated value of the cell (used for caching)
  56. * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to
  57. * create the original spreadsheet file.
  58. * Note that this value is not guaranteed to reflect the actual calculated value because it is
  59. * possible that auto-calculation was disabled in the original spreadsheet, and underlying data
  60. * values used by the formula have changed since it was last calculated.
  61. *
  62. * @var mixed
  63. */
  64. private $_calculatedValue = NULL;
  65. /**
  66. * Type of the cell data
  67. *
  68. * @var string
  69. */
  70. private $_dataType;
  71. /**
  72. * Parent worksheet
  73. *
  74. * @var PHPExcel_CachedObjectStorage_CacheBase
  75. */
  76. private $_parent;
  77. /**
  78. * Index to cellXf
  79. *
  80. * @var int
  81. */
  82. private $_xfIndex;
  83. /**
  84. * Attributes of the formula
  85. *
  86. */
  87. private $_formulaAttributes;
  88. /**
  89. * Send notification to the cache controller
  90. *
  91. * @return void
  92. **/
  93. public function notifyCacheController() {
  94. $this->_parent->updateCacheData($this);
  95. return $this;
  96. }
  97. public function detach() {
  98. $this->_parent = NULL;
  99. }
  100. public function attach(PHPExcel_CachedObjectStorage_CacheBase $parent) {
  101. $this->_parent = $parent;
  102. }
  103. /**
  104. * Create a new Cell
  105. *
  106. * @param mixed $pValue
  107. * @param string $pDataType
  108. * @param PHPExcel_Worksheet $pSheet
  109. * @throws PHPExcel_Exception
  110. */
  111. public function __construct($pValue = NULL, $pDataType = NULL, PHPExcel_Worksheet $pSheet = NULL)
  112. {
  113. // Initialise cell value
  114. $this->_value = $pValue;
  115. // Set worksheet cache
  116. $this->_parent = $pSheet->getCellCacheController();
  117. // Set datatype?
  118. if ($pDataType !== NULL) {
  119. if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2)
  120. $pDataType = PHPExcel_Cell_DataType::TYPE_STRING;
  121. $this->_dataType = $pDataType;
  122. } else {
  123. if (!self::getValueBinder()->bindValue($this, $pValue)) {
  124. throw new PHPExcel_Exception("Value could not be bound to cell.");
  125. }
  126. }
  127. // set default index to cellXf
  128. $this->_xfIndex = 0;
  129. }
  130. /**
  131. * Get cell coordinate column
  132. *
  133. * @return string
  134. */
  135. public function getColumn()
  136. {
  137. return $this->_parent->getCurrentColumn();
  138. }
  139. /**
  140. * Get cell coordinate row
  141. *
  142. * @return int
  143. */
  144. public function getRow()
  145. {
  146. return $this->_parent->getCurrentRow();
  147. }
  148. /**
  149. * Get cell coordinate
  150. *
  151. * @return string
  152. */
  153. public function getCoordinate()
  154. {
  155. return $this->_parent->getCurrentAddress();
  156. }
  157. /**
  158. * Get cell value
  159. *
  160. * @return mixed
  161. */
  162. public function getValue()
  163. {
  164. return $this->_value;
  165. }
  166. /**
  167. * Get cell value with formatting
  168. *
  169. * @return string
  170. */
  171. public function getFormattedValue()
  172. {
  173. return (string) PHPExcel_Style_NumberFormat::toFormattedString(
  174. $this->getCalculatedValue(),
  175. $this->getWorksheet()->getParent()->getCellXfByIndex($this->getXfIndex())
  176. ->getNumberFormat()->getFormatCode()
  177. );
  178. }
  179. /**
  180. * Set cell value
  181. *
  182. * Sets the value for a cell, automatically determining the datatype using the value binder
  183. *
  184. * @param mixed $pValue Value
  185. * @return PHPExcel_Cell
  186. * @throws PHPExcel_Exception
  187. */
  188. public function setValue($pValue = NULL)
  189. {
  190. if (!self::getValueBinder()->bindValue($this, $pValue)) {
  191. throw new PHPExcel_Exception("Value could not be bound to cell.");
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Set the value for a cell, with the explicit data type passed to the method (bypassing any use of the value binder)
  197. *
  198. * @param mixed $pValue Value
  199. * @param string $pDataType Explicit data type
  200. * @return PHPExcel_Cell
  201. * @throws PHPExcel_Exception
  202. */
  203. public function setValueExplicit($pValue = NULL, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  204. {
  205. // set the value according to data type
  206. switch ($pDataType) {
  207. case PHPExcel_Cell_DataType::TYPE_STRING2:
  208. $pDataType = PHPExcel_Cell_DataType::TYPE_STRING;
  209. case PHPExcel_Cell_DataType::TYPE_STRING:
  210. case PHPExcel_Cell_DataType::TYPE_NULL:
  211. case PHPExcel_Cell_DataType::TYPE_INLINE:
  212. $this->_value = PHPExcel_Cell_DataType::checkString($pValue);
  213. break;
  214. case PHPExcel_Cell_DataType::TYPE_NUMERIC:
  215. $this->_value = (float)$pValue;
  216. break;
  217. case PHPExcel_Cell_DataType::TYPE_FORMULA:
  218. $this->_value = (string)$pValue;
  219. break;
  220. case PHPExcel_Cell_DataType::TYPE_BOOL:
  221. $this->_value = (bool)$pValue;
  222. break;
  223. case PHPExcel_Cell_DataType::TYPE_ERROR:
  224. $this->_value = PHPExcel_Cell_DataType::checkErrorCode($pValue);
  225. break;
  226. default:
  227. throw new PHPExcel_Exception('Invalid datatype: ' . $pDataType);
  228. break;
  229. }
  230. // set the datatype
  231. $this->_dataType = $pDataType;
  232. return $this->notifyCacheController();
  233. }
  234. /**
  235. * Get calculated cell value
  236. *
  237. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  238. *
  239. * @param boolean $resetLog Whether the calculation engine logger should be reset or not
  240. * @return mixed
  241. * @throws PHPExcel_Exception
  242. */
  243. public function getCalculatedValue($resetLog = TRUE)
  244. {
  245. //echo 'Cell '.$this->getCoordinate().' value is a '.$this->_dataType.' with a value of '.$this->getValue().PHP_EOL;
  246. if ($this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA) {
  247. try {
  248. //echo 'Cell value for '.$this->getCoordinate().' is a formula: Calculating value'.PHP_EOL;
  249. $result = PHPExcel_Calculation::getInstance(
  250. $this->getWorksheet()->getParent()
  251. )->calculateCellValue($this,$resetLog);
  252. //echo $this->getCoordinate().' calculation result is '.$result.PHP_EOL;
  253. // We don't yet handle array returns
  254. if (is_array($result)) {
  255. while (is_array($result)) {
  256. $result = array_pop($result);
  257. }
  258. }
  259. } catch ( PHPExcel_Exception $ex ) {
  260. if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->_calculatedValue !== NULL)) {
  261. //echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().PHP_EOL;
  262. return $this->_calculatedValue; // Fallback for calculations referencing external files.
  263. }
  264. //echo 'Calculation Exception: '.$ex->getMessage().PHP_EOL;
  265. $result = '#N/A';
  266. throw new PHPExcel_Calculation_Exception(
  267. $this->getWorksheet()->getTitle().'!'.$this->getCoordinate().' -> '.$ex->getMessage()
  268. );
  269. }
  270. if ($result === '#Not Yet Implemented') {
  271. //echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().PHP_EOL;
  272. return $this->_calculatedValue; // Fallback if calculation engine does not support the formula.
  273. }
  274. //echo 'Returning calculated value of '.$result.' for cell '.$this->getCoordinate().PHP_EOL;
  275. return $result;
  276. } elseif($this->_value instanceof PHPExcel_RichText) {
  277. // echo 'Cell value for '.$this->getCoordinate().' is rich text: Returning data value of '.$this->_value.'<br />';
  278. return $this->_value->getPlainText();
  279. }
  280. // echo 'Cell value for '.$this->getCoordinate().' is not a formula: Returning data value of '.$this->_value.'<br />';
  281. return $this->_value;
  282. }
  283. /**
  284. * Set old calculated value (cached)
  285. *
  286. * @param mixed $pValue Value
  287. * @return PHPExcel_Cell
  288. */
  289. public function setCalculatedValue($pValue = NULL)
  290. {
  291. if ($pValue !== NULL) {
  292. $this->_calculatedValue = (is_numeric($pValue)) ? (float) $pValue : $pValue;
  293. }
  294. return $this->notifyCacheController();
  295. }
  296. /**
  297. * Get old calculated value (cached)
  298. * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to
  299. * create the original spreadsheet file.
  300. * Note that this value is not guaranteed to refelect the actual calculated value because it is
  301. * possible that auto-calculation was disabled in the original spreadsheet, and underlying data
  302. * values used by the formula have changed since it was last calculated.
  303. *
  304. * @return mixed
  305. */
  306. public function getOldCalculatedValue()
  307. {
  308. return $this->_calculatedValue;
  309. }
  310. /**
  311. * Get cell data type
  312. *
  313. * @return string
  314. */
  315. public function getDataType()
  316. {
  317. return $this->_dataType;
  318. }
  319. /**
  320. * Set cell data type
  321. *
  322. * @param string $pDataType
  323. * @return PHPExcel_Cell
  324. */
  325. public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  326. {
  327. if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2)
  328. $pDataType = PHPExcel_Cell_DataType::TYPE_STRING;
  329. $this->_dataType = $pDataType;
  330. return $this->notifyCacheController();
  331. }
  332. /**
  333. * Does this cell contain Data validation rules?
  334. *
  335. * @return boolean
  336. * @throws PHPExcel_Exception
  337. */
  338. public function hasDataValidation()
  339. {
  340. if (!isset($this->_parent)) {
  341. throw new PHPExcel_Exception('Cannot check for data validation when cell is not bound to a worksheet');
  342. }
  343. return $this->getWorksheet()->dataValidationExists($this->getCoordinate());
  344. }
  345. /**
  346. * Get Data validation rules
  347. *
  348. * @return PHPExcel_Cell_DataValidation
  349. * @throws PHPExcel_Exception
  350. */
  351. public function getDataValidation()
  352. {
  353. if (!isset($this->_parent)) {
  354. throw new PHPExcel_Exception('Cannot get data validation for cell that is not bound to a worksheet');
  355. }
  356. return $this->getWorksheet()->getDataValidation($this->getCoordinate());
  357. }
  358. /**
  359. * Set Data validation rules
  360. *
  361. * @param PHPExcel_Cell_DataValidation $pDataValidation
  362. * @return PHPExcel_Cell
  363. * @throws PHPExcel_Exception
  364. */
  365. public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation = NULL)
  366. {
  367. if (!isset($this->_parent)) {
  368. throw new PHPExcel_Exception('Cannot set data validation for cell that is not bound to a worksheet');
  369. }
  370. $this->getWorksheet()->setDataValidation($this->getCoordinate(), $pDataValidation);
  371. return $this->notifyCacheController();
  372. }
  373. /**
  374. * Does this cell contain a Hyperlink?
  375. *
  376. * @return boolean
  377. * @throws PHPExcel_Exception
  378. */
  379. public function hasHyperlink()
  380. {
  381. if (!isset($this->_parent)) {
  382. throw new PHPExcel_Exception('Cannot check for hyperlink when cell is not bound to a worksheet');
  383. }
  384. return $this->getWorksheet()->hyperlinkExists($this->getCoordinate());
  385. }
  386. /**
  387. * Get Hyperlink
  388. *
  389. * @return PHPExcel_Cell_Hyperlink
  390. * @throws PHPExcel_Exception
  391. */
  392. public function getHyperlink()
  393. {
  394. if (!isset($this->_parent)) {
  395. throw new PHPExcel_Exception('Cannot get hyperlink for cell that is not bound to a worksheet');
  396. }
  397. return $this->getWorksheet()->getHyperlink($this->getCoordinate());
  398. }
  399. /**
  400. * Set Hyperlink
  401. *
  402. * @param PHPExcel_Cell_Hyperlink $pHyperlink
  403. * @return PHPExcel_Cell
  404. * @throws PHPExcel_Exception
  405. */
  406. public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = NULL)
  407. {
  408. if (!isset($this->_parent)) {
  409. throw new PHPExcel_Exception('Cannot set hyperlink for cell that is not bound to a worksheet');
  410. }
  411. $this->getWorksheet()->setHyperlink($this->getCoordinate(), $pHyperlink);
  412. return $this->notifyCacheController();
  413. }
  414. /**
  415. * Get parent worksheet
  416. *
  417. * @return PHPExcel_Worksheet
  418. */
  419. public function getParent() {
  420. return $this->_parent;
  421. }
  422. /**
  423. * Get parent worksheet
  424. *
  425. * @return PHPExcel_Worksheet
  426. */
  427. public function getWorksheet() {
  428. return $this->_parent->getParent();
  429. }
  430. /**
  431. * Get cell style
  432. *
  433. * @return PHPExcel_Style
  434. */
  435. public function getStyle()
  436. {
  437. return $this->getWorksheet()->getParent()->getCellXfByIndex($this->getXfIndex());
  438. }
  439. /**
  440. * Re-bind parent
  441. *
  442. * @param PHPExcel_Worksheet $parent
  443. * @return PHPExcel_Cell
  444. */
  445. public function rebindParent(PHPExcel_Worksheet $parent) {
  446. $this->_parent = $parent->getCellCacheController();
  447. return $this->notifyCacheController();
  448. }
  449. /**
  450. * Is cell in a specific range?
  451. *
  452. * @param string $pRange Cell range (e.g. A1:A1)
  453. * @return boolean
  454. */
  455. public function isInRange($pRange = 'A1:A1')
  456. {
  457. list($rangeStart,$rangeEnd) = self::rangeBoundaries($pRange);
  458. // Translate properties
  459. $myColumn = self::columnIndexFromString($this->getColumn());
  460. $myRow = $this->getRow();
  461. // Verify if cell is in range
  462. return (($rangeStart[0] <= $myColumn) && ($rangeEnd[0] >= $myColumn) &&
  463. ($rangeStart[1] <= $myRow) && ($rangeEnd[1] >= $myRow)
  464. );
  465. }
  466. /**
  467. * Coordinate from string
  468. *
  469. * @param string $pCoordinateString
  470. * @return array Array containing column and row (indexes 0 and 1)
  471. * @throws PHPExcel_Exception
  472. */
  473. public static function coordinateFromString($pCoordinateString = 'A1')
  474. {
  475. if (preg_match("/^([$]?[A-Z]{1,3})([$]?\d{1,7})$/", $pCoordinateString, $matches)) {
  476. return array($matches[1],$matches[2]);
  477. } elseif ((strpos($pCoordinateString,':') !== FALSE) || (strpos($pCoordinateString,',') !== FALSE)) {
  478. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  479. } elseif ($pCoordinateString == '') {
  480. throw new PHPExcel_Exception('Cell coordinate can not be zero-length string');
  481. }
  482. throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString);
  483. }
  484. /**
  485. * Make string row, column or cell coordinate absolute
  486. *
  487. * @param string $pCoordinateString e.g. 'A' or '1' or 'A1'
  488. * Note that this value can be a row or column reference as well as a cell reference
  489. * @return string Absolute coordinate e.g. '$A' or '$1' or '$A$1'
  490. * @throws PHPExcel_Exception
  491. */
  492. public static function absoluteReference($pCoordinateString = 'A1')
  493. {
  494. if (strpos($pCoordinateString,':') === FALSE && strpos($pCoordinateString,',') === FALSE) {
  495. // Split out any worksheet name from the reference
  496. $worksheet = '';
  497. $cellAddress = explode('!',$pCoordinateString);
  498. if (count($cellAddress) > 1) {
  499. list($worksheet,$pCoordinateString) = $cellAddress;
  500. }
  501. if ($worksheet > '') $worksheet .= '!';
  502. // Create absolute coordinate
  503. if (ctype_digit($pCoordinateString)) {
  504. return $worksheet . '$' . $pCoordinateString;
  505. } elseif (ctype_alpha($pCoordinateString)) {
  506. return $worksheet . '$' . strtoupper($pCoordinateString);
  507. }
  508. return $worksheet . self::absoluteCoordinate($pCoordinateString);
  509. }
  510. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  511. }
  512. /**
  513. * Make string coordinate absolute
  514. *
  515. * @param string $pCoordinateString e.g. 'A1'
  516. * @return string Absolute coordinate e.g. '$A$1'
  517. * @throws PHPExcel_Exception
  518. */
  519. public static function absoluteCoordinate($pCoordinateString = 'A1')
  520. {
  521. if (strpos($pCoordinateString,':') === FALSE && strpos($pCoordinateString,',') === FALSE) {
  522. // Split out any worksheet name from the coordinate
  523. $worksheet = '';
  524. $cellAddress = explode('!',$pCoordinateString);
  525. if (count($cellAddress) > 1) {
  526. list($worksheet,$pCoordinateString) = $cellAddress;
  527. }
  528. if ($worksheet > '') $worksheet .= '!';
  529. // Create absolute coordinate
  530. list($column, $row) = self::coordinateFromString($pCoordinateString);
  531. $column = ltrim($column,'$');
  532. $row = ltrim($row,'$');
  533. return $worksheet . '$' . $column . '$' . $row;
  534. }
  535. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  536. }
  537. /**
  538. * Split range into coordinate strings
  539. *
  540. * @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'
  541. * @return array Array containg one or more arrays containing one or two coordinate strings
  542. * e.g. array('B4','D9') or array(array('B4','D9'),array('H2','O11'))
  543. * or array('B4')
  544. */
  545. public static function splitRange($pRange = 'A1:A1')
  546. {
  547. // Ensure $pRange is a valid range
  548. if(empty($pRange)) {
  549. $pRange = self::DEFAULT_RANGE;
  550. }
  551. $exploded = explode(',', $pRange);
  552. $counter = count($exploded);
  553. for ($i = 0; $i < $counter; ++$i) {
  554. $exploded[$i] = explode(':', $exploded[$i]);
  555. }
  556. return $exploded;
  557. }
  558. /**
  559. * Build range from coordinate strings
  560. *
  561. * @param array $pRange Array containg one or more arrays containing one or two coordinate strings
  562. * @return string String representation of $pRange
  563. * @throws PHPExcel_Exception
  564. */
  565. public static function buildRange($pRange)
  566. {
  567. // Verify range
  568. if (!is_array($pRange) || empty($pRange) || !is_array($pRange[0])) {
  569. throw new PHPExcel_Exception('Range does not contain any information');
  570. }
  571. // Build range
  572. $imploded = array();
  573. $counter = count($pRange);
  574. for ($i = 0; $i < $counter; ++$i) {
  575. $pRange[$i] = implode(':', $pRange[$i]);
  576. }
  577. $imploded = implode(',', $pRange);
  578. return $imploded;
  579. }
  580. /**
  581. * Calculate range boundaries
  582. *
  583. * @param string $pRange Cell range (e.g. A1:A1)
  584. * @return array Range coordinates array(Start Cell, End Cell)
  585. * where Start Cell and End Cell are arrays (Column Number, Row Number)
  586. */
  587. public static function rangeBoundaries($pRange = 'A1:A1')
  588. {
  589. // Ensure $pRange is a valid range
  590. if(empty($pRange)) {
  591. $pRange = self::DEFAULT_RANGE;
  592. }
  593. // Uppercase coordinate
  594. $pRange = strtoupper($pRange);
  595. // Extract range
  596. if (strpos($pRange, ':') === FALSE) {
  597. $rangeA = $rangeB = $pRange;
  598. } else {
  599. list($rangeA, $rangeB) = explode(':', $pRange);
  600. }
  601. // Calculate range outer borders
  602. $rangeStart = self::coordinateFromString($rangeA);
  603. $rangeEnd = self::coordinateFromString($rangeB);
  604. // Translate column into index
  605. $rangeStart[0] = self::columnIndexFromString($rangeStart[0]);
  606. $rangeEnd[0] = self::columnIndexFromString($rangeEnd[0]);
  607. return array($rangeStart, $rangeEnd);
  608. }
  609. /**
  610. * Calculate range dimension
  611. *
  612. * @param string $pRange Cell range (e.g. A1:A1)
  613. * @return array Range dimension (width, height)
  614. */
  615. public static function rangeDimension($pRange = 'A1:A1')
  616. {
  617. // Calculate range outer borders
  618. list($rangeStart,$rangeEnd) = self::rangeBoundaries($pRange);
  619. return array( ($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
  620. }
  621. /**
  622. * Calculate range boundaries
  623. *
  624. * @param string $pRange Cell range (e.g. A1:A1)
  625. * @return array Range coordinates array(Start Cell, End Cell)
  626. * where Start Cell and End Cell are arrays (Column ID, Row Number)
  627. */
  628. public static function getRangeBoundaries($pRange = 'A1:A1')
  629. {
  630. // Ensure $pRange is a valid range
  631. if(empty($pRange)) {
  632. $pRange = self::DEFAULT_RANGE;
  633. }
  634. // Uppercase coordinate
  635. $pRange = strtoupper($pRange);
  636. // Extract range
  637. if (strpos($pRange, ':') === FALSE) {
  638. $rangeA = $rangeB = $pRange;
  639. } else {
  640. list($rangeA, $rangeB) = explode(':', $pRange);
  641. }
  642. return array( self::coordinateFromString($rangeA), self::coordinateFromString($rangeB));
  643. }
  644. /**
  645. * Column index from string
  646. *
  647. * @param string $pString
  648. * @return int Column index (base 1 !!!)
  649. */
  650. public static function columnIndexFromString($pString = 'A')
  651. {
  652. // Using a lookup cache adds a slight memory overhead, but boosts speed
  653. // caching using a static within the method is faster than a class static,
  654. // though it's additional memory overhead
  655. static $_indexCache = array();
  656. if (isset($_indexCache[$pString]))
  657. return $_indexCache[$pString];
  658. // It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord()
  659. // and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant
  660. // memory overhead either
  661. static $_columnLookup = array(
  662. 'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13,
  663. 'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26,
  664. 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13,
  665. 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26
  666. );
  667. // We also use the language construct isset() rather than the more costly strlen() function to match the length of $pString
  668. // for improved performance
  669. if (isset($pString{0})) {
  670. if (!isset($pString{1})) {
  671. $_indexCache[$pString] = $_columnLookup[$pString];
  672. return $_indexCache[$pString];
  673. } elseif(!isset($pString{2})) {
  674. $_indexCache[$pString] = $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}];
  675. return $_indexCache[$pString];
  676. } elseif(!isset($pString{3})) {
  677. $_indexCache[$pString] = $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}];
  678. return $_indexCache[$pString];
  679. }
  680. }
  681. throw new PHPExcel_Exception("Column string index can not be " . ((isset($pString{0})) ? "longer than 3 characters" : "empty"));
  682. }
  683. /**
  684. * String from columnindex
  685. *
  686. * @param int $pColumnIndex Column index (base 0 !!!)
  687. * @return string
  688. */
  689. public static function stringFromColumnIndex($pColumnIndex = 0)
  690. {
  691. // Using a lookup cache adds a slight memory overhead, but boosts speed
  692. // caching using a static within the method is faster than a class static,
  693. // though it's additional memory overhead
  694. static $_indexCache = array();
  695. if (!isset($_indexCache[$pColumnIndex])) {
  696. // Determine column string
  697. if ($pColumnIndex < 26) {
  698. $_indexCache[$pColumnIndex] = chr(65 + $pColumnIndex);
  699. } elseif ($pColumnIndex < 702) {
  700. $_indexCache[$pColumnIndex] = chr(64 + ($pColumnIndex / 26)) .
  701. chr(65 + $pColumnIndex % 26);
  702. } else {
  703. $_indexCache[$pColumnIndex] = chr(64 + (($pColumnIndex - 26) / 676)) .
  704. chr(65 + ((($pColumnIndex - 26) % 676) / 26)) .
  705. chr(65 + $pColumnIndex % 26);
  706. }
  707. }
  708. return $_indexCache[$pColumnIndex];
  709. }
  710. /**
  711. * Extract all cell references in range
  712. *
  713. * @param string $pRange Range (e.g. A1 or A1:C10 or A1:E10 A20:E25)
  714. * @return array Array containing single cell references
  715. */
  716. public static function extractAllCellReferencesInRange($pRange = 'A1') {
  717. // Returnvalue
  718. $returnValue = array();
  719. // Explode spaces
  720. $cellBlocks = explode(' ', str_replace('$', '', strtoupper($pRange)));
  721. foreach ($cellBlocks as $cellBlock) {
  722. // Single cell?
  723. if (strpos($cellBlock,':') === FALSE && strpos($cellBlock,',') === FALSE) {
  724. $returnValue[] = $cellBlock;
  725. continue;
  726. }
  727. // Range...
  728. $ranges = self::splitRange($cellBlock);
  729. foreach($ranges as $range) {
  730. // Single cell?
  731. if (!isset($range[1])) {
  732. $returnValue[] = $range[0];
  733. continue;
  734. }
  735. // Range...
  736. list($rangeStart, $rangeEnd) = $range;
  737. sscanf($rangeStart,'%[A-Z]%d', $startCol, $startRow);
  738. sscanf($rangeEnd,'%[A-Z]%d', $endCol, $endRow);
  739. $endCol++;
  740. // Current data
  741. $currentCol = $startCol;
  742. $currentRow = $startRow;
  743. // Loop cells
  744. while ($currentCol != $endCol) {
  745. while ($currentRow <= $endRow) {
  746. $returnValue[] = $currentCol.$currentRow;
  747. ++$currentRow;
  748. }
  749. ++$currentCol;
  750. $currentRow = $startRow;
  751. }
  752. }
  753. }
  754. // Sort the result by column and row
  755. $sortKeys = array();
  756. foreach (array_unique($returnValue) as $coord) {
  757. sscanf($coord,'%[A-Z]%d', $column, $row);
  758. $sortKeys[sprintf('%3s%09d',$column,$row)] = $coord;
  759. }
  760. ksort($sortKeys);
  761. // Return value
  762. return array_values($sortKeys);
  763. }
  764. /**
  765. * Compare 2 cells
  766. *
  767. * @param PHPExcel_Cell $a Cell a
  768. * @param PHPExcel_Cell $b Cell b
  769. * @return int Result of comparison (always -1 or 1, never zero!)
  770. */
  771. public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
  772. {
  773. if ($a->getRow() < $b->getRow()) {
  774. return -1;
  775. } elseif ($a->getRow() > $b->getRow()) {
  776. return 1;
  777. } elseif (self::columnIndexFromString($a->getColumn()) < self::columnIndexFromString($b->getColumn())) {
  778. return -1;
  779. } else {
  780. return 1;
  781. }
  782. }
  783. /**
  784. * Get value binder to use
  785. *
  786. * @return PHPExcel_Cell_IValueBinder
  787. */
  788. public static function getValueBinder() {
  789. if (self::$_valueBinder === NULL) {
  790. self::$_valueBinder = new PHPExcel_Cell_DefaultValueBinder();
  791. }
  792. return self::$_valueBinder;
  793. }
  794. /**
  795. * Set value binder to use
  796. *
  797. * @param PHPExcel_Cell_IValueBinder $binder
  798. * @throws PHPExcel_Exception
  799. */
  800. public static function setValueBinder(PHPExcel_Cell_IValueBinder $binder = NULL) {
  801. if ($binder === NULL) {
  802. throw new PHPExcel_Exception("A PHPExcel_Cell_IValueBinder is required for PHPExcel to function correctly.");
  803. }
  804. self::$_valueBinder = $binder;
  805. }
  806. /**
  807. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  808. */
  809. public function __clone() {
  810. $vars = get_object_vars($this);
  811. foreach ($vars as $key => $value) {
  812. if ((is_object($value)) && ($key != '_parent')) {
  813. $this->$key = clone $value;
  814. } else {
  815. $this->$key = $value;
  816. }
  817. }
  818. }
  819. /**
  820. * Get index to cellXf
  821. *
  822. * @return int
  823. */
  824. public function getXfIndex()
  825. {
  826. return $this->_xfIndex;
  827. }
  828. /**
  829. * Set index to cellXf
  830. *
  831. * @param int $pValue
  832. * @return PHPExcel_Cell
  833. */
  834. public function setXfIndex($pValue = 0)
  835. {
  836. $this->_xfIndex = $pValue;
  837. return $this->notifyCacheController();
  838. }
  839. /**
  840. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  841. */
  842. public function setFormulaAttributes($pAttributes)
  843. {
  844. $this->_formulaAttributes = $pAttributes;
  845. return $this;
  846. }
  847. /**
  848. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  849. */
  850. public function getFormulaAttributes()
  851. {
  852. return $this->_formulaAttributes;
  853. }
  854. /**
  855. * Convert to string
  856. *
  857. * @return string
  858. */
  859. public function __toString()
  860. {
  861. return (string) $this->getValue();
  862. }
  863. }