Cell.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Cell
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Cell
  32. * @copyright Copyright (c) 2006 - 2014 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_NULL:
  208. $this->_value = $pValue;
  209. break;
  210. case PHPExcel_Cell_DataType::TYPE_STRING2:
  211. $pDataType = PHPExcel_Cell_DataType::TYPE_STRING;
  212. case PHPExcel_Cell_DataType::TYPE_STRING:
  213. case PHPExcel_Cell_DataType::TYPE_INLINE:
  214. $this->_value = PHPExcel_Cell_DataType::checkString($pValue);
  215. break;
  216. case PHPExcel_Cell_DataType::TYPE_NUMERIC:
  217. $this->_value = (float)$pValue;
  218. break;
  219. case PHPExcel_Cell_DataType::TYPE_FORMULA:
  220. $this->_value = (string)$pValue;
  221. break;
  222. case PHPExcel_Cell_DataType::TYPE_BOOL:
  223. $this->_value = (bool)$pValue;
  224. break;
  225. case PHPExcel_Cell_DataType::TYPE_ERROR:
  226. $this->_value = PHPExcel_Cell_DataType::checkErrorCode($pValue);
  227. break;
  228. default:
  229. throw new PHPExcel_Exception('Invalid datatype: ' . $pDataType);
  230. break;
  231. }
  232. // set the datatype
  233. $this->_dataType = $pDataType;
  234. return $this->notifyCacheController();
  235. }
  236. /**
  237. * Get calculated cell value
  238. *
  239. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  240. *
  241. * @param boolean $resetLog Whether the calculation engine logger should be reset or not
  242. * @return mixed
  243. * @throws PHPExcel_Exception
  244. */
  245. public function getCalculatedValue($resetLog = TRUE)
  246. {
  247. //echo 'Cell '.$this->getCoordinate().' value is a '.$this->_dataType.' with a value of '.$this->getValue().PHP_EOL;
  248. if ($this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA) {
  249. try {
  250. //echo 'Cell value for '.$this->getCoordinate().' is a formula: Calculating value'.PHP_EOL;
  251. $result = PHPExcel_Calculation::getInstance(
  252. $this->getWorksheet()->getParent()
  253. )->calculateCellValue($this,$resetLog);
  254. //echo $this->getCoordinate().' calculation result is '.$result.PHP_EOL;
  255. // We don't yet handle array returns
  256. if (is_array($result)) {
  257. while (is_array($result)) {
  258. $result = array_pop($result);
  259. }
  260. }
  261. } catch ( PHPExcel_Exception $ex ) {
  262. if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->_calculatedValue !== NULL)) {
  263. //echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().PHP_EOL;
  264. return $this->_calculatedValue; // Fallback for calculations referencing external files.
  265. }
  266. //echo 'Calculation Exception: '.$ex->getMessage().PHP_EOL;
  267. $result = '#N/A';
  268. throw new PHPExcel_Calculation_Exception(
  269. $this->getWorksheet()->getTitle().'!'.$this->getCoordinate().' -> '.$ex->getMessage()
  270. );
  271. }
  272. if ($result === '#Not Yet Implemented') {
  273. //echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().PHP_EOL;
  274. return $this->_calculatedValue; // Fallback if calculation engine does not support the formula.
  275. }
  276. //echo 'Returning calculated value of '.$result.' for cell '.$this->getCoordinate().PHP_EOL;
  277. return $result;
  278. } elseif($this->_value instanceof PHPExcel_RichText) {
  279. // echo 'Cell value for '.$this->getCoordinate().' is rich text: Returning data value of '.$this->_value.'<br />';
  280. return $this->_value->getPlainText();
  281. }
  282. // echo 'Cell value for '.$this->getCoordinate().' is not a formula: Returning data value of '.$this->_value.'<br />';
  283. return $this->_value;
  284. }
  285. /**
  286. * Set old calculated value (cached)
  287. *
  288. * @param mixed $pValue Value
  289. * @return PHPExcel_Cell
  290. */
  291. public function setCalculatedValue($pValue = NULL)
  292. {
  293. if ($pValue !== NULL) {
  294. $this->_calculatedValue = (is_numeric($pValue)) ? (float) $pValue : $pValue;
  295. }
  296. return $this->notifyCacheController();
  297. }
  298. /**
  299. * Get old calculated value (cached)
  300. * This returns the value last calculated by MS Excel or whichever spreadsheet program was used to
  301. * create the original spreadsheet file.
  302. * Note that this value is not guaranteed to refelect the actual calculated value because it is
  303. * possible that auto-calculation was disabled in the original spreadsheet, and underlying data
  304. * values used by the formula have changed since it was last calculated.
  305. *
  306. * @return mixed
  307. */
  308. public function getOldCalculatedValue()
  309. {
  310. return $this->_calculatedValue;
  311. }
  312. /**
  313. * Get cell data type
  314. *
  315. * @return string
  316. */
  317. public function getDataType()
  318. {
  319. return $this->_dataType;
  320. }
  321. /**
  322. * Set cell data type
  323. *
  324. * @param string $pDataType
  325. * @return PHPExcel_Cell
  326. */
  327. public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  328. {
  329. if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2)
  330. $pDataType = PHPExcel_Cell_DataType::TYPE_STRING;
  331. $this->_dataType = $pDataType;
  332. return $this->notifyCacheController();
  333. }
  334. /**
  335. * Identify if the cell contains a formula
  336. *
  337. * @return boolean
  338. */
  339. public function isFormula()
  340. {
  341. return $this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA;
  342. }
  343. /**
  344. * Does this cell contain Data validation rules?
  345. *
  346. * @return boolean
  347. * @throws PHPExcel_Exception
  348. */
  349. public function hasDataValidation()
  350. {
  351. if (!isset($this->_parent)) {
  352. throw new PHPExcel_Exception('Cannot check for data validation when cell is not bound to a worksheet');
  353. }
  354. return $this->getWorksheet()->dataValidationExists($this->getCoordinate());
  355. }
  356. /**
  357. * Get Data validation rules
  358. *
  359. * @return PHPExcel_Cell_DataValidation
  360. * @throws PHPExcel_Exception
  361. */
  362. public function getDataValidation()
  363. {
  364. if (!isset($this->_parent)) {
  365. throw new PHPExcel_Exception('Cannot get data validation for cell that is not bound to a worksheet');
  366. }
  367. return $this->getWorksheet()->getDataValidation($this->getCoordinate());
  368. }
  369. /**
  370. * Set Data validation rules
  371. *
  372. * @param PHPExcel_Cell_DataValidation $pDataValidation
  373. * @return PHPExcel_Cell
  374. * @throws PHPExcel_Exception
  375. */
  376. public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation = NULL)
  377. {
  378. if (!isset($this->_parent)) {
  379. throw new PHPExcel_Exception('Cannot set data validation for cell that is not bound to a worksheet');
  380. }
  381. $this->getWorksheet()->setDataValidation($this->getCoordinate(), $pDataValidation);
  382. return $this->notifyCacheController();
  383. }
  384. /**
  385. * Does this cell contain a Hyperlink?
  386. *
  387. * @return boolean
  388. * @throws PHPExcel_Exception
  389. */
  390. public function hasHyperlink()
  391. {
  392. if (!isset($this->_parent)) {
  393. throw new PHPExcel_Exception('Cannot check for hyperlink when cell is not bound to a worksheet');
  394. }
  395. return $this->getWorksheet()->hyperlinkExists($this->getCoordinate());
  396. }
  397. /**
  398. * Get Hyperlink
  399. *
  400. * @return PHPExcel_Cell_Hyperlink
  401. * @throws PHPExcel_Exception
  402. */
  403. public function getHyperlink()
  404. {
  405. if (!isset($this->_parent)) {
  406. throw new PHPExcel_Exception('Cannot get hyperlink for cell that is not bound to a worksheet');
  407. }
  408. return $this->getWorksheet()->getHyperlink($this->getCoordinate());
  409. }
  410. /**
  411. * Set Hyperlink
  412. *
  413. * @param PHPExcel_Cell_Hyperlink $pHyperlink
  414. * @return PHPExcel_Cell
  415. * @throws PHPExcel_Exception
  416. */
  417. public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = NULL)
  418. {
  419. if (!isset($this->_parent)) {
  420. throw new PHPExcel_Exception('Cannot set hyperlink for cell that is not bound to a worksheet');
  421. }
  422. $this->getWorksheet()->setHyperlink($this->getCoordinate(), $pHyperlink);
  423. return $this->notifyCacheController();
  424. }
  425. /**
  426. * Get parent worksheet
  427. *
  428. * @return PHPExcel_CachedObjectStorage_CacheBase
  429. */
  430. public function getParent() {
  431. return $this->_parent;
  432. }
  433. /**
  434. * Get parent worksheet
  435. *
  436. * @return PHPExcel_Worksheet
  437. */
  438. public function getWorksheet() {
  439. return $this->_parent->getParent();
  440. }
  441. /**
  442. * Get cell style
  443. *
  444. * @return PHPExcel_Style
  445. */
  446. public function getStyle()
  447. {
  448. return $this->getWorksheet()->getParent()->getCellXfByIndex($this->getXfIndex());
  449. }
  450. /**
  451. * Re-bind parent
  452. *
  453. * @param PHPExcel_Worksheet $parent
  454. * @return PHPExcel_Cell
  455. */
  456. public function rebindParent(PHPExcel_Worksheet $parent) {
  457. $this->_parent = $parent->getCellCacheController();
  458. return $this->notifyCacheController();
  459. }
  460. /**
  461. * Is cell in a specific range?
  462. *
  463. * @param string $pRange Cell range (e.g. A1:A1)
  464. * @return boolean
  465. */
  466. public function isInRange($pRange = 'A1:A1')
  467. {
  468. list($rangeStart,$rangeEnd) = self::rangeBoundaries($pRange);
  469. // Translate properties
  470. $myColumn = self::columnIndexFromString($this->getColumn());
  471. $myRow = $this->getRow();
  472. // Verify if cell is in range
  473. return (($rangeStart[0] <= $myColumn) && ($rangeEnd[0] >= $myColumn) &&
  474. ($rangeStart[1] <= $myRow) && ($rangeEnd[1] >= $myRow)
  475. );
  476. }
  477. /**
  478. * Coordinate from string
  479. *
  480. * @param string $pCoordinateString
  481. * @return array Array containing column and row (indexes 0 and 1)
  482. * @throws PHPExcel_Exception
  483. */
  484. public static function coordinateFromString($pCoordinateString = 'A1')
  485. {
  486. if (preg_match("/^([$]?[A-Z]{1,3})([$]?\d{1,7})$/", $pCoordinateString, $matches)) {
  487. return array($matches[1],$matches[2]);
  488. } elseif ((strpos($pCoordinateString,':') !== FALSE) || (strpos($pCoordinateString,',') !== FALSE)) {
  489. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  490. } elseif ($pCoordinateString == '') {
  491. throw new PHPExcel_Exception('Cell coordinate can not be zero-length string');
  492. }
  493. throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString);
  494. }
  495. /**
  496. * Make string row, column or cell coordinate absolute
  497. *
  498. * @param string $pCoordinateString e.g. 'A' or '1' or 'A1'
  499. * Note that this value can be a row or column reference as well as a cell reference
  500. * @return string Absolute coordinate e.g. '$A' or '$1' or '$A$1'
  501. * @throws PHPExcel_Exception
  502. */
  503. public static function absoluteReference($pCoordinateString = 'A1')
  504. {
  505. if (strpos($pCoordinateString,':') === FALSE && strpos($pCoordinateString,',') === FALSE) {
  506. // Split out any worksheet name from the reference
  507. $worksheet = '';
  508. $cellAddress = explode('!',$pCoordinateString);
  509. if (count($cellAddress) > 1) {
  510. list($worksheet,$pCoordinateString) = $cellAddress;
  511. }
  512. if ($worksheet > '') $worksheet .= '!';
  513. // Create absolute coordinate
  514. if (ctype_digit($pCoordinateString)) {
  515. return $worksheet . '$' . $pCoordinateString;
  516. } elseif (ctype_alpha($pCoordinateString)) {
  517. return $worksheet . '$' . strtoupper($pCoordinateString);
  518. }
  519. return $worksheet . self::absoluteCoordinate($pCoordinateString);
  520. }
  521. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  522. }
  523. /**
  524. * Make string coordinate absolute
  525. *
  526. * @param string $pCoordinateString e.g. 'A1'
  527. * @return string Absolute coordinate e.g. '$A$1'
  528. * @throws PHPExcel_Exception
  529. */
  530. public static function absoluteCoordinate($pCoordinateString = 'A1')
  531. {
  532. if (strpos($pCoordinateString,':') === FALSE && strpos($pCoordinateString,',') === FALSE) {
  533. // Split out any worksheet name from the coordinate
  534. $worksheet = '';
  535. $cellAddress = explode('!',$pCoordinateString);
  536. if (count($cellAddress) > 1) {
  537. list($worksheet,$pCoordinateString) = $cellAddress;
  538. }
  539. if ($worksheet > '') $worksheet .= '!';
  540. // Create absolute coordinate
  541. list($column, $row) = self::coordinateFromString($pCoordinateString);
  542. $column = ltrim($column,'$');
  543. $row = ltrim($row,'$');
  544. return $worksheet . '$' . $column . '$' . $row;
  545. }
  546. throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells');
  547. }
  548. /**
  549. * Split range into coordinate strings
  550. *
  551. * @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'
  552. * @return array Array containg one or more arrays containing one or two coordinate strings
  553. * e.g. array('B4','D9') or array(array('B4','D9'),array('H2','O11'))
  554. * or array('B4')
  555. */
  556. public static function splitRange($pRange = 'A1:A1')
  557. {
  558. // Ensure $pRange is a valid range
  559. if(empty($pRange)) {
  560. $pRange = self::DEFAULT_RANGE;
  561. }
  562. $exploded = explode(',', $pRange);
  563. $counter = count($exploded);
  564. for ($i = 0; $i < $counter; ++$i) {
  565. $exploded[$i] = explode(':', $exploded[$i]);
  566. }
  567. return $exploded;
  568. }
  569. /**
  570. * Build range from coordinate strings
  571. *
  572. * @param array $pRange Array containg one or more arrays containing one or two coordinate strings
  573. * @return string String representation of $pRange
  574. * @throws PHPExcel_Exception
  575. */
  576. public static function buildRange($pRange)
  577. {
  578. // Verify range
  579. if (!is_array($pRange) || empty($pRange) || !is_array($pRange[0])) {
  580. throw new PHPExcel_Exception('Range does not contain any information');
  581. }
  582. // Build range
  583. $imploded = array();
  584. $counter = count($pRange);
  585. for ($i = 0; $i < $counter; ++$i) {
  586. $pRange[$i] = implode(':', $pRange[$i]);
  587. }
  588. $imploded = implode(',', $pRange);
  589. return $imploded;
  590. }
  591. /**
  592. * Calculate range boundaries
  593. *
  594. * @param string $pRange Cell range (e.g. A1:A1)
  595. * @return array Range coordinates array(Start Cell, End Cell)
  596. * where Start Cell and End Cell are arrays (Column Number, Row Number)
  597. */
  598. public static function rangeBoundaries($pRange = 'A1:A1')
  599. {
  600. // Ensure $pRange is a valid range
  601. if(empty($pRange)) {
  602. $pRange = self::DEFAULT_RANGE;
  603. }
  604. // Uppercase coordinate
  605. $pRange = strtoupper($pRange);
  606. // Extract range
  607. if (strpos($pRange, ':') === FALSE) {
  608. $rangeA = $rangeB = $pRange;
  609. } else {
  610. list($rangeA, $rangeB) = explode(':', $pRange);
  611. }
  612. // Calculate range outer borders
  613. $rangeStart = self::coordinateFromString($rangeA);
  614. $rangeEnd = self::coordinateFromString($rangeB);
  615. // Translate column into index
  616. $rangeStart[0] = self::columnIndexFromString($rangeStart[0]);
  617. $rangeEnd[0] = self::columnIndexFromString($rangeEnd[0]);
  618. return array($rangeStart, $rangeEnd);
  619. }
  620. /**
  621. * Calculate range dimension
  622. *
  623. * @param string $pRange Cell range (e.g. A1:A1)
  624. * @return array Range dimension (width, height)
  625. */
  626. public static function rangeDimension($pRange = 'A1:A1')
  627. {
  628. // Calculate range outer borders
  629. list($rangeStart,$rangeEnd) = self::rangeBoundaries($pRange);
  630. return array( ($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
  631. }
  632. /**
  633. * Calculate range boundaries
  634. *
  635. * @param string $pRange Cell range (e.g. A1:A1)
  636. * @return array Range coordinates array(Start Cell, End Cell)
  637. * where Start Cell and End Cell are arrays (Column ID, Row Number)
  638. */
  639. public static function getRangeBoundaries($pRange = 'A1:A1')
  640. {
  641. // Ensure $pRange is a valid range
  642. if(empty($pRange)) {
  643. $pRange = self::DEFAULT_RANGE;
  644. }
  645. // Uppercase coordinate
  646. $pRange = strtoupper($pRange);
  647. // Extract range
  648. if (strpos($pRange, ':') === FALSE) {
  649. $rangeA = $rangeB = $pRange;
  650. } else {
  651. list($rangeA, $rangeB) = explode(':', $pRange);
  652. }
  653. return array( self::coordinateFromString($rangeA), self::coordinateFromString($rangeB));
  654. }
  655. /**
  656. * Column index from string
  657. *
  658. * @param string $pString
  659. * @return int Column index (base 1 !!!)
  660. */
  661. public static function columnIndexFromString($pString = 'A')
  662. {
  663. // Using a lookup cache adds a slight memory overhead, but boosts speed
  664. // caching using a static within the method is faster than a class static,
  665. // though it's additional memory overhead
  666. static $_indexCache = array();
  667. if (isset($_indexCache[$pString]))
  668. return $_indexCache[$pString];
  669. // It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord()
  670. // and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant
  671. // memory overhead either
  672. static $_columnLookup = array(
  673. '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,
  674. '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,
  675. '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,
  676. '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
  677. );
  678. // We also use the language construct isset() rather than the more costly strlen() function to match the length of $pString
  679. // for improved performance
  680. if (isset($pString{0})) {
  681. if (!isset($pString{1})) {
  682. $_indexCache[$pString] = $_columnLookup[$pString];
  683. return $_indexCache[$pString];
  684. } elseif(!isset($pString{2})) {
  685. $_indexCache[$pString] = $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}];
  686. return $_indexCache[$pString];
  687. } elseif(!isset($pString{3})) {
  688. $_indexCache[$pString] = $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}];
  689. return $_indexCache[$pString];
  690. }
  691. }
  692. throw new PHPExcel_Exception("Column string index can not be " . ((isset($pString{0})) ? "longer than 3 characters" : "empty"));
  693. }
  694. /**
  695. * String from columnindex
  696. *
  697. * @param int $pColumnIndex Column index (base 0 !!!)
  698. * @return string
  699. */
  700. public static function stringFromColumnIndex($pColumnIndex = 0)
  701. {
  702. // Using a lookup cache adds a slight memory overhead, but boosts speed
  703. // caching using a static within the method is faster than a class static,
  704. // though it's additional memory overhead
  705. static $_indexCache = array();
  706. if (!isset($_indexCache[$pColumnIndex])) {
  707. // Determine column string
  708. if ($pColumnIndex < 26) {
  709. $_indexCache[$pColumnIndex] = chr(65 + $pColumnIndex);
  710. } elseif ($pColumnIndex < 702) {
  711. $_indexCache[$pColumnIndex] = chr(64 + ($pColumnIndex / 26)) .
  712. chr(65 + $pColumnIndex % 26);
  713. } else {
  714. $_indexCache[$pColumnIndex] = chr(64 + (($pColumnIndex - 26) / 676)) .
  715. chr(65 + ((($pColumnIndex - 26) % 676) / 26)) .
  716. chr(65 + $pColumnIndex % 26);
  717. }
  718. }
  719. return $_indexCache[$pColumnIndex];
  720. }
  721. /**
  722. * Extract all cell references in range
  723. *
  724. * @param string $pRange Range (e.g. A1 or A1:C10 or A1:E10 A20:E25)
  725. * @return array Array containing single cell references
  726. */
  727. public static function extractAllCellReferencesInRange($pRange = 'A1') {
  728. // Returnvalue
  729. $returnValue = array();
  730. // Explode spaces
  731. $cellBlocks = explode(' ', str_replace('$', '', strtoupper($pRange)));
  732. foreach ($cellBlocks as $cellBlock) {
  733. // Single cell?
  734. if (strpos($cellBlock,':') === FALSE && strpos($cellBlock,',') === FALSE) {
  735. $returnValue[] = $cellBlock;
  736. continue;
  737. }
  738. // Range...
  739. $ranges = self::splitRange($cellBlock);
  740. foreach($ranges as $range) {
  741. // Single cell?
  742. if (!isset($range[1])) {
  743. $returnValue[] = $range[0];
  744. continue;
  745. }
  746. // Range...
  747. list($rangeStart, $rangeEnd) = $range;
  748. sscanf($rangeStart,'%[A-Z]%d', $startCol, $startRow);
  749. sscanf($rangeEnd,'%[A-Z]%d', $endCol, $endRow);
  750. $endCol++;
  751. // Current data
  752. $currentCol = $startCol;
  753. $currentRow = $startRow;
  754. // Loop cells
  755. while ($currentCol != $endCol) {
  756. while ($currentRow <= $endRow) {
  757. $returnValue[] = $currentCol.$currentRow;
  758. ++$currentRow;
  759. }
  760. ++$currentCol;
  761. $currentRow = $startRow;
  762. }
  763. }
  764. }
  765. // Sort the result by column and row
  766. $sortKeys = array();
  767. foreach (array_unique($returnValue) as $coord) {
  768. sscanf($coord,'%[A-Z]%d', $column, $row);
  769. $sortKeys[sprintf('%3s%09d',$column,$row)] = $coord;
  770. }
  771. ksort($sortKeys);
  772. // Return value
  773. return array_values($sortKeys);
  774. }
  775. /**
  776. * Compare 2 cells
  777. *
  778. * @param PHPExcel_Cell $a Cell a
  779. * @param PHPExcel_Cell $b Cell b
  780. * @return int Result of comparison (always -1 or 1, never zero!)
  781. */
  782. public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
  783. {
  784. if ($a->getRow() < $b->getRow()) {
  785. return -1;
  786. } elseif ($a->getRow() > $b->getRow()) {
  787. return 1;
  788. } elseif (self::columnIndexFromString($a->getColumn()) < self::columnIndexFromString($b->getColumn())) {
  789. return -1;
  790. } else {
  791. return 1;
  792. }
  793. }
  794. /**
  795. * Get value binder to use
  796. *
  797. * @return PHPExcel_Cell_IValueBinder
  798. */
  799. public static function getValueBinder() {
  800. if (self::$_valueBinder === NULL) {
  801. self::$_valueBinder = new PHPExcel_Cell_DefaultValueBinder();
  802. }
  803. return self::$_valueBinder;
  804. }
  805. /**
  806. * Set value binder to use
  807. *
  808. * @param PHPExcel_Cell_IValueBinder $binder
  809. * @throws PHPExcel_Exception
  810. */
  811. public static function setValueBinder(PHPExcel_Cell_IValueBinder $binder = NULL) {
  812. if ($binder === NULL) {
  813. throw new PHPExcel_Exception("A PHPExcel_Cell_IValueBinder is required for PHPExcel to function correctly.");
  814. }
  815. self::$_valueBinder = $binder;
  816. }
  817. /**
  818. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  819. */
  820. public function __clone() {
  821. $vars = get_object_vars($this);
  822. foreach ($vars as $key => $value) {
  823. if ((is_object($value)) && ($key != '_parent')) {
  824. $this->$key = clone $value;
  825. } else {
  826. $this->$key = $value;
  827. }
  828. }
  829. }
  830. /**
  831. * Get index to cellXf
  832. *
  833. * @return int
  834. */
  835. public function getXfIndex()
  836. {
  837. return $this->_xfIndex;
  838. }
  839. /**
  840. * Set index to cellXf
  841. *
  842. * @param int $pValue
  843. * @return PHPExcel_Cell
  844. */
  845. public function setXfIndex($pValue = 0)
  846. {
  847. $this->_xfIndex = $pValue;
  848. return $this->notifyCacheController();
  849. }
  850. /**
  851. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  852. */
  853. public function setFormulaAttributes($pAttributes)
  854. {
  855. $this->_formulaAttributes = $pAttributes;
  856. return $this;
  857. }
  858. /**
  859. * @deprecated Since version 1.7.8 for planned changes to cell for array formula handling
  860. */
  861. public function getFormulaAttributes()
  862. {
  863. return $this->_formulaAttributes;
  864. }
  865. /**
  866. * Convert to string
  867. *
  868. * @return string
  869. */
  870. public function __toString()
  871. {
  872. return (string) $this->getValue();
  873. }
  874. }