You are here

private function PHPExcel_Writer_HTML::_calculateSpans in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php \PHPExcel_Writer_HTML::_calculateSpans()

* Calculate information about HTML colspan and rowspan which is not always the same as Excel's

2 calls to PHPExcel_Writer_HTML::_calculateSpans()
PHPExcel_Writer_HTML::buildCSS in vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php
* Build CSS styles * *
PHPExcel_Writer_HTML::generateSheetData in vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php
* Generate sheet data * *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php, line 1438

Class

PHPExcel_Writer_HTML
PHPExcel_Writer_HTML

Code

private function _calculateSpans() {

  // Identify all cells that should be omitted in HTML due to cell merge.
  // In HTML only the upper-left cell should be written and it should have
  //   appropriate rowspan / colspan attribute
  $sheetIndexes = $this->_sheetIndex !== null ? array(
    $this->_sheetIndex,
  ) : range(0, $this->_phpExcel
    ->getSheetCount() - 1);
  foreach ($sheetIndexes as $sheetIndex) {
    $sheet = $this->_phpExcel
      ->getSheet($sheetIndex);
    $candidateSpannedRow = array();

    // loop through all Excel merged cells
    foreach ($sheet
      ->getMergeCells() as $cells) {
      list($cells, ) = PHPExcel_Cell::splitRange($cells);
      $first = $cells[0];
      $last = $cells[1];
      list($fc, $fr) = PHPExcel_Cell::coordinateFromString($first);
      $fc = PHPExcel_Cell::columnIndexFromString($fc) - 1;
      list($lc, $lr) = PHPExcel_Cell::coordinateFromString($last);
      $lc = PHPExcel_Cell::columnIndexFromString($lc) - 1;

      // loop through the individual cells in the individual merge
      $r = $fr - 1;
      while ($r++ < $lr) {

        // also, flag this row as a HTML row that is candidate to be omitted
        $candidateSpannedRow[$r] = $r;
        $c = $fc - 1;
        while ($c++ < $lc) {
          if (!($c == $fc && $r == $fr)) {

            // not the upper-left cell (should not be written in HTML)
            $this->_isSpannedCell[$sheetIndex][$r][$c] = array(
              'baseCell' => array(
                $fr,
                $fc,
              ),
            );
          }
          else {

            // upper-left is the base cell that should hold the colspan/rowspan attribute
            $this->_isBaseCell[$sheetIndex][$r][$c] = array(
              'xlrowspan' => $lr - $fr + 1,
              // Excel rowspan
              'rowspan' => $lr - $fr + 1,
              // HTML rowspan, value may change
              'xlcolspan' => $lc - $fc + 1,
              // Excel colspan
              'colspan' => $lc - $fc + 1,
            );
          }
        }
      }
    }

    // Identify which rows should be omitted in HTML. These are the rows where all the cells
    //   participate in a merge and the where base cells are somewhere above.
    $countColumns = PHPExcel_Cell::columnIndexFromString($sheet
      ->getHighestColumn());
    foreach ($candidateSpannedRow as $rowIndex) {
      if (isset($this->_isSpannedCell[$sheetIndex][$rowIndex])) {
        if (count($this->_isSpannedCell[$sheetIndex][$rowIndex]) == $countColumns) {
          $this->_isSpannedRow[$sheetIndex][$rowIndex] = $rowIndex;
        }
      }
    }

    // For each of the omitted rows we found above, the affected rowspans should be subtracted by 1
    if (isset($this->_isSpannedRow[$sheetIndex])) {
      foreach ($this->_isSpannedRow[$sheetIndex] as $rowIndex) {
        $adjustedBaseCells = array();
        $c = -1;
        $e = $countColumns - 1;
        while ($c++ < $e) {
          $baseCell = $this->_isSpannedCell[$sheetIndex][$rowIndex][$c]['baseCell'];
          if (!in_array($baseCell, $adjustedBaseCells)) {

            // subtract rowspan by 1
            --$this->_isBaseCell[$sheetIndex][$baseCell[0]][$baseCell[1]]['rowspan'];
            $adjustedBaseCells[] = $baseCell;
          }
        }
      }
    }

    // TODO: Same for columns
  }

  // We have calculated the spans
  $this->_spansAreCalculated = true;
}