Error in Worksheet.php

Topics: Developer Forum, User Forum
Oct 23, 2013 at 10:57 PM
Edited Oct 24, 2013 at 3:25 PM
I am receiving a weird error in Worksheet.php, line 1353. It is saying that $this->_parent is "<Uninitialized>" so it can't call setActiveSheetIndex().
foreach($checklists as $key => $checklist) {
    $excel->getActiveSheet()->setTitle(substr($checklist['checklist']->get_Name(), 0, 30));

        create_Header($excel, 'A2', 'Text', 'A2:B2');
        ....

    $new_sheet = new PHPExcel_Worksheet();
    $excel->addSheet($new_sheet);

    $excel->setActiveSheetIndex($excel->getActiveSheetIndex()+1);
}

function create_Header($excel, $cell, $txt, $merged = '') {
    $excel->getActiveSheet()
        ->setCellValue($cell, $txt);

    if($merged != '') { $excel->getActiveSheet()->mergeCells($merged); }

    $excel->getActiveSheet()
        ->getStyle($cell)
        ->getFont()
        ->applyFromArray($GLOBALS['styles']['header']);
    $excel->getActiveSheet()
        ->getStyle($cell)
        ->getAlignment()
        ->applyFromArray($GLOBALS['styles']['right']);
    $excel->getActiveSheet()
        ->getStyle($cell)
        ->getFill()
        ->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
    $excel->getActiveSheet()
        ->getStyle($cell)
        ->getFill()
        ->setStartColor($GLOBALS['light_brown']);
}
The error only occurs on the second iteration through the loop. The first iteration go through without a hitch. Any thoughts? Thanks!

EDIT: I forgot to mention that the get the error on the first line after the if block in create_Header(). So even on the second iteration it is able to get through the first couple lines with no problem.

EDIT 2: I'm using XAMPP 3.2.1 (which comes with PHP 5.5.3)
Oct 25, 2013 at 7:50 AM
You don't define the parent when you created a new worksheet.
Either you do: $new_sheet = new PHPExcel_Worksheet ($excel); then the addSheet() or you use createSheet which does both.

The one and the other returns the worksheet object that I would spend to create_header rather than 'play' with the active sheet