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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。