I use library PHPExcel 1.7.9
to work with Excel
files. First, I create a template, stylise and polish it. Then, to avoid style hardcoding, using the above mentioned library I open that template, change some values and save it as a new .xlsx
file.
First, we fetch that style from cells.
$this->styles = array() ;
$this->styles['category'] = $sheet->getStyle("A4");
$this->styles['subcategory'] = $sheet->getStyle("A5");
Here is the recursive function, that displays categories and subcategories.
private function displayCategories($categories, &$row, $level = 0){
$sheet = $this->content ;
foreach($categories as $category){
if ($category->hasChildren() || $category->hasItems()){ //Check if the row has changed.
$sheet->getRowDimension($row)->setRowHeight(20);
$sheet->mergeCells(Cell::NUMBER . $row . ":" . Cell::TOTAL . $row) ;
$name = ($level == 0) ? strtoupper($category->name) : str_repeat(" ", $level*6) ."- {$category->name}" ;
$sheet->setCellValue(Cell::NUMBER . $row, $name) ;
$sheet->duplicateStyle((($level == 0) ? $this->styles['category'] : $this->styles['subcategory']), Cell::NUMBER . $row);
$row++ ;
if ($category->hasChildren()){
$this->displayCategories($category->children, $row, $level+1);
}
}
}
}
The problem
If $sheet->duplicateStyle()
is used, it will be impossible to save document because of infinite recursion.
Maximum function nesting level of '200' reached, aborting! <- FATAL ERROR
The problem is in the next piece of code, inside PHPExcel_Style_Fill
class, one object is referencing himself over and over.
public function getHashCode() { //class PHPExcel_Style_Fill
if ($this->_isSupervisor) {
var_dump($this === $this->getSharedComponent()); //Always true 200 times
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->getFillType()
. $this->getRotation()
. $this->getStartColor()->getHashCode()
. $this->getEndColor()->getHashCode()
. __CLASS__
);
}
Is any workaround to solve this? I would also accept any ideas on how to apply a complete style of one cell to another.
Solution:
As @MarkBaker said in comments, branch develop
on GitHub really contains fixes to the bug.