You are here

public function PHPExcel::garbageCollect in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel.php \PHPExcel::garbageCollect()

Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells and columns in the workbook

File

vendor/phpoffice/phpexcel/Classes/PHPExcel.php, line 1052

Class

PHPExcel
PHPExcel

Code

public function garbageCollect() {

  // how many references are there to each cellXf ?
  $countReferencesCellXf = array();
  foreach ($this->_cellXfCollection as $index => $cellXf) {
    $countReferencesCellXf[$index] = 0;
  }
  foreach ($this
    ->getWorksheetIterator() as $sheet) {

    // from cells
    foreach ($sheet
      ->getCellCollection(false) as $cellID) {
      $cell = $sheet
        ->getCell($cellID);
      ++$countReferencesCellXf[$cell
        ->getXfIndex()];
    }

    // from row dimensions
    foreach ($sheet
      ->getRowDimensions() as $rowDimension) {
      if ($rowDimension
        ->getXfIndex() !== null) {
        ++$countReferencesCellXf[$rowDimension
          ->getXfIndex()];
      }
    }

    // from column dimensions
    foreach ($sheet
      ->getColumnDimensions() as $columnDimension) {
      ++$countReferencesCellXf[$columnDimension
        ->getXfIndex()];
    }
  }

  // remove cellXfs without references and create mapping so we can update xfIndex
  // for all cells and columns
  $countNeededCellXfs = 0;
  foreach ($this->_cellXfCollection as $index => $cellXf) {
    if ($countReferencesCellXf[$index] > 0 || $index == 0) {

      // we must never remove the first cellXf
      ++$countNeededCellXfs;
    }
    else {
      unset($this->_cellXfCollection[$index]);
    }
    $map[$index] = $countNeededCellXfs - 1;
  }
  $this->_cellXfCollection = array_values($this->_cellXfCollection);

  // update the index for all cellXfs
  foreach ($this->_cellXfCollection as $i => $cellXf) {
    $cellXf
      ->setIndex($i);
  }

  // make sure there is always at least one cellXf (there should be)
  if (empty($this->_cellXfCollection)) {
    $this->_cellXfCollection[] = new PHPExcel_Style();
  }

  // update the xfIndex for all cells, row dimensions, column dimensions
  foreach ($this
    ->getWorksheetIterator() as $sheet) {

    // for all cells
    foreach ($sheet
      ->getCellCollection(false) as $cellID) {
      $cell = $sheet
        ->getCell($cellID);
      $cell
        ->setXfIndex($map[$cell
        ->getXfIndex()]);
    }

    // for all row dimensions
    foreach ($sheet
      ->getRowDimensions() as $rowDimension) {
      if ($rowDimension
        ->getXfIndex() !== null) {
        $rowDimension
          ->setXfIndex($map[$rowDimension
          ->getXfIndex()]);
      }
    }

    // for all column dimensions
    foreach ($sheet
      ->getColumnDimensions() as $columnDimension) {
      $columnDimension
        ->setXfIndex($map[$columnDimension
        ->getXfIndex()]);
    }

    // also do garbage collection for all the sheets
    $sheet
      ->garbageCollect();
  }
}