Undefined offset: 7

Sep 9, 2009 at 3:38 AM

Hi,

I created a script that will write a new pdf file if it exceed the record limit.. since i upgraded phpexcel i encountered this problem.. Can anyone help me thanks ;)

 

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined offset:  7</p>
<p>Filename: libraries/PHPExcel.php</p>
<p>Line Number: 555</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  __clone method called on non-object</p>
<p>Filename: PHPExcel/Style.php</p>
<p>Line Number: 504</p>

</div><br />
<b>Fatal error</b>:  Call to a member function applyFromArray() on a non-object in <b>/var/www/rex/mcs/system/application/libraries/PHPExcel/Style.php</b> on line <b>505</b><br />

Developer
Sep 9, 2009 at 3:53 AM

I'm afraid we will have to see your full script. So far I have no idea what is causing this. Can you post it?

Sep 9, 2009 at 5:16 AM
Edited Sep 9, 2009 at 12:15 PM

 Here's the code. I noticed that the errors were caused by style. I removed all style and it worked.  I cant figure out whats or where the  error is. Some style (setbold) works. but when i use it again it doesnt. I am confused and i dont have any idea how to debug it.  Please help me.... thank you

 

                                $this->excel->workbook->setActiveSheetIndex(0);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('A')->setWidth(22);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('B')->setWidth(30);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('C')->setWidth(25);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('D')->setWidth(25);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('E')->setWidth(20);
                        $this->excel->workbook->getActiveSheet()->getColumnDimension('F')->setWidth(30);

                          $this->excel->workbook->getActiveSheet()->SetCellValue('A3', 'CUST AC NO');
                            $this->excel->workbook->getActiveSheet()->SetCellValue('B3', 'MOBTEL');
                            $this->excel->workbook->getActiveSheet()->SetCellValue('C3', 'MSF');
                            $this->excel->workbook->getActiveSheet()->SetCellValue('D3', 'AUR');
                            $this->excel->workbook->getActiveSheet()->SetCellValue('E3', 'IDD');
                            $this->excel->workbook->getActiveSheet()->SetCellValue('F3', 'NDD');

 

         foreach ($query->result() as $row)
                    {

                                  if($limit_records == $recperpage)
                                    {
                                          //start new file
                                         $currfile++;
                                         $filename = $corp_id."_Report_".$filenameCtr.$fextension;
                                          $this->excel->write($filename,$rtype,$targetFolder);
                                          exec('touch '.$targetFolder.$filename);
                                         $this->zip->read_file($targetFolder.$filename);
                         
                      

                                        $this->excel->workbook->setActiveSheetIndex(0);
                                       $this->excel->workbook->getActiveSheet()->getColumnDimension('A')->setWidth(22);
                                       $this->excel->workbook->getActiveSheet()->getColumnDimension('B')->setWidth(30);
                                       $this->excel->workbook->getActiveSheet()->getColumnDimension('C')->setWidth(25);
                                       $this->excel->workbook->getActiveSheet()->getColumnDimension('D')->setWidth(25);
                                       $this->excel->workbook->getActiveSheet()->getColumnDimension('E')->setWidth(20);
                                        $this->excel->workbook->getActiveSheet()->getColumnDimension('F')->setWidth(30);

 

             $this->excel->workbook->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
                            $this->excel->workbook->getActiveSheet()->getStyle('A2')->getFont()->setSize('7');
                            $this->excel->workbook->getActiveSheet()->mergeCells('A2:Q2');

                            $this->excel->workbook->getActiveSheet()->getStyle('A3')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
                            $this->excel->workbook->getActiveSheet()->getStyle('A3')->getFont()->setSize('7');
                            $this->excel->workbook->getActiveSheet()->getStyle('A3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                            $this->excel->workbook->getActiveSheet()->getStyle('A3')->getFill()->getStartColor()->setARGB('FF54687C');
                            $this->excel->workbook->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                            $this->excel->workbook->getActiveSheet()->duplicateStyle( $this->excel->workbook->getActiveSheet()->getStyle('A3'), 'B3:Q3' );

                                       $this->excel->workbook->getActiveSheet()->SetCellValue('A3', 'CUST AC NO');
                                        $this->excel->workbook->getActiveSheet()->SetCellValue('B3', 'MOBTEL');
                                        $this->excel->workbook->getActiveSheet()->SetCellValue('C3', 'MSF');
                                         $this->excel->workbook->getActiveSheet()->SetCellValue('D3', 'AUR');
                                      $this->excel->workbook->getActiveSheet()->SetCellValue('E3', 'IDD');
                                      $this->excel->workbook->getActiveSheet()->SetCellValue('F3', 'NDD');

                             }//end if

            }//end foreach

                                         //save to pdf

                                         $filename = $corp_id."_Report_".$filenameCtr.$fextension;
                                          $this->excel->write($filename,$rtype,$targetFolder);
                                          exec('touch '.$targetFolder.$filename);
                                         $this->zip->read_file($targetFolder.$filename);

 

Developer
Sep 9, 2009 at 12:22 PM

Hmm... I cannot see anything wrong here. Not sure what how your read_file and write methods are working?

What we will need is really a script so we can reporduce the error ourselves. Can you try to narrow down the problem by removing as much code as possible until you have the smallest script that still produces the error? That will perhaps give us a pointer whether there is a bug in PHPExcel or not.

Sep 9, 2009 at 1:11 PM
Edited Sep 9, 2009 at 1:17 PM

here's the read and write script. I am using code igniter and i made a library for phpexcel.

 

class Excel
{
    var $workbook;
    var $reader;
    var $writer;

    function __construct()
    {
        set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__)) );

        require_once('PHPExcel.php');
        require_once('PHPExcel/IOFactory.php');

        $this->workbook = new PHPExcel();
    }

    function read($filename)
    {
        $this->reader = PHPExcel_IOFactory::createReader('Excel5');
        $this->workbook = $this->reader->load($filename);
    }

    function write($filename, $format = 'Excel5', $targerDir = '/tmp/')
    {
        $this->writer = PHPExcel_IOFactory::createWriter($this->workbook, $format);
        $this->writer->save($targerDir . $filename);
    }
 }

 

