MDL-55707 grades: Stop infinite loop when regrading.
[moodle.git] / lib / phpexcel / PHPExcel.php
blobc3ec0519788b85d364e202b0c2560be66c2436e8
1 <?php
3 /** PHPExcel root directory */
4 if (!defined('PHPEXCEL_ROOT')) {
5 define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
6 require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
9 /**
10 * PHPExcel
12 * Copyright (c) 2006 - 2015 PHPExcel
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * @category PHPExcel
29 * @package PHPExcel
30 * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
31 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
32 * @version ##VERSION##, ##DATE##
34 class PHPExcel
36 /**
37 * Unique ID
39 * @var string
41 private $uniqueID;
43 /**
44 * Document properties
46 * @var PHPExcel_DocumentProperties
48 private $properties;
50 /**
51 * Document security
53 * @var PHPExcel_DocumentSecurity
55 private $security;
57 /**
58 * Collection of Worksheet objects
60 * @var PHPExcel_Worksheet[]
62 private $workSheetCollection = array();
64 /**
65 * Calculation Engine
67 * @var PHPExcel_Calculation
69 private $calculationEngine;
71 /**
72 * Active sheet index
74 * @var integer
76 private $activeSheetIndex = 0;
78 /**
79 * Named ranges
81 * @var PHPExcel_NamedRange[]
83 private $namedRanges = array();
85 /**
86 * CellXf supervisor
88 * @var PHPExcel_Style
90 private $cellXfSupervisor;
92 /**
93 * CellXf collection
95 * @var PHPExcel_Style[]
97 private $cellXfCollection = array();
99 /**
100 * CellStyleXf collection
102 * @var PHPExcel_Style[]
104 private $cellStyleXfCollection = array();
107 * hasMacros : this workbook have macros ?
109 * @var bool
111 private $hasMacros = false;
114 * macrosCode : all macros code (the vbaProject.bin file, this include form, code, etc.), null if no macro
116 * @var binary
118 private $macrosCode;
120 * macrosCertificate : if macros are signed, contains vbaProjectSignature.bin file, null if not signed
122 * @var binary
124 private $macrosCertificate;
127 * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI
129 * @var null|string
131 private $ribbonXMLData;
134 * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
135 * ignored if $ribbonXMLData is null
137 * @var null|array
139 private $ribbonBinObjects;
142 * The workbook has macros ?
144 * @return true if workbook has macros, false if not
146 public function hasMacros()
148 return $this->hasMacros;
152 * Define if a workbook has macros
154 * @param boolean $hasMacros true|false
156 public function setHasMacros($hasMacros = false)
158 $this->hasMacros = (bool) $hasMacros;
162 * Set the macros code
164 * @param string $MacrosCode string|null
166 public function setMacrosCode($MacrosCode = null)
168 $this->macrosCode=$MacrosCode;
169 $this->setHasMacros(!is_null($MacrosCode));
173 * Return the macros code
175 * @return string|null
177 public function getMacrosCode()
179 return $this->macrosCode;
183 * Set the macros certificate
185 * @param string|null $Certificate
187 public function setMacrosCertificate($Certificate = null)
189 $this->macrosCertificate=$Certificate;
193 * Is the project signed ?
195 * @return boolean true|false
197 public function hasMacrosCertificate()
199 return !is_null($this->macrosCertificate);
203 * Return the macros certificate
205 * @return string|null
207 public function getMacrosCertificate()
209 return $this->macrosCertificate;
213 * Remove all macros, certificate from spreadsheet
216 public function discardMacros()
218 $this->hasMacros=false;
219 $this->macrosCode=null;
220 $this->macrosCertificate=null;
224 * set ribbon XML data
227 public function setRibbonXMLData($Target = null, $XMLData = null)
229 if (!is_null($Target) && !is_null($XMLData)) {
230 $this->ribbonXMLData = array('target' => $Target, 'data' => $XMLData);
231 } else {
232 $this->ribbonXMLData = null;
237 * retrieve ribbon XML Data
239 * return string|null|array
241 public function getRibbonXMLData($What = 'all') //we need some constants here...
243 $ReturnData = null;
244 $What = strtolower($What);
245 switch ($What){
246 case 'all':
247 $ReturnData = $this->ribbonXMLData;
248 break;
249 case 'target':
250 case 'data':
251 if (is_array($this->ribbonXMLData) && array_key_exists($What, $this->ribbonXMLData)) {
252 $ReturnData = $this->ribbonXMLData[$What];
254 break;
257 return $ReturnData;
261 * store binaries ribbon objects (pictures)
264 public function setRibbonBinObjects($BinObjectsNames = null, $BinObjectsData = null)
266 if (!is_null($BinObjectsNames) && !is_null($BinObjectsData)) {
267 $this->ribbonBinObjects = array('names' => $BinObjectsNames, 'data' => $BinObjectsData);
268 } else {
269 $this->ribbonBinObjects = null;
273 * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function)
276 private function getExtensionOnly($ThePath)
278 return pathinfo($ThePath, PATHINFO_EXTENSION);
282 * retrieve Binaries Ribbon Objects
285 public function getRibbonBinObjects($What = 'all')
287 $ReturnData = null;
288 $What = strtolower($What);
289 switch($What) {
290 case 'all':
291 return $this->ribbonBinObjects;
292 break;
293 case 'names':
294 case 'data':
295 if (is_array($this->ribbonBinObjects) && array_key_exists($What, $this->ribbonBinObjects)) {
296 $ReturnData=$this->ribbonBinObjects[$What];
298 break;
299 case 'types':
300 if (is_array($this->ribbonBinObjects) &&
301 array_key_exists('data', $this->ribbonBinObjects) && is_array($this->ribbonBinObjects['data'])) {
302 $tmpTypes=array_keys($this->ribbonBinObjects['data']);
303 $ReturnData = array_unique(array_map(array($this, 'getExtensionOnly'), $tmpTypes));
304 } else {
305 $ReturnData=array(); // the caller want an array... not null if empty
307 break;
309 return $ReturnData;
313 * This workbook have a custom UI ?
315 * @return true|false
317 public function hasRibbon()
319 return !is_null($this->ribbonXMLData);
323 * This workbook have additionnal object for the ribbon ?
325 * @return true|false
327 public function hasRibbonBinObjects()
329 return !is_null($this->ribbonBinObjects);
333 * Check if a sheet with a specified code name already exists
335 * @param string $pSheetCodeName Name of the worksheet to check
336 * @return boolean
338 public function sheetCodeNameExists($pSheetCodeName)
340 return ($this->getSheetByCodeName($pSheetCodeName) !== null);
344 * Get sheet by code name. Warning : sheet don't have always a code name !
346 * @param string $pName Sheet name
347 * @return PHPExcel_Worksheet
349 public function getSheetByCodeName($pName = '')
351 $worksheetCount = count($this->workSheetCollection);
352 for ($i = 0; $i < $worksheetCount; ++$i) {
353 if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
354 return $this->workSheetCollection[$i];
358 return null;
362 * Create a new PHPExcel with one Worksheet
364 public function __construct()
366 $this->uniqueID = uniqid();
367 $this->calculationEngine = PHPExcel_Calculation::getInstance($this);
369 // Initialise worksheet collection and add one worksheet
370 $this->workSheetCollection = array();
371 $this->workSheetCollection[] = new PHPExcel_Worksheet($this);
372 $this->activeSheetIndex = 0;
374 // Create document properties
375 $this->properties = new PHPExcel_DocumentProperties();
377 // Create document security
378 $this->security = new PHPExcel_DocumentSecurity();
380 // Set named ranges
381 $this->namedRanges = array();
383 // Create the cellXf supervisor
384 $this->cellXfSupervisor = new PHPExcel_Style(true);
385 $this->cellXfSupervisor->bindParent($this);
387 // Create the default style
388 $this->addCellXf(new PHPExcel_Style);
389 $this->addCellStyleXf(new PHPExcel_Style);
393 * Code to execute when this worksheet is unset()
396 public function __destruct()
398 PHPExcel_Calculation::unsetInstance($this);
399 $this->disconnectWorksheets();
403 * Disconnect all worksheets from this PHPExcel workbook object,
404 * typically so that the PHPExcel object can be unset
407 public function disconnectWorksheets()
409 $worksheet = null;
410 foreach ($this->workSheetCollection as $k => &$worksheet) {
411 $worksheet->disconnectCells();
412 $this->workSheetCollection[$k] = null;
414 unset($worksheet);
415 $this->workSheetCollection = array();
419 * Return the calculation engine for this worksheet
421 * @return PHPExcel_Calculation
423 public function getCalculationEngine()
425 return $this->calculationEngine;
426 } // function getCellCacheController()
429 * Get properties
431 * @return PHPExcel_DocumentProperties
433 public function getProperties()
435 return $this->properties;
439 * Set properties
441 * @param PHPExcel_DocumentProperties $pValue
443 public function setProperties(PHPExcel_DocumentProperties $pValue)
445 $this->properties = $pValue;
449 * Get security
451 * @return PHPExcel_DocumentSecurity
453 public function getSecurity()
455 return $this->security;
459 * Set security
461 * @param PHPExcel_DocumentSecurity $pValue
463 public function setSecurity(PHPExcel_DocumentSecurity $pValue)
465 $this->security = $pValue;
469 * Get active sheet
471 * @return PHPExcel_Worksheet
473 * @throws PHPExcel_Exception
475 public function getActiveSheet()
477 return $this->getSheet($this->activeSheetIndex);
481 * Create sheet and add it to this workbook
483 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
484 * @return PHPExcel_Worksheet
485 * @throws PHPExcel_Exception
487 public function createSheet($iSheetIndex = null)
489 $newSheet = new PHPExcel_Worksheet($this);
490 $this->addSheet($newSheet, $iSheetIndex);
491 return $newSheet;
495 * Check if a sheet with a specified name already exists
497 * @param string $pSheetName Name of the worksheet to check
498 * @return boolean
500 public function sheetNameExists($pSheetName)
502 return ($this->getSheetByName($pSheetName) !== null);
506 * Add sheet
508 * @param PHPExcel_Worksheet $pSheet
509 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
510 * @return PHPExcel_Worksheet
511 * @throws PHPExcel_Exception
513 public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
515 if ($this->sheetNameExists($pSheet->getTitle())) {
516 throw new PHPExcel_Exception(
517 "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
521 if ($iSheetIndex === null) {
522 if ($this->activeSheetIndex < 0) {
523 $this->activeSheetIndex = 0;
525 $this->workSheetCollection[] = $pSheet;
526 } else {
527 // Insert the sheet at the requested index
528 array_splice(
529 $this->workSheetCollection,
530 $iSheetIndex,
532 array($pSheet)
535 // Adjust active sheet index if necessary
536 if ($this->activeSheetIndex >= $iSheetIndex) {
537 ++$this->activeSheetIndex;
541 if ($pSheet->getParent() === null) {
542 $pSheet->rebindParent($this);
545 return $pSheet;
549 * Remove sheet by index
551 * @param int $pIndex Active sheet index
552 * @throws PHPExcel_Exception
554 public function removeSheetByIndex($pIndex = 0)
557 $numSheets = count($this->workSheetCollection);
558 if ($pIndex > $numSheets - 1) {
559 throw new PHPExcel_Exception(
560 "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
562 } else {
563 array_splice($this->workSheetCollection, $pIndex, 1);
565 // Adjust active sheet index if necessary
566 if (($this->activeSheetIndex >= $pIndex) &&
567 ($pIndex > count($this->workSheetCollection) - 1)) {
568 --$this->activeSheetIndex;
574 * Get sheet by index
576 * @param int $pIndex Sheet index
577 * @return PHPExcel_Worksheet
578 * @throws PHPExcel_Exception
580 public function getSheet($pIndex = 0)
582 if (!isset($this->workSheetCollection[$pIndex])) {
583 $numSheets = $this->getSheetCount();
584 throw new PHPExcel_Exception(
585 "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
589 return $this->workSheetCollection[$pIndex];
593 * Get all sheets
595 * @return PHPExcel_Worksheet[]
597 public function getAllSheets()
599 return $this->workSheetCollection;
603 * Get sheet by name
605 * @param string $pName Sheet name
606 * @return PHPExcel_Worksheet
608 public function getSheetByName($pName = '')
610 $worksheetCount = count($this->workSheetCollection);
611 for ($i = 0; $i < $worksheetCount; ++$i) {
612 if ($this->workSheetCollection[$i]->getTitle() === $pName) {
613 return $this->workSheetCollection[$i];
617 return null;
621 * Get index for sheet
623 * @param PHPExcel_Worksheet $pSheet
624 * @return Sheet index
625 * @throws PHPExcel_Exception
627 public function getIndex(PHPExcel_Worksheet $pSheet)
629 foreach ($this->workSheetCollection as $key => $value) {
630 if ($value->getHashCode() == $pSheet->getHashCode()) {
631 return $key;
635 throw new PHPExcel_Exception("Sheet does not exist.");
639 * Set index for sheet by sheet name.
641 * @param string $sheetName Sheet name to modify index for
642 * @param int $newIndex New index for the sheet
643 * @return New sheet index
644 * @throws PHPExcel_Exception
646 public function setIndexByName($sheetName, $newIndex)
648 $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
649 $pSheet = array_splice(
650 $this->workSheetCollection,
651 $oldIndex,
654 array_splice(
655 $this->workSheetCollection,
656 $newIndex,
658 $pSheet
660 return $newIndex;
664 * Get sheet count
666 * @return int
668 public function getSheetCount()
670 return count($this->workSheetCollection);
674 * Get active sheet index
676 * @return int Active sheet index
678 public function getActiveSheetIndex()
680 return $this->activeSheetIndex;
684 * Set active sheet index
686 * @param int $pIndex Active sheet index
687 * @throws PHPExcel_Exception
688 * @return PHPExcel_Worksheet
690 public function setActiveSheetIndex($pIndex = 0)
692 $numSheets = count($this->workSheetCollection);
694 if ($pIndex > $numSheets - 1) {
695 throw new PHPExcel_Exception(
696 "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
698 } else {
699 $this->activeSheetIndex = $pIndex;
701 return $this->getActiveSheet();
705 * Set active sheet index by name
707 * @param string $pValue Sheet title
708 * @return PHPExcel_Worksheet
709 * @throws PHPExcel_Exception
711 public function setActiveSheetIndexByName($pValue = '')
713 if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
714 $this->setActiveSheetIndex($this->getIndex($worksheet));
715 return $worksheet;
718 throw new PHPExcel_Exception('Workbook does not contain sheet:' . $pValue);
722 * Get sheet names
724 * @return string[]
726 public function getSheetNames()
728 $returnValue = array();
729 $worksheetCount = $this->getSheetCount();
730 for ($i = 0; $i < $worksheetCount; ++$i) {
731 $returnValue[] = $this->getSheet($i)->getTitle();
734 return $returnValue;
738 * Add external sheet
740 * @param PHPExcel_Worksheet $pSheet External sheet to add
741 * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
742 * @throws PHPExcel_Exception
743 * @return PHPExcel_Worksheet
745 public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
747 if ($this->sheetNameExists($pSheet->getTitle())) {
748 throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
751 // count how many cellXfs there are in this workbook currently, we will need this below
752 $countCellXfs = count($this->cellXfCollection);
754 // copy all the shared cellXfs from the external workbook and append them to the current
755 foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
756 $this->addCellXf(clone $cellXf);
759 // move sheet to this workbook
760 $pSheet->rebindParent($this);
762 // update the cellXfs
763 foreach ($pSheet->getCellCollection(false) as $cellID) {
764 $cell = $pSheet->getCell($cellID);
765 $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
768 return $this->addSheet($pSheet, $iSheetIndex);
772 * Get named ranges
774 * @return PHPExcel_NamedRange[]
776 public function getNamedRanges()
778 return $this->namedRanges;
782 * Add named range
784 * @param PHPExcel_NamedRange $namedRange
785 * @return PHPExcel
787 public function addNamedRange(PHPExcel_NamedRange $namedRange)
789 if ($namedRange->getScope() == null) {
790 // global scope
791 $this->namedRanges[$namedRange->getName()] = $namedRange;
792 } else {
793 // local scope
794 $this->namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
796 return true;
800 * Get named range
802 * @param string $namedRange
803 * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
804 * @return PHPExcel_NamedRange|null
806 public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
808 $returnValue = null;
810 if ($namedRange != '' && ($namedRange !== null)) {
811 // first look for global defined name
812 if (isset($this->namedRanges[$namedRange])) {
813 $returnValue = $this->namedRanges[$namedRange];
816 // then look for local defined name (has priority over global defined name if both names exist)
817 if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
818 $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange];
822 return $returnValue;
826 * Remove named range
828 * @param string $namedRange
829 * @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope.
830 * @return PHPExcel
832 public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
834 if ($pSheet === null) {
835 if (isset($this->namedRanges[$namedRange])) {
836 unset($this->namedRanges[$namedRange]);
838 } else {
839 if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
840 unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
843 return $this;
847 * Get worksheet iterator
849 * @return PHPExcel_WorksheetIterator
851 public function getWorksheetIterator()
853 return new PHPExcel_WorksheetIterator($this);
857 * Copy workbook (!= clone!)
859 * @return PHPExcel
861 public function copy()
863 $copied = clone $this;
865 $worksheetCount = count($this->workSheetCollection);
866 for ($i = 0; $i < $worksheetCount; ++$i) {
867 $this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy();
868 $this->workSheetCollection[$i]->rebindParent($this);
871 return $copied;
875 * Implement PHP __clone to create a deep clone, not just a shallow copy.
877 public function __clone()
879 foreach ($this as $key => $val) {
880 if (is_object($val) || (is_array($val))) {
881 $this->{$key} = unserialize(serialize($val));
887 * Get the workbook collection of cellXfs
889 * @return PHPExcel_Style[]
891 public function getCellXfCollection()
893 return $this->cellXfCollection;
897 * Get cellXf by index
899 * @param int $pIndex
900 * @return PHPExcel_Style
902 public function getCellXfByIndex($pIndex = 0)
904 return $this->cellXfCollection[$pIndex];
908 * Get cellXf by hash code
910 * @param string $pValue
911 * @return PHPExcel_Style|false
913 public function getCellXfByHashCode($pValue = '')
915 foreach ($this->cellXfCollection as $cellXf) {
916 if ($cellXf->getHashCode() == $pValue) {
917 return $cellXf;
920 return false;
924 * Check if style exists in style collection
926 * @param PHPExcel_Style $pCellStyle
927 * @return boolean
929 public function cellXfExists($pCellStyle = null)
931 return in_array($pCellStyle, $this->cellXfCollection, true);
935 * Get default style
937 * @return PHPExcel_Style
938 * @throws PHPExcel_Exception
940 public function getDefaultStyle()
942 if (isset($this->cellXfCollection[0])) {
943 return $this->cellXfCollection[0];
945 throw new PHPExcel_Exception('No default style found for this workbook');
949 * Add a cellXf to the workbook
951 * @param PHPExcel_Style $style
953 public function addCellXf(PHPExcel_Style $style)
955 $this->cellXfCollection[] = $style;
956 $style->setIndex(count($this->cellXfCollection) - 1);
960 * Remove cellXf by index. It is ensured that all cells get their xf index updated.
962 * @param integer $pIndex Index to cellXf
963 * @throws PHPExcel_Exception
965 public function removeCellXfByIndex($pIndex = 0)
967 if ($pIndex > count($this->cellXfCollection) - 1) {
968 throw new PHPExcel_Exception("CellXf index is out of bounds.");
969 } else {
970 // first remove the cellXf
971 array_splice($this->cellXfCollection, $pIndex, 1);
973 // then update cellXf indexes for cells
974 foreach ($this->workSheetCollection as $worksheet) {
975 foreach ($worksheet->getCellCollection(false) as $cellID) {
976 $cell = $worksheet->getCell($cellID);
977 $xfIndex = $cell->getXfIndex();
978 if ($xfIndex > $pIndex) {
979 // decrease xf index by 1
980 $cell->setXfIndex($xfIndex - 1);
981 } elseif ($xfIndex == $pIndex) {
982 // set to default xf index 0
983 $cell->setXfIndex(0);
991 * Get the cellXf supervisor
993 * @return PHPExcel_Style
995 public function getCellXfSupervisor()
997 return $this->cellXfSupervisor;
1001 * Get the workbook collection of cellStyleXfs
1003 * @return PHPExcel_Style[]
1005 public function getCellStyleXfCollection()
1007 return $this->cellStyleXfCollection;
1011 * Get cellStyleXf by index
1013 * @param integer $pIndex Index to cellXf
1014 * @return PHPExcel_Style
1016 public function getCellStyleXfByIndex($pIndex = 0)
1018 return $this->cellStyleXfCollection[$pIndex];
1022 * Get cellStyleXf by hash code
1024 * @param string $pValue
1025 * @return PHPExcel_Style|false
1027 public function getCellStyleXfByHashCode($pValue = '')
1029 foreach ($this->cellStyleXfCollection as $cellStyleXf) {
1030 if ($cellStyleXf->getHashCode() == $pValue) {
1031 return $cellStyleXf;
1034 return false;
1038 * Add a cellStyleXf to the workbook
1040 * @param PHPExcel_Style $pStyle
1042 public function addCellStyleXf(PHPExcel_Style $pStyle)
1044 $this->cellStyleXfCollection[] = $pStyle;
1045 $pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
1049 * Remove cellStyleXf by index
1051 * @param integer $pIndex Index to cellXf
1052 * @throws PHPExcel_Exception
1054 public function removeCellStyleXfByIndex($pIndex = 0)
1056 if ($pIndex > count($this->cellStyleXfCollection) - 1) {
1057 throw new PHPExcel_Exception("CellStyleXf index is out of bounds.");
1058 } else {
1059 array_splice($this->cellStyleXfCollection, $pIndex, 1);
1064 * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
1065 * and columns in the workbook
1067 public function garbageCollect()
1069 // how many references are there to each cellXf ?
1070 $countReferencesCellXf = array();
1071 foreach ($this->cellXfCollection as $index => $cellXf) {
1072 $countReferencesCellXf[$index] = 0;
1075 foreach ($this->getWorksheetIterator() as $sheet) {
1076 // from cells
1077 foreach ($sheet->getCellCollection(false) as $cellID) {
1078 $cell = $sheet->getCell($cellID);
1079 ++$countReferencesCellXf[$cell->getXfIndex()];
1082 // from row dimensions
1083 foreach ($sheet->getRowDimensions() as $rowDimension) {
1084 if ($rowDimension->getXfIndex() !== null) {
1085 ++$countReferencesCellXf[$rowDimension->getXfIndex()];
1089 // from column dimensions
1090 foreach ($sheet->getColumnDimensions() as $columnDimension) {
1091 ++$countReferencesCellXf[$columnDimension->getXfIndex()];
1095 // remove cellXfs without references and create mapping so we can update xfIndex
1096 // for all cells and columns
1097 $countNeededCellXfs = 0;
1098 foreach ($this->cellXfCollection as $index => $cellXf) {
1099 if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
1100 ++$countNeededCellXfs;
1101 } else {
1102 unset($this->cellXfCollection[$index]);
1104 $map[$index] = $countNeededCellXfs - 1;
1106 $this->cellXfCollection = array_values($this->cellXfCollection);
1108 // update the index for all cellXfs
1109 foreach ($this->cellXfCollection as $i => $cellXf) {
1110 $cellXf->setIndex($i);
1113 // make sure there is always at least one cellXf (there should be)
1114 if (empty($this->cellXfCollection)) {
1115 $this->cellXfCollection[] = new PHPExcel_Style();
1118 // update the xfIndex for all cells, row dimensions, column dimensions
1119 foreach ($this->getWorksheetIterator() as $sheet) {
1120 // for all cells
1121 foreach ($sheet->getCellCollection(false) as $cellID) {
1122 $cell = $sheet->getCell($cellID);
1123 $cell->setXfIndex($map[$cell->getXfIndex()]);
1126 // for all row dimensions
1127 foreach ($sheet->getRowDimensions() as $rowDimension) {
1128 if ($rowDimension->getXfIndex() !== null) {
1129 $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
1133 // for all column dimensions
1134 foreach ($sheet->getColumnDimensions() as $columnDimension) {
1135 $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
1138 // also do garbage collection for all the sheets
1139 $sheet->garbageCollect();
1144 * Return the unique ID value assigned to this spreadsheet workbook
1146 * @return string
1148 public function getID()
1150 return $this->uniqueID;