You are here

public function PHPExcel_Worksheet::duplicateStyle in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php \PHPExcel_Worksheet::duplicateStyle()

Duplicate cell style to a range of cells

Please note that this will overwrite existing cell styles for cells in range!

Parameters

PHPExcel_Style $pCellStyle Cell style to duplicate:

string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1"):

Return value

PHPExcel_Worksheet

Throws

PHPExcel_Exception

1 call to PHPExcel_Worksheet::duplicateStyle()
PHPExcel_Worksheet::setSharedStyle in vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php
Set shared cell style to a range of cells

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Worksheet.php, line 1535

Class

PHPExcel_Worksheet
PHPExcel_Worksheet

Code

public function duplicateStyle(PHPExcel_Style $pCellStyle = null, $pRange = '') {

  // make sure we have a real style and not supervisor
  $style = $pCellStyle
    ->getIsSupervisor() ? $pCellStyle
    ->getSharedComponent() : $pCellStyle;

  // Add the style to the workbook if necessary
  $workbook = $this->_parent;
  if ($existingStyle = $this->_parent
    ->getCellXfByHashCode($pCellStyle
    ->getHashCode())) {

    // there is already such cell Xf in our collection
    $xfIndex = $existingStyle
      ->getIndex();
  }
  else {

    // we don't have such a cell Xf, need to add
    $workbook
      ->addCellXf($pCellStyle);
    $xfIndex = $pCellStyle
      ->getIndex();
  }

  // Calculate range outer borders
  list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange . ':' . $pRange);

  // Make sure we can loop upwards on rows and columns
  if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
    $tmp = $rangeStart;
    $rangeStart = $rangeEnd;
    $rangeEnd = $tmp;
  }

  // Loop through cells and apply styles
  for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
    for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
      $this
        ->getCell(PHPExcel_Cell::stringFromColumnIndex($col - 1) . $row)
        ->setXfIndex($xfIndex);
    }
  }
  return $this;
}