Before i added style, i was working on "multiple file generation". it limits the data stored in pdf file and writes the remaining data into a new so that it wont use too much memory.. The script already works perfectly without style. I didnt noticed that when i added style the "multiple file generation" script didnt work.

 

here's a simplier script:

 

    //start page

                        $this->excel->workbook->setActiveSheetIndex(0);
                        $this->excel->workbook->getActiveSheet()->SetCellValue('A1', 'MONTHLY' .$month."/".$year);
                        $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
                        $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setSize('14');
                        $this->excel->workbook->getActiveSheet()->duplicateStyle( $this->excel->workbook->getActiveSheet()->getStyle('A1'), 'B1:K1');
                         $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
                            $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setSize('9');
                            $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
                            $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF54687C');

                       $this->excel->write('filename','pdf','var/www/test/');

         /*
            //start page / file again
            // this is where the problem occurr.
            // It wont accept style..
       */


$this->excel->workbook->setActiveSheetIndex(0); $this->excel->workbook->getActiveSheet()->SetCellValue('A1', 'MONTHLY' .$month."/".$year); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setBold(true); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setSize('14'); $this->excel->workbook->getActiveSheet()->duplicateStyle( $this->excel->workbook->getActiveSheet()->getStyle('A1'), 'B1:K1'); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setSize('9'); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $this->excel->workbook->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FF54687C'); $this->excel->write('filename','pdf','var/www/test/');

is there a proper way to start a new page again?
If you have any debugging technique can you please share it. thanks

 


                      
 

Developer
Sep 9, 2009 at 2:00 PM

Hi vile, I have tried various tests, but failed to reproduce the error. I realize it will be difficult to find the error by inspecting code here in the forum.

If you can give me temporary FTP access to some test site this can probably be resolved quickly. We have done that before. Please send message to: (erik at phpexcel dot net)

Sep 10, 2009 at 3:37 AM

good day!

i found where the problem is. I made few testing script.. Since i am using the same object and im just overwriting the contents of it, I noticed that cell style cannot be overwrite.
In the first file i already added styles, these styles were also used in the 2nd file, even though i didn't add style in file 2. I'm guessing that it automatically use the cell style in file 1. This might cause some problem if the contents in file 2 is different from file 1, styles may vary in contents.. can you please verify if i am right...  thanks!!

Developer
Sep 10, 2009 at 4:12 AM

I think you may have a point although I'm not sure what the exact cause is.

Try to avoid using duplicateStyle(). Perhaps it is giving problems.

Instead of this:

$this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
$this->excel->workbook->getActiveSheet()->getStyle('A1')->getFont()->setSize('14');
$this->excel->workbook->getActiveSheet()->duplicateStyle( $this->excel->workbook->getActiveSheet()->getStyle('A1'), 'B1:K1');

Do like this:
$this->excel->workbook->getActiveSheet()->getStyle('A1:K1')->getFont()->setBold(true);
$this->excel->workbook->getActiveSheet()->getStyle('A1:K1')->getFont()->setSize('14');

That is safer, and is the recommended way to style a cell range in PHPExcel 1.7.0.

Can you test and see if the error goes away?

Sep 10, 2009 at 6:18 AM

ok i changed all duplicatestyle but still not working is there a function to reset all cell format?

Sep 10, 2009 at 7:52 AM

its all ok now. sorry for the commotion. i just made another phpexcel object. ;)

Developer
Sep 10, 2009 at 3:06 PM

Thanks to your last comments I am finally able to reproduce this. Work item created:

http://phpexcel.codeplex.com/WorkItem/View.aspx?WorkItemId=10617

I will try to see if I can get this fixed at the end of this week.