/PHPExcel.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 823 lines · 387 code · 89 blank · 347 comment · 57 complexity · 82a1dc7b788bae6abc9e55534c2b3ca3 MD5 · raw file

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