Alpesh
Alpesh

Reputation: 5405

Obtaining Blank column values included in final output array from SpreadsheetExcelReader

I am using the below plugin for reading micrsoft excel file (.xls extension)

http://php-spreadsheetreader.googlecode.com/svn-history/r26/Excel/OLERead.php

As per the current plugin implementation it returns the read excel as an array.

The below code reads the excels and returns it as an array

        include_once('spreadsheet_excel_reader.php');
        $data = new Spreadsheet_Excel_Reader_New();
        $data->setOutputEncoding('CP1251');
        $data->read('full path of file');
        echo '<pre>';print_r($data->sheets[0]['cells']);echo '</pre>';exit;

The array cells contains the number of values equal to number of rows in an excel sheet.

Each of the value itself is an array consisting of number of keys equal to number of columns in the sheet.

The array cells looks as below -

    [cells] => Array( 
                              [1] => Array
                                    (
                                        [1] => a
                                        [2] => b
                                        [3] => c
                                        [4] => d
                                        [5] => e
                                        [6] => f
                                        [7] => g
                                    )

                                [2] => Array
                                    (
                                        [1] => fddfg
                                        [2] => dfgd
                                        [5] => ghjgh 
                                        [6] => dgdf
                                        [7] => uyijkgh
                                    )

                                [3] => Array
                                    (
                                        [1] => fghfg
                                        [2] => gvsfdgdf
                                        [4] => fdg4t4  
                                        [5] => gfdg
                                        [6] => dfgd
                                        [7] => ghfghf
                                    )

                                [4] => Array
                                    (
                                        [1] => fgh
                                        [2] => sfsdf
                                        [5] => fghfgh
                                        [6] => fsdf
                                        [7] => dfgdfg
                                    ) 
)

As seen from the above array the key 1 contains array of headings of the sheet. So in all there are 7 columns in an excel sheet. Currently if any of the cell contains no value in sheet then it skips those values in an array but my requirement is that if a cell contains no values in that case the array should be containing blank values.

In plugin this whole array creation process takes place in below function

function _parsesheet($spos)

I tries adding a new case for handling null entries but with no success.

Any help on the above issue would be great.

Upvotes: 0

Views: 1484

Answers (2)

Mark Baker
Mark Baker

Reputation: 212522

$max = 0;
array_walk($data->sheets[0]['cells'],function($row) use (&$max) { $max = max(max(array_keys($row)),$max); });
array_walk($data->sheets[0]['cells'],function(&$row) use ($max) { $row = $row + array_fill_keys(range(1,$max),NULL); ksort($row); });

var_dump($data->sheets[0]['cells']);

Pads every missing column with a NULL cell Doesn't do anything about missing rows. PHP >= 5.3.0

Upvotes: 2

The Silencer
The Silencer

Reputation: 787

How should it handle all the null values after your content? You could iterate through the array and pad all the missing rows.

Upvotes: 0

Related Questions