nuovo / spreadsheet-reader

A PHP spreadsheet reader (Excel XLS and XLSX, OpenOffice ODS, and variously separated text files) with a singular goal of getting the data out, efficiently
http://www.nuovo.lv/
Other
674 stars 498 forks source link

ChangeSheet doesn't work #72

Closed mn1aC closed 9 years ago

mn1aC commented 9 years ago

I have the following code from the example:

foreach ($Sheets as $Index => $Name) { echo 'Sheet #'.$Index.': '.$Name; $Reader->ChangeSheet($Index); echo "
change sheet"; foreach ($Reader as $rowData) { print_r($rowData); } }

output: Sheet #0: cash Sheet #1 : others

put the printout of (print_r) gives twice the rows of Sheet #1:others.... ?? Weird??

jtresponse commented 9 years ago

There is an issue with the ChangeSheet and rewind methods. There is a fix by janardhanpasumarthi in issue 66, but I don't think it is correct.

I have fixed by removing the check for Index > 0 and by setting CurrentRow to be false to force a re-read of the sheet data. I'm not sure if removing the check will cause any side-effects, as it is not obvious what it is for, and there are no comments. My version is below:

    public function rewind()
    {

        // removed this test as it should rewind when Index == 0 after a sheet change
                    //if ($this -> Index > 0 || !($this -> Worksheet instanceof XMLReader))
        {
            // If the worksheet was already iterated, XML file is reopened.
            // Otherwise it should be at the beginning anyway
            if ($this -> Worksheet instanceof XMLReader)
            {
                $this -> Worksheet -> close();
            }
            else
            {
                $this -> Worksheet = new XMLReader;
            }

            $this -> Worksheet -> open($this -> WorksheetPath);
            $this -> Valid = true;

            $this -> RowOpen = false;
        }

        $this -> Index = 0;
                    $this -> CurrentRow = false; //ADDED  to force data reload after sheet change
    }
pilsetnieks commented 9 years ago

Thanks, this has been incorporated in v.0.5.8. now.

sergiomeneses commented 9 years ago

Hi this still happens in v.0.5.10, i need this working, any new about it?

sergiomeneses commented 9 years ago

Hi this is fixed by apply the update in the following issue: https://github.com/nuovo/spreadsheet-reader/issues/59#issuecomment-40710172