I am getting an error something like below when writing a file
Error: Call to a member function getCacheData() on null
File E:\websites\d3a-qa\vendor\PHPExcel\PHPExcel\Worksheet.php
Line: 2554
Line 2554:
public function garbageCollect() {
// Flush cache
$this->_cellCollection->getCacheData('A1');
I know this error is coming because $this->_cellCollection
is null
but I dont know why is it null
. I am calling $writer->save($saving_name);
where it gives this issue.
I tried doing all sort of things. From backtracing to commenting the disconnectWorksheet code. resetting the variables for PHPExcel object but nothing works.
The funny part is sometimes it works and sometimes it doesn't. There's not much documentation or answers to queries aboout PHPExcel on internet. Therefore at last I have come here so that I could get any answer or at least a hint of what needs to be done.
Thanks in Advance.
EDIT
Here's my code where I am using PHPExcel. It appears that the error is coming at $writer->save($saving_name);
.
$parts
is an array having several number of XLS that needs to be merged into one XLS sheet.
PS: The error comes only when heavy processing is done (or nearly 45 sheets). For smaller xls counts its working fine.
if(!empty($insertSummary)) {
$PHPExcel = new \PHPExcel();
$PHPExcel = $PHPExcel->createSheet();
$PHPExcel->setTitle(_EXPORT_DES_SUMMARY);
}
foreach ($parts as $part) {
$bigExcel = '';
if(!empty($insertSummary))
$bigExcel = $this->addSummarySheet($insertSummary, $part, $PHPExcel);
$bigExcel = $this->processPartFiles($part, $bigExcel);
$writer = \PHPExcel_IOFactory::createWriter($bigExcel, 'Excel5');
$returnFilename = $dbName . '_' . 'DES' . '_' . date('Y-m-d-h-i-s') . '_' . 'Part_' . ++$count . '.xls';
// name of file, which needs to be attached during email sending
$saving_name = _DES_PATH_WEBROOT . DS . $returnFilename;
$zipFiles[] = $saving_name;
$writer->save($saving_name);
//Log::write('debug', 'Line(' . __LINE__ .') memory_get_peak_usage - ' . memory_get_peak_usage(true));
//Log::write('debug', 'Line(' . __LINE__ .') memory_get_usage - ' . memory_get_usage());
}
EDIT-2
$this->addSummarySheet()
public function addSummarySheet($insertSummary, $part, $PHPExcel) {
$keys = array_keys($part);
$bigExcel = $this->CommonInterface->readXlsOrCsv($insertSummary, false);
//Copy & paste values of range of cells
$cellValuesTitle = $bigExcel->getActiveSheet()->rangeToArray('A1:C1');
$cellValues = $bigExcel->getActiveSheet()->rangeToArray('A' . ($keys[0] + 1) .':C' . ($keys[count($keys) - 1] + 1));
$bigExcel->removeSheetByIndex(0);
$bigExcel->addSheet($PHPExcel);
$bigExcel->getActiveSheet()->fromArray($cellValuesTitle, null, 'A1');
$bigExcel->getActiveSheet()->fromArray($cellValues, null, 'A2');
foreach($keys as $rowCount => $sheet) {
$cellCordinateRow = $rowCount + 2;
$bigExcel->getActiveSheet()->getCell('A' . $cellCordinateRow)->getHyperlink()->setUrl("sheet://'Data $sheet'!A{$cellCordinateRow}");
}
return $bigExcel;
}
$this->CommonInterface->readXlsOrCsv()
public function readXlsOrCsv($filename = null, $unlinkFile = true, $sheetnames = null) {
require_once(ROOT . DS . 'vendor' . DS . 'PHPExcel' . DS . 'PHPExcel' . DS . 'IOFactory.php');
/** Identify the type of $inputFileName **/
$inputFileType = \PHPExcel_IOFactory::identify($filename);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
if(!empty($sheetnames)) {
//$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly($sheetnames);
}
$objPHPExcel = $objReader->load($filename);
//$objPHPExcel = \PHPExcel_IOFactory::load($filename);
if ($unlinkFile == true)
$this->unlinkFiles($filename); // Delete The uploaded file
return $objPHPExcel;
}