Fatal Error when calling PHPExcel_Worksheet->disconnectCells()

Topics: Developer Forum
Dec 2, 2010 at 4:19 PM

I was getting a PHP fatal error (on line 357) when calling PHPExcel_Worksheet->disconnectCells().  Well, actually I was indirectly calling that method by calling PHPExcel->disconnectWorksheets().

So, I went to the line mentioned and modified the code as follows:

 

// OLD CODE
public function disconnectWorksheets() {
	foreach($this->_workSheetCollection as $k => &$worksheet) {
		$worksheet->disconnectCells();
		$this->_workSheetCollection[$k] = null;
	}
	unset($worksheet);
	$this->_workSheetCollection = array();
}

// NEW CODE
public function disconnectWorksheets() {
	foreach($this->_workSheetCollection as $k => &$worksheet) {
		if ($worksheet instanceof PHPExcel_Worksheet) {
			$worksheet->disconnectCells();
		}
		$this->_workSheetCollection[$k] = null;
	}
	unset($worksheet);
	$this->_workSheetCollection = array();
}

This should not break anything.

 

Oh, and BTW:

Dec 2, 2010 at 4:38 PM

OOPS!!!

OK, the code change above was made to the PHPExcel class, not the class indicated above.  Also, I DID make a change to PHPExcel_Worksheet as indicated below:

// OLD CODE
public function disconnectCells() {
	$this->_cellCollection->unsetWorksheetCells();
        $this->_cellCollection = null;

	//	detach ourself from the workbook, so that it can then delete this worksheet successfully
	$this->_parent = null;
}
// NEW CODE
public function disconnectCells() {
	if (isset($this->_cellCollection)) {
		if ($this->_cellCollection instanceof PHPExcel_CachedObjectStorageFactory) {
			$this->_cellCollection->unsetWorksheetCells();
		}
		$this->_cellCollection = null;
	}

	//	detach ourself from the workbook, so that it can then delete this worksheet successfully
	$this->_parent = null;
}

Again, this should work without breaking anything.

 

Coordinator
Dec 2, 2010 at 9:47 PM

I'll have a look at doing this, but I'm more concerned with why there might be entries in the Workbook's _workSheetCollection array that aren't instances of PHPExcel_Worksheet

Dec 2, 2010 at 11:53 PM

I agree.  I will check it on my end too.