ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
PHPExcel.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
31  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
32  require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
33 }
34 
35 
43 class PHPExcel
44 {
50  private $_properties;
51 
57  private $_security;
58 
64  private $_workSheetCollection = array();
65 
71  private $_activeSheetIndex = 0;
72 
78  private $_namedRanges = array();
79 
86 
92  private $_cellXfCollection = array();
93 
99  private $_cellStyleXfCollection = array();
100 
104  public function __construct()
105  {
106  // Initialise worksheet collection and add one worksheet
107  $this->_workSheetCollection = array();
108  $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
109  $this->_activeSheetIndex = 0;
110 
111  // Create document properties
112  $this->_properties = new PHPExcel_DocumentProperties();
113 
114  // Create document security
115  $this->_security = new PHPExcel_DocumentSecurity();
116 
117  // Set named ranges
118  $this->_namedRanges = array();
119 
120  // Create the cellXf supervisor
121  $this->_cellXfSupervisor = new PHPExcel_Style(true);
122  $this->_cellXfSupervisor->bindParent($this);
123 
124  // Create the default style
125  $this->addCellXf(new PHPExcel_Style);
126  $this->addCellStyleXf(new PHPExcel_Style);
127  }
128 
129 
130  public function disconnectWorksheets() {
131  foreach($this->_workSheetCollection as $k => &$worksheet) {
132  $worksheet->disconnectCells();
133  $this->_workSheetCollection[$k] = null;
134  }
135  unset($worksheet);
136  $this->_workSheetCollection = array();
137  }
138 
144  public function getProperties()
145  {
146  return $this->_properties;
147  }
148 
155  {
156  $this->_properties = $pValue;
157  }
158 
164  public function getSecurity()
165  {
166  return $this->_security;
167  }
168 
174  public function setSecurity(PHPExcel_DocumentSecurity $pValue)
175  {
176  $this->_security = $pValue;
177  }
178 
184  public function getActiveSheet()
185  {
186  return $this->_workSheetCollection[$this->_activeSheetIndex];
187  }
188 
194  public function createSheet($iSheetIndex = null)
195  {
196  $newSheet = new PHPExcel_Worksheet($this);
197  $this->addSheet($newSheet, $iSheetIndex);
198  return $newSheet;
199  }
200 
209  public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null)
210  {
211  if(is_null($iSheetIndex))
212  {
213  $this->_workSheetCollection[] = $pSheet;
214  }
215  else
216  {
217  // Insert the sheet at the requested index
218  array_splice(
219  $this->_workSheetCollection,
220  $iSheetIndex,
221  0,
222  array($pSheet)
223  );
224 
225  // Adjust active sheet index if necessary
226  if ($this->_activeSheetIndex >= $iSheetIndex) {
228  }
229 
230  }
231  return $pSheet;
232  }
233 
240  public function removeSheetByIndex($pIndex = 0)
241  {
242  if ($pIndex > count($this->_workSheetCollection) - 1) {
243  throw new Exception("Sheet index is out of bounds.");
244  } else {
245  array_splice($this->_workSheetCollection, $pIndex, 1);
246  }
247  }
248 
256  public function getSheet($pIndex = 0)
257  {
258  if ($pIndex > count($this->_workSheetCollection) - 1) {
259  throw new Exception("Sheet index is out of bounds.");
260  } else {
261  return $this->_workSheetCollection[$pIndex];
262  }
263  }
264 
270  public function getAllSheets()
271  {
273  }
274 
282  public function getSheetByName($pName = '')
283  {
284  $worksheetCount = count($this->_workSheetCollection);
285  for ($i = 0; $i < $worksheetCount; ++$i) {
286  if ($this->_workSheetCollection[$i]->getTitle() == $pName) {
287  return $this->_workSheetCollection[$i];
288  }
289  }
290 
291  return null;
292  }
293 
301  public function getIndex(PHPExcel_Worksheet $pSheet)
302  {
303  foreach ($this->_workSheetCollection as $key => $value) {
304  if ($value->getHashCode() == $pSheet->getHashCode()) {
305  return $key;
306  }
307  }
308  }
309 
318  public function setIndexByName($sheetName, $newIndex)
319  {
320  $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
321  $pSheet = array_splice(
322  $this->_workSheetCollection,
323  $oldIndex,
324  1
325  );
326  array_splice(
327  $this->_workSheetCollection,
328  $newIndex,
329  0,
330  $pSheet
331  );
332  return $newIndex;
333  }
334 
340  public function getSheetCount()
341  {
342  return count($this->_workSheetCollection);
343  }
344 
350  public function getActiveSheetIndex()
351  {
353  }
354 
362  public function setActiveSheetIndex($pIndex = 0)
363  {
364  if ($pIndex > count($this->_workSheetCollection) - 1) {
365  throw new Exception("Active sheet index is out of bounds.");
366  } else {
367  $this->_activeSheetIndex = $pIndex;
368  }
369  return $this->getActiveSheet();
370  }
371 
379  public function setActiveSheetIndexByName($pValue = '')
380  {
381  if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
382  $this->setActiveSheetIndex($worksheet->getParent()->getIndex($worksheet));
383  return $worksheet;
384  }
385 
386  throw new Exception('Workbook does not contain sheet:' . $pValue);
387  }
388 
394  public function getSheetNames()
395  {
396  $returnValue = array();
397  $worksheetCount = $this->getSheetCount();
398  for ($i = 0; $i < $worksheetCount; ++$i) {
399  array_push($returnValue, $this->getSheet($i)->getTitle());
400  }
401 
402  return $returnValue;
403  }
404 
413  public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) {
414  if (!is_null($this->getSheetByName($pSheet->getTitle()))) {
415  throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
416  }
417 
418  // count how many cellXfs there are in this workbook currently, we will need this below
419  $countCellXfs = count($this->_cellXfCollection);
420 
421  // copy all the shared cellXfs from the external workbook and append them to the current
422  foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
423  $this->addCellXf(clone $cellXf);
424  }
425 
426  // move sheet to this workbook
427  $pSheet->rebindParent($this);
428 
429  // update the cellXfs
430  foreach ($pSheet->getCellCollection(false) as $cellID) {
431  $cell = $pSheet->getCell($cellID);
432  $cell->setXfIndex( $cell->getXfIndex() + $countCellXfs );
433  }
434 
435  return $this->addSheet($pSheet, $iSheetIndex);
436  }
437 
443  public function getNamedRanges() {
444  return $this->_namedRanges;
445  }
446 
453  public function addNamedRange(PHPExcel_NamedRange $namedRange) {
454  if ($namedRange->getScope() == null) {
455  // global scope
456  $this->_namedRanges[$namedRange->getName()] = $namedRange;
457  } else {
458  // local scope
459  $this->_namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
460  }
461  return true;
462  }
463 
471  public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
472  $returnValue = null;
473 
474  if ($namedRange != '' && !is_null($namedRange)) {
475  // first look for global defined name
476  if (isset($this->_namedRanges[$namedRange])) {
477  $returnValue = $this->_namedRanges[$namedRange];
478  }
479 
480  // then look for local defined name (has priority over global defined name if both names exist)
481  if (!is_null($pSheet) && isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
482  $returnValue = $this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange];
483  }
484  }
485 
486  return $returnValue;
487  }
488 
496  public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null) {
497  if (is_null($pSheet)) {
498  if (isset($this->_namedRanges[$namedRange])) {
499  unset($this->_namedRanges[$namedRange]);
500  }
501  } else {
502  if (isset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
503  unset($this->_namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
504  }
505  }
506  return $this;
507  }
508 
514  public function getWorksheetIterator() {
515  return new PHPExcel_WorksheetIterator($this);
516  }
517 
523  public function copy() {
524  $copied = clone $this;
525 
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);
530  }
531 
532  return $copied;
533  }
534 
538  public function __clone() {
539  foreach($this as $key => $val) {
540  if (is_object($val) || (is_array($val))) {
541  $this->{$key} = unserialize(serialize($val));
542  }
543  }
544  }
545 
551  public function getCellXfCollection()
552  {
554  }
555 
562  public function getCellXfByIndex($pIndex = 0)
563  {
564  return $this->_cellXfCollection[$pIndex];
565  }
566 
573  public function getCellXfByHashCode($pValue = '')
574  {
575  foreach ($this->_cellXfCollection as $cellXf) {
576  if ($cellXf->getHashCode() == $pValue) {
577  return $cellXf;
578  }
579  }
580  return false;
581  }
582 
589  public function getDefaultStyle()
590  {
591  if (isset($this->_cellXfCollection[0])) {
592  return $this->_cellXfCollection[0];
593  }
594  throw new Exception('No default style found for this workbook');
595  }
596 
602  public function addCellXf(PHPExcel_Style $style)
603  {
604  $this->_cellXfCollection[] = $style;
605  $style->setIndex(count($this->_cellXfCollection) - 1);
606  }
607 
614  public function removeCellXfByIndex($pIndex = 0)
615  {
616  if ($pIndex > count($this->_cellXfCollection) - 1) {
617  throw new Exception("CellXf index is out of bounds.");
618  } else {
619  // first remove the cellXf
620  array_splice($this->_cellXfCollection, $pIndex, 1);
621 
622  // then update cellXf indexes for cells
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 ) {
628  // decrease xf index by 1
629  $cell->setXfIndex($xfIndex - 1);
630  } else if ($xfIndex == $pIndex) {
631  // set to default xf index 0
632  $cell->setXfIndex(0);
633  }
634  }
635  }
636  }
637  }
638 
644  public function getCellXfSupervisor()
645  {
647  }
648 
654  public function getCellStyleXfCollection()
655  {
657  }
658 
665  public function getCellStyleXfByIndex($pIndex = 0)
666  {
667  return $this->_cellStyleXfCollection[$pIndex];
668  }
669 
676  public function getCellStyleXfByHashCode($pValue = '')
677  {
678  foreach ($this->_cellXfStyleCollection as $cellStyleXf) {
679  if ($cellStyleXf->getHashCode() == $pValue) {
680  return $cellStyleXf;
681  }
682  }
683  return false;
684  }
685 
691  public function addCellStyleXf(PHPExcel_Style $pStyle)
692  {
693  $this->_cellStyleXfCollection[] = $pStyle;
694  $pStyle->setIndex(count($this->_cellStyleXfCollection) - 1);
695  }
696 
703  public function removeCellStyleXfByIndex($pIndex = 0)
704  {
705  if ($pIndex > count($this->_cellStyleXfCollection) - 1) {
706  throw new Exception("CellStyleXf index is out of bounds.");
707  } else {
708  array_splice($this->_cellStyleXfCollection, $pIndex, 1);
709  }
710  }
711 
716  public function garbageCollect()
717  {
718  // how many references are there to each cellXf ?
719  $countReferencesCellXf = array();
720  foreach ($this->_cellXfCollection as $index => $cellXf) {
721  $countReferencesCellXf[$index] = 0;
722  }
723 
724  foreach ($this->getWorksheetIterator() as $sheet) {
725 
726  // from cells
727  foreach ($sheet->getCellCollection(false) as $cellID) {
728  $cell = $sheet->getCell($cellID);
729  ++$countReferencesCellXf[$cell->getXfIndex()];
730  }
731 
732  // from row dimensions
733  foreach ($sheet->getRowDimensions() as $rowDimension) {
734  if ($rowDimension->getXfIndex() !== null) {
735  ++$countReferencesCellXf[$rowDimension->getXfIndex()];
736  }
737  }
738 
739  // from column dimensions
740  foreach ($sheet->getColumnDimensions() as $columnDimension) {
741  ++$countReferencesCellXf[$columnDimension->getXfIndex()];
742  }
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 
758  // update the index for all cellXfs
759  foreach ($this->_cellXfCollection as $i => $cellXf) {
760  $cellXf->setIndex($i);
761  }
762 
763  // make sure there is always at least one cellXf (there should be)
764  if (count($this->_cellXfCollection) == 0) {
765  $this->_cellXfCollection[] = new PHPExcel_Style();
766  }
767 
768  // update the xfIndex for all cells, row dimensions, column dimensions
769  foreach ($this->getWorksheetIterator() as $sheet) {
770 
771  // for all cells
772  foreach ($sheet->getCellCollection(false) as $cellID) {
773  $cell = $sheet->getCell($cellID);
774  $cell->setXfIndex( $map[$cell->getXfIndex()] );
775  }
776 
777  // for all row dimensions
778  foreach ($sheet->getRowDimensions() as $rowDimension) {
779  if ($rowDimension->getXfIndex() !== null) {
780  $rowDimension->setXfIndex( $map[$rowDimension->getXfIndex()] );
781  }
782  }
783 
784  // for all column dimensions
785  foreach ($sheet->getColumnDimensions() as $columnDimension) {
786  $columnDimension->setXfIndex( $map[$columnDimension->getXfIndex()] );
787  }
788  }
789 
790  // also do garbage collection for all the sheets
791  foreach ($this->getWorksheetIterator() as $sheet) {
792  $sheet->garbageCollect();
793  }
794  }
795 
796 }