problem with several worksheets, labels and images

Topics: User Forum
Jun 20, 2011 at 7:42 PM
Edited Jun 20, 2011 at 7:45 PM

Hi,

I have a problem with PHPExcel

I'm creating several sheets in a workbook using a template.
When I insert a sheet, I assign a new label for this new sheet.  The problem is when the second sheet is inserted, the name of the previous sheet is changed with the name of the sheet in the template, then, when the third sheet is inserted occur an error, and I think is because the name of the second sheet is changed with the name of the template's sheet and the worksheet don't allow to have two sheets with the same name.

The problem is that I assign diferent names for each sheet, and I don't know why every time a insert a new sheet, the names of the previous sheet are changing.

The same occurs when I insert an image in every sheet I'm creating.   When a new sheet is created, the images in the previous sheets, disapear.

To solve this, I had to rename every sheet after a new sheet was created, and when all sheets was created, I had to insert my image in every sheet in the document.

This is what I did.

$names = array();

for($count=0; $count<5; $count++){       
            $Template   = $objReader->load("template.xlsx");
            $workSheet    = $Template->getSheet(0);
            $objPHPExcel->addExternalSheet($workSheet,0);
            $objPHPExcel->setActiveSheetIndex($count);
            $noms[$count]="sheetName_".$count;
            $objPHPExcel->getActiveSheet()->setTitle($names[$count]);                               

            // If I don't rename the label of the previous sheets, an error occurs because the previous labels are missing and the label of the original template is showed.

             if($count>0){
                for($i=0; $i<$count; $i++){
                    $label=trim($names[$hoj]);
                    $objPHPExcel->setActiveSheetIndex($i);
                    $objPHPExcel->getActiveSheet()->setTitle($label);           
                }   
            }       
}

// Finaly, I tried to insert an image in the moment that a new sheet was inserted, but at the end, only the last sheet had the image.

// Then I had to insert the image in every sheet when every sheets was created.

        for($i=0;$i<11;$i++){           
            $objDrawing = new PHPExcel_Worksheet_Drawing();
            $objDrawing->setPath('logo.jpg');
            $objDrawing->setCoordinates('B1');
            $objDrawing->setName('logo');
            $objDrawing->setDescription('logo');
            $objDrawing->setHeight(65);
            $objDrawing->setOffsetX(45);
            $objDrawing->setOffsetY(4);
            $objDrawing->setRotation(0);       
            $objPHPExcel->setActiveSheetIndex($i);
            $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
        }      

 

Could anybody help me to find out if I am doing something wrong?, and what could be the right way to do this?.

Thanks in advance.

 

Regards.

Alex.

Jun 22, 2011 at 11:13 PM

Hi,

I finaly found what the problem was.

In the next line, I must put the index number of the sheet, insted the number zero.

$objPHPExcel->addExternalSheet($workSheet,0);

Correct:

$objPHPExcel->addExternalSheet($workSheet,$count);

 

Thanks anyway.

Regards.

Añex.