30 if (!defined(
'PHPEXCEL_ROOT')) {
31 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/');
32 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
107 $this->_workSheetCollection = array();
109 $this->_activeSheetIndex = 0;
118 $this->_namedRanges = array();
122 $this->_cellXfSupervisor->bindParent($this);
131 foreach($this->_workSheetCollection as $k => &$worksheet) {
132 $worksheet->disconnectCells();
133 $this->_workSheetCollection[$k] = null;
136 $this->_workSheetCollection = array();
156 $this->_properties = $pValue;
176 $this->_security = $pValue;
197 $this->
addSheet($newSheet, $iSheetIndex);
211 if(is_null($iSheetIndex))
213 $this->_workSheetCollection[] = $pSheet;
219 $this->_workSheetCollection,
226 if ($this->_activeSheetIndex >= $iSheetIndex) {
242 if ($pIndex > count($this->_workSheetCollection) - 1) {
243 throw new Exception(
"Sheet index is out of bounds.");
245 array_splice($this->_workSheetCollection, $pIndex, 1);
258 if ($pIndex > count($this->_workSheetCollection) - 1) {
259 throw new Exception(
"Sheet index is out of bounds.");
261 return $this->_workSheetCollection[$pIndex];
284 $worksheetCount = count($this->_workSheetCollection);
285 for ($i = 0; $i < $worksheetCount; ++$i) {
286 if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
287 return $this->_workSheetCollection[$i];
303 foreach ($this->_workSheetCollection as $key => $value) {
304 if ($value->getHashCode() == $pSheet->
getHashCode()) {
321 $pSheet = array_splice(
322 $this->_workSheetCollection,
327 $this->_workSheetCollection,
342 return count($this->_workSheetCollection);
364 if ($pIndex > count($this->_workSheetCollection) - 1) {
365 throw new Exception(
"Active sheet index is out of bounds.");
367 $this->_activeSheetIndex = $pIndex;
386 throw new Exception(
'Workbook does not contain sheet:' . $pValue);
396 $returnValue = array();
398 for ($i = 0; $i < $worksheetCount; ++$i) {
399 array_push($returnValue, $this->
getSheet($i)->getTitle());
415 throw new Exception(
"Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
419 $countCellXfs = count($this->_cellXfCollection);
422 foreach ($pSheet->
getParent()->getCellXfCollection() as $cellXf) {
431 $cell = $pSheet->
getCell($cellID);
432 $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
435 return $this->
addSheet($pSheet, $iSheetIndex);
454 if ($namedRange->
getScope() == null) {
456 $this->_namedRanges[$namedRange->
getName()] = $namedRange;
459 $this->_namedRanges[$namedRange->
getScope()->getTitle().
'!'.$namedRange->
getName()] = $namedRange;
474 if ($namedRange !=
'' && !is_null($namedRange)) {
476 if (isset($this->_namedRanges[$namedRange])) {
477 $returnValue = $this->_namedRanges[$namedRange];
481 if (!is_null($pSheet) && isset($this->_namedRanges[$pSheet->getTitle() .
'!' . $namedRange])) {
482 $returnValue = $this->_namedRanges[$pSheet->getTitle() .
'!' . $namedRange];
497 if (is_null($pSheet)) {
498 if (isset($this->_namedRanges[$namedRange])) {
499 unset($this->_namedRanges[$namedRange]);
502 if (isset($this->_namedRanges[$pSheet->getTitle() .
'!' . $namedRange])) {
503 unset($this->_namedRanges[$pSheet->getTitle() .
'!' . $namedRange]);
524 $copied = clone $this;
526 $worksheetCount = count($this->_workSheetCollection);
527 for ($i = 0; $i < $worksheetCount; ++$i) {
528 $this->_workSheetCollection[$i] = $this->_workSheetCollection[$i]->copy();
529 $this->_workSheetCollection[$i]->rebindParent($this);
539 foreach($this as $key => $val) {
540 if (is_object($val) || (is_array($val))) {
541 $this->{$key} = unserialize(serialize($val));
564 return $this->_cellXfCollection[$pIndex];
575 foreach ($this->_cellXfCollection as $cellXf) {
576 if ($cellXf->getHashCode() == $pValue) {
591 if (isset($this->_cellXfCollection[0])) {
592 return $this->_cellXfCollection[0];
594 throw new Exception(
'No default style found for this workbook');
604 $this->_cellXfCollection[] = $style;
605 $style->
setIndex(count($this->_cellXfCollection) - 1);
616 if ($pIndex > count($this->_cellXfCollection) - 1) {
617 throw new Exception(
"CellXf index is out of bounds.");
620 array_splice($this->_cellXfCollection, $pIndex, 1);
623 foreach ($this->_workSheetCollection as $worksheet) {
624 foreach ($worksheet->getCellCollection(
false) as $cellID) {
625 $cell = $worksheet->getCell($cellID);
626 $xfIndex = $cell->getXfIndex();
627 if ($xfIndex > $pIndex ) {
629 $cell->setXfIndex($xfIndex - 1);
630 }
else if ($xfIndex == $pIndex) {
632 $cell->setXfIndex(0);
667 return $this->_cellStyleXfCollection[$pIndex];
678 foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
679 if ($cellStyleXf->getHashCode() == $pValue) {
693 $this->_cellStyleXfCollection[] = $pStyle;
694 $pStyle->
setIndex(count($this->_cellStyleXfCollection) - 1);
705 if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
706 throw new Exception(
"CellStyleXf index is out of bounds.");
708 array_splice($this->_cellStyleXfCollection, $pIndex, 1);
719 $countReferencesCellXf = array();
720 foreach ($this->_cellXfCollection as $index => $cellXf) {
721 $countReferencesCellXf[$index] = 0;
727 foreach ($sheet->getCellCollection(
false) as $cellID) {
728 $cell = $sheet->getCell($cellID);
729 ++$countReferencesCellXf[$cell->getXfIndex()];
733 foreach ($sheet->getRowDimensions() as $rowDimension) {
734 if ($rowDimension->getXfIndex() !== null) {
735 ++$countReferencesCellXf[$rowDimension->getXfIndex()];
740 foreach ($sheet->getColumnDimensions() as $columnDimension) {
741 ++$countReferencesCellXf[$columnDimension->getXfIndex()];
747 $countNeededCellXfs = 0;
748 foreach ($this->_cellXfCollection as $index => $cellXf) {
749 if ($countReferencesCellXf[$index] > 0 || $index == 0) {
750 ++$countNeededCellXfs;
752 unset($this->_cellXfCollection[$index]);
754 $map[$index] = $countNeededCellXfs - 1;
756 $this->_cellXfCollection = array_values($this->_cellXfCollection);
759 foreach ($this->_cellXfCollection as $i => $cellXf) {
760 $cellXf->setIndex($i);
764 if (count($this->_cellXfCollection) == 0) {
772 foreach ($sheet->getCellCollection(
false) as $cellID) {
773 $cell = $sheet->getCell($cellID);
774 $cell->setXfIndex( $map[$cell->getXfIndex()] );
778 foreach ($sheet->getRowDimensions() as $rowDimension) {
779 if ($rowDimension->getXfIndex() !== null) {
780 $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] );
785 foreach ($sheet->getColumnDimensions() as $columnDimension) {
786 $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] );
792 $sheet->garbageCollect();