cvs: pear /Structures_DataGrid package2-ds-excel.xml /Structures_DataGrid/DataGrid/DataSource Excel.php

From: Date: Sat, 14 Oct 2006 20:13:36 +0000
Subject: cvs: pear /Structures_DataGrid package2-ds-excel.xml /Structures_DataGrid/DataGrid/DataSource Excel.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-42576@lists.php.net to get a copy of this message
wiesemann Sat Oct 14 20:13:36 2006 UTC Added files: /pear/Structures_DataGrid package2-ds-excel.xml /pear/Structures_DataGrid/DataGrid/DataSource Excel.php Log: - initial commit of new DataSource driver

http://cvs.php.net/viewvc.cgi/pear/Structures_DataGrid/package2-ds-excel.xml?view=markup&rev=1.1 Index: pear/Structures_DataGrid/package2-ds-excel.xml +++ pear/Structures_DataGrid/package2-ds-excel.xml <?xml version="1.0" encoding="UTF-8"?> <package packagerversion="1.4.9" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> <name>Structures_DataGrid_DataSource_Excel</name> <channel>pear.php.net</channel> <summary>DataSource driver using Excel spreadsheets</summary> <description> This is a DataSource driver for Structures_DataGrid using Excel spreadsheets. It requires that the package Spreadsheet_Excel_Reader is installed (not available as a PEAR package, but via Sourceforge: TODO). </description> <lead> <name>Mark Wiesemann</name> <user>wiesemann</user> <email>wiesemann@php.net</email> <active>yes</active> </lead> <date>2006-10-14</date> <time>22:15:00</time> <version> <release>0.1.0</release> <api>0.1.0</api> </version> <stability> <release>beta</release> <api>beta</api> </stability> <license uri="http://www.php.net/license">PHP License</license> <notes> Initial release. Please note the warning about the die() statement in Spreadsheet_Excel_Reader on the manual page for this driver. </notes> <contents> <dir name="/"> <dir name="DataGrid"> <dir name="DataSource"> <file baseinstalldir="Structures" name="Excel.php" role="php" /> </dir> <!-- //DataGrid/DataSource --> </dir> <!-- //DataGrid --> </dir> <!-- / --> </contents> <dependencies> <required> <php> <min>4.3</min> </php> <pearinstaller> <min>1.4.11</min> </pearinstaller> <package> <name>Structures_DataGrid</name> <channel>pear.php.net</channel> <min>0.7.1</min> </package> <package> <name>Structures_DataGrid_DataSource_Array</name> <channel>pear.php.net</channel> <min>0.1.0</min> </package> </required> </dependencies> <phprelease /> </package> http://cvs.php.net/viewvc.cgi/pear/Structures_DataGrid/DataGrid/DataSource/Excel.php?view=markup&rev=1.1 Index: pear/Structures_DataGrid/DataGrid/DataSource/Excel.php +++ pear/Structures_DataGrid/DataGrid/DataSource/Excel.php <?php /** * Excel Spreadsheet Data Source Driver * * <pre> * +----------------------------------------------------------------------+ * | PHP version 4 | * +----------------------------------------------------------------------+ * | Copyright (c) 1997-2005 The PHP Group | * +----------------------------------------------------------------------+ * | This source file is subject to version 2.0 of the PHP license, | * | that is bundled with this package in the file LICENSE, and is | * | available through the world-wide-web at | * | http://www.php.net/license/2_02.txt. | * | If you did not receive a copy of the PHP license and are unable to | * | obtain it through the world-wide-web, please send a note to | * | license@php.net so we can mail you a copy immediately. | * +----------------------------------------------------------------------+ * | Authors: Andrew Nagy <asnagy@webitecture.org> | * | Olivier Guilyardi <olivier@samalyse.com> | * | Mark Wiesemann <wiesemann@php.net> | * +----------------------------------------------------------------------+ * </pre> * * Excel file id: $Id: Excel.php,v 1.1 2006/10/14 20:13:36 wiesemann Exp $ * * @version $Revision: 1.1 $ * @package Structures_DataGrid_DataSource_Excel * @category Structures */ require_once 'Structures/DataGrid/DataSource/Array.php'; require_once 'Spreadsheet/Excel/reader.php'; /** * Excel Spreadsheet Data Source Driver * * This class is a data source driver for an Excel spreadsheet. * * SUPPORTED OPTIONS: * * - header: (bool) Whether the Excel file contains a header row * (default: false) * * GENERAL NOTES: * * This class expects the file reader.php in the directory Spreadsheet/Excel/. * * Please note that the current version (2i) of Spreadsheet_Excel_Reader contains * a die() statement in the read() method in reader.php (line 171). This makes * a reasonable PEAR error handling for the "file not found" error impossible. * * It is therefore recommended that you replace the die() statement by something * like this: * <code> * return PEAR::raiseError('The filename ' . $sFileName . ' is not readable'); * </code> * This class is optimized for the changed code (but will work also with the * die() in the reader class, of course) and also then a reasonable error * handling. * * @version $Revision: 1.1 $ * @author Mark Wiesemann <wiesemann@php.net> * @access public * @package Structures_DataGrid_DataSource_Excel * @category Structures */ class Structures_DataGrid_DataSource_Excel extends Structures_DataGrid_DataSource_Array { function Structures_DataGrid_DataSource_Excel() { parent::Structures_DataGrid_DataSource_Array(); $this->_addDefaultOptions(array('header' => false)); } /** * Bind * * @param mixed $filename Path/filename to the Excel file * @access public * @return mixed True on success, PEAR_Error on failure */ function bind($filename, $options = array()) { if ($options) { $this->setOptions($options); } $reader =& new Spreadsheet_Excel_Reader(); $result = $reader->read($filename); if (PEAR::isError($result)) { return $result; } // if the options say that there is a header row, use the contents of it // as the column names if ($this->_options['header']) { $keys = array_values($reader->sheets[0]['cells'][1]); $startRow = 2; } else { $keys = null; $startRow = 1; } // store every field (column name) that is actually used $fields = $keys; // helper variable for the case that we have a file without a header row $maxkeys = 0; for ($i = $startRow; $i <= $reader->sheets[0]['numRows']; $i++) { if (empty($keys)) { $this->_ar[] = $reader->sheets[0]['cells'][$i]; $maxkeys = max($maxkeys, $reader->sheets[0]['numCols']); } else { $rowAssoc = array(); for ($j = 1; $j <= $reader->sheets[0]['numCols']; $j++) { if (!empty($keys[$j - 1])) { $key = $keys[$j - 1]; } else { // there are more fields than we have column names // from the header of the CSV file => we need to use // the numeric index. if (!in_array($j - 1, $fields, true)) { $fields[] = $j - 1; } $key = $j - 1; } if (isset($reader->sheets[0]['cells'][$i][$j])) { $rowAssoc[$key] = $reader->sheets[0]['cells'][$i][$j]; } } $this->_ar[] = $rowAssoc; } } // set field names if they were not set as an option if (!$this->_options['fields']) { if (empty($keys)) { $this->_options['fields'] = range(0, $maxkeys); } else { $this->_options['fields'] = $fields; } } return true; } } ?>
« previous php.pear.cvs (#42576) next »