Bug with copy function, changes on new pages appear on old one

Topics: Developer Forum
Oct 1, 2010 at 1:25 PM
Edited Oct 1, 2010 at 1:31 PM

Hi ,

I try to create multi sheet document, so I use sheet N2 as template, copy it several times and then put data to new pages, but new data appears on template sheet too, also data on new pages is messed. Looks like copy function doesn't do clone , but copy linked page, so if I change one page I change all copied pages. Bellow you can see an example of my test program.

Could you help me with this problem?

phpExcel version: 1.7.4

<?php

error_reporting(E_ALL);

date_default_timezone_set('Europe/London');

/** PHPExcel */
require_once '../Classes/PHPExcel.php';


// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");

$sheet1 = $objPHPExcel->getSheet(1)->copy();

//here template is copied
for ($i=1;$i<8;$i++){
$sheet2 = clone $sheet1;
//notice that here sheet is cloned
//it work and solved prolblem with memory leaks

$sheet2title = sheetSetTitles($sheet2,$i);
$objPHPExcel->addSheet($sheet2);


echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";
unset($sheet2);
}
$objPHPExcel->setActiveSheetIndex(1);
// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('resultbug.xlsx');

// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 ) . " KB\r\n";

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
function sheetSetTitles($sheet,$number){
$sheettitle = 'Cloned'.$number;
if ($number == 2 ){$sheettitle = 'OOOPS where link in List';}


$sheet->setTitle($sheettitle);
$sheet->setCellValue('A1',$number.'New value at cloned');
$sheet->getCell('A1')
->getHyperlink()
->setUrl("sheet://'List'!A".$number);
$sheet->insertNewRowBefore(2, $number);

$sheet->setCellValue('C1',(1/$number));
$sheet->setCellValue('A2',(1/$number));
$sheet->setCellValue('C'.$number,(1/$number));
return $sheettitle;

}

?>

Coordinator
Oct 1, 2010 at 1:38 PM

This problem is Work Item 14146, fixed in the latest SVN code

Oct 1, 2010 at 2:41 PM

Thank you , you saved my brain and my mood for holidays .