crash when deleting worksheet

Topics: User Forum
Sep 20, 2013 at 3:59 PM
Edited Sep 20, 2013 at 3:59 PM
ok I am surely doing something wrong, but whenever I try to delete the empty worksheet when I am done building my workbook, it crashes my apache.
the code I am using is this:
 foreach($objPHPExcel->getSheetNames() as $index=>$name){
      if($name=='Worksheet'){
        $objPHPExcel->removeSheetByIndex($index);
      }
    }
so that should be relatively straightforward. but for some reason no.

Using latest downloadable version
Sep 23, 2013 at 11:31 AM
Hum... I tested your code and no problem, the script goes to the end without error (even if I destroy the last sheet... Here, it is Excel that really do not like).
The methodology would be a problem in the case where multiple sheets would be destroyed (the index changes).
Sep 23, 2013 at 12:06 PM
thanks, I assumed it should work, which is why I find it weird, and it is supposed to delete a single worksheet, though I modified my code and made it use worksheet for my wirst tab, before creating new ones.
I did a couple tries a any worksheet I delete crashes actually my apache,

I'll try to get full logs from apache to see what else could be at play in this
May 19, 2014 at 1:39 PM
Edited May 19, 2014 at 6:23 PM
It look to me that the problem of creating the an extra works (named "Worksheet") by default when multiple sheets are created can be avoided if you can prevent the call to "$objPHPExcel->createSheet()"; for last sheet and for simplicity I used the below check at the top;

.....
.....

$sheetIndex = 0; // Initialize sheet index 

foreach($dataSheet as $key =>$val) {

     if (0 != $sheetIndex) $objPHPExcel->createSheet();  // NOTE: The "if" condition will simply do the same if it is at the bottom; also avoid calling for last sheet
 
     $objPHPExcel->setActiveSheetIndex($sheetIndex++); // Increment $sheetIndex every time after setting the index 

     .....
     .....

     $objPHPExcel->getActiveSheet()->setTitle("Your sheetname");
     // $objPHPExcel->createSheet(); --------> This line has moved to top; <---------------
     // if the above live is here, it will always create the dummy sheet called "Worksheet" 
}
Please note that if we simply remove by comparing the name; what if the sheet is created by the user especially when creating dynamic sheets with user input.

Thanks