Workaround for copying style with PHPExcel -
i want copy style information cells ranges, format painter in excel. documentation says this:
$activesheet->duplicatestyle($activesheet->getstyle('a1'), 'd1:d100'); $activesheet->duplicatestyle($activesheet->getstyle('b1'), 'e1:e100');
there appears bug because both d1:d100 , e1:e100 style cell b1. if change order of 2 lines, both ranges style a1. similarly,
$stylea = $activesheet->getstyle('a1'); $styleb = $activesheet->getstyle('b1'); $activesheet->duplicatestyle($stylea, 'd1:d100');
results in d1:d100 getting style info cell b1. last getstyle value used in duplicatestyle results.
i'm sure future release of phpexcel have fix, need figure out work-around until then.
one workround might use style xf indexes:
$xfindex = $activesheet->getcell('a1')->getxfindex();
then set value xfindex of cells in range
for ($col = 'd'; $col != 'e'; ++$col) { ($row = 1; $row <= 100; ++$row) { $activesheet->getcell($col . $row)->setxfindex($xfindex); } }
edit
alternatively, apply fix duplicatestyle() method in classes/phpexcel/worksheet.php
lines 1479 1486 read:
if ($this->_parent->cellxfexists($pcellstyle)) { // there cell xf in our collection $xfindex = $pcellstyle->getindex(); } else { // don't have such cell xf, need add $workbook->addcellxf($pcellstyle); $xfindex = $pcellstyle->getindex(); }
change to:
if ($existingstyle = $this->_parent->getcellxfbyhashcode($pcellstyle->gethashcode())) { // there such cell xf in our collection $xfindex = $existingstyle->getindex(); } else { // don't have such cell xf, need add $workbook->addcellxf($pcellstyle); $xfindex = $pcellstyle->getindex(); }
similarly in applyfromarray() method in classes/phpexcel/style.php
lines 425 432 read:
if ($workbook->cellxfexists($newstyle)) { // there such cell xf in our collection $newxfindexes[$oldxfindex] = $existingstyle->getindex(); } else { // don't have such cell xf, need add $workbook->addcellxf($newstyle); $newxfindexes[$oldxfindex] = $newstyle->getindex(); }
change to:
if ($existingstyle = $workbook->getcellxfbyhashcode($newstyle->gethashcode())) { // there such cell xf in our collection $newxfindexes[$oldxfindex] = $existingstyle->getindex(); } else { // don't have such cell xf, need add $workbook->addcellxf($newstyle); $newxfindexes[$oldxfindex] = $newstyle->getindex(); }
edit #2
fix has been pushed develop branch on github. give slight performance hit, depending on number of styles in use... i'll try , faster version tomorrow night
Comments
Post a Comment