I have a row with different styles for each cell. I need to duplicate it (copy text, style, size).
I used the following function:
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;
$ws_to->fromArray($ws_from->rangeToArray($rangeFrom), null, 'A'.$row_to);
++$lastColumn;
for ($c = 'A'; $c != $lastColumn; ++$c) {
$ws_to->duplicateStyle($ws_from->getStyle($c.$row_from), $c.$row_to);
}
}
However, it is very slow due to the loop, but I need it quickly because many lines will be copied.
I also tried this to copy the style:
$rangeTo = 'A'.$row_to.':'.$lastColumn.$row_to;
$ws_to->getStyle($rangeTo)->applyFromArray($ws_from->getStyle($rangeFrom));
But this will not work - it throws the error "Invalid array of styles passed."
Is there a faster method?
source
share