PHPExcel.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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
  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. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
  30. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  31. }
  32. /**
  33. * PHPExcel
  34. *
  35. * @category PHPExcel
  36. * @package PHPExcel
  37. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  38. */
  39. class PHPExcel
  40. {
  41. /**
  42. * Unique ID
  43. *
  44. * @var string
  45. */
  46. private $_uniqueID;
  47. /**
  48. * Document properties
  49. *
  50. * @var PHPExcel_DocumentProperties
  51. */
  52. private $_properties;
  53. /**
  54. * Document security
  55. *
  56. * @var PHPExcel_DocumentSecurity
  57. */
  58. private $_security;
  59. /**
  60. * Collection of Worksheet objects
  61. *
  62. * @var PHPExcel_Worksheet[]
  63. */
  64. private $_workSheetCollection = array();
  65. /**
  66. * Calculation Engine
  67. *
  68. * @var PHPExcel_Calculation
  69. */
  70. private $_calculationEngine = NULL;
  71. /**
  72. * Active sheet index
  73. *
  74. * @var int
  75. */
  76. private $_activeSheetIndex = 0;
  77. /**
  78. * Named ranges
  79. *
  80. * @var PHPExcel_NamedRange[]
  81. */
  82. private $_namedRanges = array();
  83. /**
  84. * CellXf supervisor
  85. *
  86. * @var PHPExcel_Style
  87. */
  88. private $_cellXfSupervisor;
  89. /**
  90. * CellXf collection
  91. *
  92. * @var PHPExcel_Style[]
  93. */
  94. private $_cellXfCollection = array();
  95. /**
  96. * CellStyleXf collection
  97. *
  98. * @var PHPExcel_Style[]
  99. */
  100. private $_cellStyleXfCollection = array();
  101. /**
  102. * Create a new PHPExcel with one Worksheet
  103. */
  104. public function __construct()
  105. {
  106. $this->_uniqueID = uniqid();
  107. $this->_calculationEngine = PHPExcel_Calculation::getInstance($this);
  108. // Initialise worksheet collection and add one worksheet
  109. $this->_workSheetCollection = array();
  110. $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
  111. $this->_activeSheetIndex = 0;
  112. // Create document properties
  113. $this->_properties = new PHPExcel_DocumentProperties();
  114. // Create document security
  115. $this->_security = new PHPExcel_DocumentSecurity();
  116. // Set named ranges
  117. $this->_namedRanges = array();
  118. // Create the cellXf supervisor
  119. $this->_cellXfSupervisor = new PHPExcel_Style(true);
  120. $this->_cellXfSupervisor->bindParent($this);
  121. // Create the default style
  122. $this->addCellXf(new PHPExcel_Style);
  123. $this->addCellStyleXf(new PHPExcel_Style);
  124. }
  125. /**
  126. * Code to execute when this worksheet is unset()
  127. *
  128. */
  129. public function __destruct() {
  130. PHPExcel_Calculation::unsetInstance($this);
  131. $this->disconnectWorksheets();
  132. } // function __destruct()
  133. /**
  134. * Disconnect all worksheets from this PHPExcel workbook object,
  135. * typically so that the PHPExcel object can be unset
  136. *
  137. */
  138. public function disconnectWorksheets()
  139. {
  140. $worksheet = NULL;
  141. foreach($this->_workSheetCollection as $k => &$worksheet) {
  142. $worksheet->disconnectCells();
  143. $this->_workSheetCollection[$k] = null;
  144. }
  145. unset($worksheet);
  146. $this->_workSheetCollection = array();
  147. }
  148. /**
  149. * Return the calculation engine for this worksheet
  150. *
  151. * @return PHPExcel_Calculation
  152. */
  153. public function getCalculationEngine()
  154. {
  155. return $this->_calculationEngine;
  156. } // function getCellCacheController()
  157. /**
  158. * Get properties
  159. *
  160. * @return PHPExcel_DocumentProperties
  161. */
  162. public function getProperties()
  163. {
  164. return $this->_properties;
  165. }
  166. /**
  167. * Set properties
  168. *
  169. * @param PHPExcel_DocumentProperties $pValue
  170. */
  171. public function setProperties(PHPExcel_DocumentProperties $pValue)
  172. {
  173. $this->_properties = $pValue;
  174. }
  175. /**
  176. * Get security
  177. *
  178. * @return PHPExcel_DocumentSecurity
  179. */
  180. public function getSecurity()
  181. {
  182. return $this->_security;
  183. }
  184. /**
  185. * Set security
  186. *
  187. * @param PHPExcel_DocumentSecurity $pValue
  188. */
  189. public function setSecurity(PHPExcel_DocumentSecurity $pValue)
  190. {
  191. $this->_security = $pValue;
  192. }
  193. /**
  194. * Get active sheet
  195. *
  196. * @return PHPExcel_Worksheet
  197. */
  198. public function getActiveSheet()
  199. {
  200. return $this->_workSheetCollection[$this->_activeSheetIndex];
  201. }
  202. /**
  203. * Create sheet and add it to this workbook
  204. *
  205. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  206. * @return PHPExcel_Worksheet
  207. * @throws PHPExcel_Exception
  208. */
  209. public function createSheet($iSheetIndex = NULL)
  210. {
  211. $newSheet = new PHPExcel_Worksheet($this);
  212. $this->addSheet($newSheet, $iSheetIndex);
  213. return $newSheet;
  214. }
  215. /**
  216. * Check if a sheet with a specified name already exists
  217. *
  218. * @param string $pSheetName Name of the worksheet to check
  219. * @return boolean
  220. */
  221. public function sheetNameExists($pSheetName)
  222. {
  223. return ($this->getSheetByName($pSheetName) !== NULL);
  224. }
  225. /**
  226. * Add sheet
  227. *
  228. * @param PHPExcel_Worksheet $pSheet
  229. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  230. * @return PHPExcel_Worksheet
  231. * @throws PHPExcel_Exception
  232. */
  233. public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = NULL)
  234. {
  235. if ($this->sheetNameExists($pSheet->getTitle())) {
  236. throw new PHPExcel_Exception(
  237. "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
  238. );
  239. }
  240. if($iSheetIndex === NULL) {
  241. if ($this->_activeSheetIndex < 0) {
  242. $this->_activeSheetIndex = 0;
  243. }
  244. $this->_workSheetCollection[] = $pSheet;
  245. } else {
  246. // Insert the sheet at the requested index
  247. array_splice(
  248. $this->_workSheetCollection,
  249. $iSheetIndex,
  250. 0,
  251. array($pSheet)
  252. );
  253. // Adjust active sheet index if necessary
  254. if ($this->_activeSheetIndex >= $iSheetIndex) {
  255. ++$this->_activeSheetIndex;
  256. }
  257. }
  258. return $pSheet;
  259. }
  260. /**
  261. * Remove sheet by index
  262. *
  263. * @param int $pIndex Active sheet index
  264. * @throws PHPExcel_Exception
  265. */
  266. public function removeSheetByIndex($pIndex = 0)
  267. {
  268. $numSheets = count($this->_workSheetCollection);
  269. if ($pIndex > $numSheets - 1) {
  270. throw new PHPExcel_Exception(
  271. "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  272. );
  273. } else {
  274. array_splice($this->_workSheetCollection, $pIndex, 1);
  275. }
  276. // Adjust active sheet index if necessary
  277. if (($this->_activeSheetIndex >= $pIndex) &&
  278. ($pIndex > count($this->_workSheetCollection) - 1)) {
  279. --$this->_activeSheetIndex;
  280. }
  281. }
  282. /**
  283. * Get sheet by index
  284. *
  285. * @param int $pIndex Sheet index
  286. * @return PHPExcel_Worksheet
  287. * @throws PHPExcel_Exception
  288. */
  289. public function getSheet($pIndex = 0)
  290. {
  291. $numSheets = count($this->_workSheetCollection);
  292. if ($pIndex > $numSheets - 1) {
  293. throw new PHPExcel_Exception(
  294. "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
  295. );
  296. } else {
  297. return $this->_workSheetCollection[$pIndex];
  298. }
  299. }
  300. /**
  301. * Get all sheets
  302. *
  303. * @return PHPExcel_Worksheet[]
  304. */
  305. public function getAllSheets()
  306. {
  307. return $this->_workSheetCollection;
  308. }
  309. /**
  310. * Get sheet by name
  311. *
  312. * @param string $pName Sheet name
  313. * @return PHPExcel_Worksheet
  314. */
  315. public function getSheetByName($pName = '')
  316. {
  317. $worksheetCount = count($this->_workSheetCollection);
  318. for ($i = 0; $i < $worksheetCount; ++$i) {
  319. if ($this->_workSheetCollection[$i]->getTitle() === $pName) {
  320. return $this->_workSheetCollection[$i];
  321. }
  322. }
  323. return NULL;
  324. }
  325. /**
  326. * Get index for sheet
  327. *
  328. * @param PHPExcel_Worksheet $pSheet
  329. * @return Sheet index
  330. * @throws PHPExcel_Exception
  331. */
  332. public function getIndex(PHPExcel_Worksheet $pSheet)
  333. {
  334. foreach ($this->_workSheetCollection as $key => $value) {
  335. if ($value->getHashCode() == $pSheet->getHashCode()) {
  336. return $key;
  337. }
  338. }
  339. throw new PHPExcel_Exception("Sheet does not exist.");
  340. }
  341. /**
  342. * Set index for sheet by sheet name.
  343. *
  344. * @param string $sheetName Sheet name to modify index for
  345. * @param int $newIndex New index for the sheet
  346. * @return New sheet index
  347. * @throws PHPExcel_Exception
  348. */
  349. public function setIndexByName($sheetName, $newIndex)
  350. {
  351. $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
  352. $pSheet = array_splice(
  353. $this->_workSheetCollection,
  354. $oldIndex,
  355. 1
  356. );
  357. array_splice(
  358. $this->_workSheetCollection,
  359. $newIndex,
  360. 0,
  361. $pSheet
  362. );
  363. return $newIndex;
  364. }
  365. /**
  366. * Get sheet count
  367. *
  368. * @return int
  369. */
  370. public function getSheetCount()
  371. {
  372. return count($this->_workSheetCollection);
  373. }
  374. /**
  375. * Get active sheet index
  376. *
  377. * @return int Active sheet index
  378. */
  379. public function getActiveSheetIndex()
  380. {
  381. return $this->_activeSheetIndex;
  382. }
  383. /**
  384. * Set active sheet index
  385. *
  386. * @param int $pIndex Active sheet index
  387. * @throws PHPExcel_Exception
  388. * @return PHPExcel_Worksheet
  389. */
  390. public function setActiveSheetIndex($pIndex = 0)
  391. {
  392. $numSheets = count($this->_workSheetCollection);
  393. if ($pIndex > $numSheets - 1) {
  394. throw new PHPExcel_Exception(
  395. "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  396. );
  397. } else {
  398. $this->_activeSheetIndex = $pIndex;
  399. }
  400. return $this->getActiveSheet();
  401. }
  402. /**
  403. * Set active sheet index by name
  404. *
  405. * @param string $pValue Sheet title
  406. * @return PHPExcel_Worksheet
  407. * @throws PHPExcel_Exception
  408. */
  409. public function setActiveSheetIndexByName($pValue = '')
  410. {
  411. if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
  412. $this->setActiveSheetIndex($this->getIndex($worksheet));
  413. return $worksheet;
  414. }
  415. throw new PHPExcel_Exception('Workbook does not contain sheet:' . $pValue);
  416. }
  417. /**
  418. * Get sheet names
  419. *
  420. * @return string[]
  421. */
  422. public function getSheetNames()
  423. {
  424. $returnValue = array();
  425. $worksheetCount = $this->getSheetCount();
  426. for ($i = 0; $i < $worksheetCount; ++$i) {
  427. $returnValue[] = $this->getSheet($i)->getTitle();
  428. }
  429. return $returnValue;
  430. }
  431. /**
  432. * Add external sheet
  433. *
  434. * @param PHPExcel_Worksheet $pSheet External sheet to add
  435. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  436. * @throws PHPExcel_Exception
  437. * @return PHPExcel_Worksheet
  438. */
  439. public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) {
  440. if ($this->sheetNameExists($pSheet->getTitle())) {
  441. throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  442. }
  443. // count how many cellXfs there are in this workbook currently, we will need this below
  444. $countCellXfs = count($this->_cellXfCollection);
  445. // copy all the shared cellXfs from the external workbook and append them to the current
  446. foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
  447. $this->addCellXf(clone $cellXf);
  448. }
  449. // move sheet to this workbook
  450. $pSheet->rebindParent($this);
  451. // update the cellXfs
  452. foreach ($pSheet->getCellCollection(false) as $cellID) {
  453. $cell = $pSheet->getCell($cellID);
  454. $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
  455. }
  456. return $this->addSheet($pSheet, $iSheetIndex);
  457. }
  458. /**
  459. * Get named ranges
  460. *
  461. * @return PHPExcel_NamedRange[]
  462. */
  463. public function getNamedRanges() {
  464. return $this->_namedRanges;
  465. }
  466. /**
  467. * Add named range
  468. *
  469. * @param PHPExcel_NamedRange $namedRange
  470. * @return PHPExcel
  471. */
  472. public function addNamedRange(PHPExcel_NamedRange $namedRange) {
  473. if ($namedRange->getScope() == null) {
  474. // global scope
  475. $this->_namedRanges[$namedRange->getName()] = $namedRange;
  476. } else {
  477. // local scope
  478. $this->_namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
  479. }
  480. return true;
  481. }
  482. /**
  483. * Get named range
  484. *
  485. * @param string $namedRange
  486. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  487. * @return PHPExcel_NamedRange|null
  488. */
  489. public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
  490. $returnValue = null;
  491. if ($namedRange != '' && ($namedRange !== NULL)) {
  492. // first look for global defined name
  493. if (isset($this->_namedRanges[$namedRange])) {
  494. $returnValue = $this->_namedRanges[$namedRange];
  495. }
  496. // then look for local defined name (has priority over global defined name if both names exist)
  497. if (($pSheet !== NULL) && isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  498. $returnValue = $this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange];
  499. }
  500. }
  501. return $returnValue;
  502. }
  503. /**
  504. * Remove named range
  505. *
  506. * @param string $namedRange
  507. * @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope.
  508. * @return PHPExcel
  509. */
  510. public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
  511. if ($pSheet === NULL) {
  512. if (isset($this->_namedRanges[$namedRange])) {
  513. unset($this->_namedRanges[$namedRange]);
  514. }
  515. } else {
  516. if (isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  517. unset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
  518. }
  519. }
  520. return $this;
  521. }
  522. /**
  523. * Get worksheet iterator
  524. *
  525. * @return PHPExcel_WorksheetIterator
  526. */
  527. public function getWorksheetIterator() {
  528. return new PHPExcel_WorksheetIterator($this);
  529. }
  530. /**
  531. * Copy workbook (!= clone!)
  532. *
  533. * @return PHPExcel
  534. */
  535. public function copy() {
  536. $copied = clone $this;
  537. $worksheetCount = count($this->_workSheetCollection);
  538. for ($i = 0; $i < $worksheetCount; ++$i) {
  539. $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy();
  540. $this->_workSheetCollection[$i]->rebindParent($this);
  541. }
  542. return $copied;
  543. }
  544. /**
  545. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  546. */
  547. public function __clone() {
  548. foreach($this as $key => $val) {
  549. if (is_object($val) || (is_array($val))) {
  550. $this->{$key} = unserialize(serialize($val));
  551. }
  552. }
  553. }
  554. /**
  555. * Get the workbook collection of cellXfs
  556. *
  557. * @return PHPExcel_Style[]
  558. */
  559. public function getCellXfCollection()
  560. {
  561. return $this->_cellXfCollection;
  562. }
  563. /**
  564. * Get cellXf by index
  565. *
  566. * @param int $pIndex
  567. * @return PHPExcel_Style
  568. */
  569. public function getCellXfByIndex($pIndex = 0)
  570. {
  571. return $this->_cellXfCollection[$pIndex];
  572. }
  573. /**
  574. * Get cellXf by hash code
  575. *
  576. * @param string $pValue
  577. * @return PHPExcel_Style|false
  578. */
  579. public function getCellXfByHashCode($pValue = '')
  580. {
  581. foreach ($this->_cellXfCollection as $cellXf) {
  582. if ($cellXf->getHashCode() == $pValue) {
  583. return $cellXf;
  584. }
  585. }
  586. return false;
  587. }
  588. /**
  589. * Check if style exists in style collection
  590. *
  591. * @param PHPExcel_Style $pCellStyle
  592. * @return boolean
  593. */
  594. public function cellXfExists($pCellStyle = null)
  595. {
  596. return in_array($pCellStyle, $this->_cellXfCollection, true);
  597. }
  598. /**
  599. * Get default style
  600. *
  601. * @return PHPExcel_Style
  602. * @throws PHPExcel_Exception
  603. */
  604. public function getDefaultStyle()
  605. {
  606. if (isset($this->_cellXfCollection[0])) {
  607. return $this->_cellXfCollection[0];
  608. }
  609. throw new PHPExcel_Exception('No default style found for this workbook');
  610. }
  611. /**
  612. * Add a cellXf to the workbook
  613. *
  614. * @param PHPExcel_Style $style
  615. */
  616. public function addCellXf(PHPExcel_Style $style)
  617. {
  618. $this->_cellXfCollection[] = $style;
  619. $style->setIndex(count($this->_cellXfCollection) - 1);
  620. }
  621. /**
  622. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  623. *
  624. * @param int $pIndex Index to cellXf
  625. * @throws PHPExcel_Exception
  626. */
  627. public function removeCellXfByIndex($pIndex = 0)
  628. {
  629. if ($pIndex > count($this->_cellXfCollection) - 1) {
  630. throw new PHPExcel_Exception("CellXf index is out of bounds.");
  631. } else {
  632. // first remove the cellXf
  633. array_splice($this->_cellXfCollection, $pIndex, 1);
  634. // then update cellXf indexes for cells
  635. foreach ($this->_workSheetCollection as $worksheet) {
  636. foreach ($worksheet->getCellCollection(false) as $cellID) {
  637. $cell = $worksheet->getCell($cellID);
  638. $xfIndex = $cell->getXfIndex();
  639. if ($xfIndex > $pIndex ) {
  640. // decrease xf index by 1
  641. $cell->setXfIndex($xfIndex - 1);
  642. } else if ($xfIndex == $pIndex) {
  643. // set to default xf index 0
  644. $cell->setXfIndex(0);
  645. }
  646. }
  647. }
  648. }
  649. }
  650. /**
  651. * Get the cellXf supervisor
  652. *
  653. * @return PHPExcel_Style
  654. */
  655. public function getCellXfSupervisor()
  656. {
  657. return $this->_cellXfSupervisor;
  658. }
  659. /**
  660. * Get the workbook collection of cellStyleXfs
  661. *
  662. * @return PHPExcel_Style[]
  663. */
  664. public function getCellStyleXfCollection()
  665. {
  666. return $this->_cellStyleXfCollection;
  667. }
  668. /**
  669. * Get cellStyleXf by index
  670. *
  671. * @param int $pIndex
  672. * @return PHPExcel_Style
  673. */
  674. public function getCellStyleXfByIndex($pIndex = 0)
  675. {
  676. return $this->_cellStyleXfCollection[$pIndex];
  677. }
  678. /**
  679. * Get cellStyleXf by hash code
  680. *
  681. * @param string $pValue
  682. * @return PHPExcel_Style|false
  683. */
  684. public function getCellStyleXfByHashCode($pValue = '')
  685. {
  686. foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
  687. if ($cellStyleXf->getHashCode() == $pValue) {
  688. return $cellStyleXf;
  689. }
  690. }
  691. return false;
  692. }
  693. /**
  694. * Add a cellStyleXf to the workbook
  695. *
  696. * @param PHPExcel_Style $pStyle
  697. */
  698. public function addCellStyleXf(PHPExcel_Style $pStyle)
  699. {
  700. $this->_cellStyleXfCollection[] = $pStyle;
  701. $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1);
  702. }
  703. /**
  704. * Remove cellStyleXf by index
  705. *
  706. * @param int $pIndex
  707. * @throws PHPExcel_Exception
  708. */
  709. public function removeCellStyleXfByIndex($pIndex = 0)
  710. {
  711. if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
  712. throw new PHPExcel_Exception("CellStyleXf index is out of bounds.");
  713. } else {
  714. array_splice($this->_cellStyleXfCollection, $pIndex, 1);
  715. }
  716. }
  717. /**
  718. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  719. * and columns in the workbook
  720. */
  721. public function garbageCollect()
  722. {
  723. // how many references are there to each cellXf ?
  724. $countReferencesCellXf = array();
  725. foreach ($this->_cellXfCollection as $index => $cellXf) {
  726. $countReferencesCellXf[$index] = 0;
  727. }
  728. foreach ($this->getWorksheetIterator() as $sheet) {
  729. // from cells
  730. foreach ($sheet->getCellCollection(false) as $cellID) {
  731. $cell = $sheet->getCell($cellID);
  732. ++$countReferencesCellXf[$cell->getXfIndex()];
  733. }
  734. // from row dimensions
  735. foreach ($sheet->getRowDimensions() as $rowDimension) {
  736. if ($rowDimension->getXfIndex() !== null) {
  737. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  738. }
  739. }
  740. // from column dimensions
  741. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  742. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  743. }
  744. }
  745. // remove cellXfs without references and create mapping so we can update xfIndex
  746. // for all cells and columns
  747. $countNeededCellXfs = 0;
  748. foreach ($this->_cellXfCollection as $index => $cellXf) {
  749. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  750. ++$countNeededCellXfs;
  751. } else {
  752. unset($this->_cellXfCollection[$index]);
  753. }
  754. $map[$index] = $countNeededCellXfs - 1;
  755. }
  756. $this->_cellXfCollection = array_values($this->_cellXfCollection);
  757. // update the index for all cellXfs
  758. foreach ($this->_cellXfCollection as $i => $cellXf) {
  759. $cellXf->setIndex($i);
  760. }
  761. // make sure there is always at least one cellXf (there should be)
  762. if (empty($this->_cellXfCollection)) {
  763. $this->_cellXfCollection[] = new PHPExcel_Style();
  764. }
  765. // update the xfIndex for all cells, row dimensions, column dimensions
  766. foreach ($this->getWorksheetIterator() as $sheet) {
  767. // for all cells
  768. foreach ($sheet->getCellCollection(false) as $cellID) {
  769. $cell = $sheet->getCell($cellID);
  770. $cell->setXfIndex( $map[$cell->getXfIndex()] );
  771. }
  772. // for all row dimensions
  773. foreach ($sheet->getRowDimensions() as $rowDimension) {
  774. if ($rowDimension->getXfIndex() !== null) {
  775. $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] );
  776. }
  777. }
  778. // for all column dimensions
  779. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  780. $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] );
  781. }
  782. // also do garbage collection for all the sheets
  783. $sheet->garbageCollect();
  784. }
  785. }
  786. /**
  787. * Return the unique ID value assigned to this spreadsheet workbook
  788. *
  789. * @return string
  790. */
  791. public function getID() {
  792. return $this->_uniqueID;
  793. }
  794. }