Fix for the listWorksheetInfo() method in OOCalc reader.

Topics: Developer Forum, User Forum
Jun 6, 2012 at 5:48 PM

Since my method was implemented into the new release I feel somewhat responsible for the code working correctly the first time out. 

I've been setting up a test bed for the new release and discovered that the listWorksheetInfo() method in the OOCalc reader does not return the array information expected (not sure how I missed this).

Anyway, I delved into the code and changed it so it now returns the expected values.

I'm posting the fixed method below for Mark Baker to correct the code and put it into the svn.

Apologies for any grief this has caused.

I've marked the lines that I had to change/replace so that editing should be quick and easy.


  /**
   * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
   *
   * @param   string     $pFilename
   * @throws   Exception
   */
  public function listWorksheetInfo($pFilename)
  {
    // Check if file exists
    if (!file_exists($pFilename)) {
      throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
    }

    $worksheetInfo = array();

    $zip = new ZipArchive;
    if ($zip->open($pFilename) === true) {

      $xml = simplexml_load_string($zip->getFromName("content.xml"));
      $namespacesContent = $xml->getNamespaces(true);

      $workbook = $xml->children($namespacesContent['office']);
      foreach($workbook->body->spreadsheet as $workbookData) {
        $workbookData = $workbookData->children($namespacesContent['table']);
        foreach($workbookData->table as $worksheetDataSet) {
          $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
          $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);

          $tmpInfo = array();
          $tmpInfo['worksheetName'] = (string) $worksheetDataAttributes['name'];                // Change
          $tmpInfo['lastColumnLetter'] = 'A';
          $tmpInfo['lastColumnIndex'] = 0;
          $tmpInfo['totalRows'] = 0;
          $tmpInfo['totalColumns'] = 0;

          $rowIndex = 0;
          foreach ($worksheetData as $key => $rowData) {
            $rowHasData = false;

            switch ($key) {
              case 'table-row' :
                $columnIndex = 0;

                foreach ($rowData as $key => $cellData) {                                       // change
                  $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex); // change
                  ++$columnIndex;                                                               // change
                }                                                                               // change
                                                                                                // change
                ++$rowIndex;                                                                    // change
                                                                                                // change
                $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);                  // change
                break;
            }
          }

          $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
          $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;

          $worksheetInfo[] = $tmpInfo;
        }
      }
    }

    return $worksheetInfo;
  }


 

-  Christopher Mullins

Coordinator
Jun 9, 2012 at 2:12 PM

Thanks for that fix Christopher, welcome to the feeling of open source contributor obligation :)

I've now added this to the SVN source code.