dtxooq1020 2013-09-11 13:29
浏览 84
已采纳

PHPExcel_Style_Fill无限递归

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.

  • 写回答

2条回答 默认 最新

  • dongzi3434 2013-09-27 07:31
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?