Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel_Style_Fill infinite recursion

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.

like image 701
sybear Avatar asked Sep 11 '13 13:09

sybear


2 Answers

EDIT I added a pull request with a fix: https://github.com/PHPOffice/PHPExcel/pull/251

This happens when you try to duplicate cell's style to the same cell; Take a look at this:

$phpe = new PHPExcel();
$sheet = $phpe->createSheet();

$sheet->setCellValue('A1', 'hi there') ;
$sheet->setCellValue('A2', 'hi again') ;

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A2');

$writer = new PHPExcel_Writer_Excel2007($phpe);
$writer->save('./test.xlsx');

It will work just fine. BUT if I add another line like this:

$sheet->duplicateStyle($sheet->getStyle('A1'), 'A1');

then bang, infinite recursion starts after calling the save method

To fix your code, you should modify this part:

$sheet->duplicateStyle((($level == 0) ?  $this->styles['category'] : $this->styles['subcategory']), Cell::NUMBER . $row);

To something along the lines of:

$style = ($level == 0) ?  $this->styles['category'] : $this->styles['subcategory'];
$targetCoords = Cell::NUMBER . $row;
if($style->getActiveCell() != $targetCoords) {
    $sheet->duplicateStyle($style, $targetCoords);
}
like image 82
Adam Zielinski Avatar answered Oct 31 '22 19:10

Adam Zielinski


Without knowing the specifics of the library...

How about changing this:

public function getHashCode() { //class PHPExcel_Style_Fill
    if ($this->_isSupervisor) { 

To this:

public function getHashCode() { //class PHPExcel_Style_Fill
    if ($this->_isSupervisor && ( $this != $this->getSharedComponent() ) ) { 

If the hash code logic after the if statement does not apply to _isSupervisor, then add another logic statement and return a fixed value, like this:

public function getHashCode() { //class PHPExcel_Style_Fill
    if ($this->_isSupervisor) { 
        if ( $this == $this->getSharedComponent() )
            return md5(0);
like image 45
ash Avatar answered Oct 31 '22 19:10

ash