Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel fast duplicate row

Tags:

php

phpexcel

I have row with different styles for each cell. I need to duplicate it (copy text, style, size).

I was using following function to do it:

  function copyRowFull(&$ws_from, &$ws_to, $row_from, $row_to) {
    $ws_to->getRowDimension($row_to)->setRowHeight($ws_from->getRowDimension($row_from)->getRowHeight());
    $lastColumn = $ws_from->getHighestColumn();
    $rangeFrom = 'A'.$row_from.':'.$lastColumn.$row_from;
    // copy text
    $ws_to->fromArray($ws_from->rangeToArray($rangeFrom), null, 'A'.$row_to);
    // copy style
    ++$lastColumn;
    for ($c = 'A'; $c != $lastColumn; ++$c) {
      $ws_to->duplicateStyle($ws_from->getStyle($c.$row_from), $c.$row_to);
    }
  }

However it it VERY slow due to loop but I need it fast because many rows will be copied.

Also I have tried this for style copying:

$rangeTo = 'A'.$row_to.':'.$lastColumn.$row_to;
$ws_to->getStyle($rangeTo)->applyFromArray($ws_from->getStyle($rangeFrom));

But it doesn't work - throws error "Invalid style array passed".

Is there any faster method?

like image 497
Somnium Avatar asked Feb 10 '23 13:02

Somnium


2 Answers

After crawling in PHPExcel sources, I have figured out a lot faster way to duplicate row. It works copying only inside one workbook but it is what I needed. Posting it, maybe someone also had or will have similar problem.

function copyRowFull(&$ws_from, &$ws_to, $row_from, $row_to) {
  $ws_to->getRowDimension($row_to)->setRowHeight($ws_from->getRowDimension($row_from)->getRowHeight());
  $lastColumn = $ws_from->getHighestColumn();
  ++$lastColumn;
  for ($c = 'A'; $c != $lastColumn; ++$c) {
    $cell_from = $ws_from->getCell($c.$row_from);
    $cell_to = $ws_to->getCell($c.$row_to);
    $cell_to->setXfIndex($cell_from->getXfIndex()); // black magic here
    $cell_to->setValue($cell_from->getValue());
  }
}
like image 59
Somnium Avatar answered Feb 13 '23 03:02

Somnium


For those on PHP 5.3 or so, Somnium's answer blended with Ravean's comment and adapted for PHP 5.3:

    /**
 * Copies entire row with formatting and merging
 * @param $ws_from
 * @param $ws_to
 * @param $row_from
 * @param $row_to
 * @throws PHPExcel_Exception
 */
public function copyRowFull(&$ws_from, &$ws_to, $row_from, $row_to)
{
    $ws_to->getRowDimension($row_to)->setRowHeight($ws_from->getRowDimension($row_from)->getRowHeight());
    $lastColumn = $ws_from->getHighestColumn();
    ++$lastColumn;
    for ($c = 'A'; $c != $lastColumn; ++$c) {
        $cell_from = $ws_from->getCell($c . $row_from);
        if ($cell_from->isMergeRangeValueCell()) {
            $pCoordinateString = PHPExcel_Cell::splitRange($cell_from->getMergeRange());
            $coordinateFromString = PHPExcel_Cell::coordinateFromString($pCoordinateString[0][1]);
            $col = $coordinateFromString[0];
            $ws_to->mergeCells($c . $row_to . ':' . $col . $row_to);
            //$cell->getMergeRange()
        }
        $cell_to = $ws_to->getCell($c . $row_to);
        $cell_to->setXfIndex($cell_from->getXfIndex()); // black magic here
        $cell_to->setValue($cell_from->getValue());
    }
}
like image 44
Ilya Sheershoff Avatar answered Feb 13 '23 05:02

Ilya Sheershoff