PHPExcel_Style_Fill无限递归 [英] PHPExcel_Style_Fill infinite recursion
问题描述
我使用库PHPExcel 1.7.9
处理Excel
文件.首先,我创建一个模板,对其进行样式化和抛光.然后,为避免样式硬编码,使用上述库,我打开了该模板,更改了一些值并将其保存为新的.xlsx
文件.
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);
}
}
}
}
问题
如果使用$sheet->duplicateStyle()
,由于无限递归,将无法保存文档.
If $sheet->duplicateStyle()
is used, it will be impossible to save document because of infinite recursion.
达到最大功能嵌套级别'200',正在中止! <-致命错误
Maximum function nesting level of '200' reached, aborting! <- FATAL ERROR
问题出在PHPExcel_Style_Fill
类中的下一段代码中,一个对象反复引用自己.
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.
解决方案:
正如@MarkBaker在评论中所说,GitHub上的分支develop
确实包含对该错误的修复.
As @MarkBaker said in comments, branch develop
on GitHub really contains fixes to the bug.
推荐答案
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');
然后砰,在调用save
方法之后,无限递归开始
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);
类似于以下内容:
$style = ($level == 0) ? $this->styles['category'] : $this->styles['subcategory'];
$targetCoords = Cell::NUMBER . $row;
if($style->getActiveCell() != $targetCoords) {
$sheet->duplicateStyle($style, $targetCoords);
}
这篇关于PHPExcel_Style_Fill无限递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!