phpDocumentor OpenDocumentPHP
util_Spreadsheet
[ class tree: OpenDocumentPHP ] [ index: OpenDocumentPHP ] [ all elements ]

Source for file Worksheet.php

Documentation is available at Worksheet.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /*
  6.  * Created on 22. Jul. 2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  7.  */
  8.  
  9. /**
  10.  * Spreadsheet_OpenDocument_Worksheet class file.
  11.  * 
  12.  * Implementation of a <i>Worksheet</i> class for OpenDocuments like the <i>Worksheet</i>
  13.  * class in <i>PEAR_Spreadsheet_Writer</i> does.
  14.  * 
  15.  * PHP versions 5
  16.  *   
  17.  * LICENSE:
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software consists of voluntary contributions made by many individuals
  31.  * and is licensed under the GPL. For more information please see
  32.  * <http://opendocumentphp.org>.
  33.  * 
  34.  * $Id: Worksheet.php 260 2007-08-03 13:42:29Z nmarkgraf $
  35.  * 
  36.  * @category    File Formats
  37.  * @package     OpenDocumentPHP
  38.  * @subpackage  util_Spreadsheet
  39.  * @author      Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  40.  * @copyright   Copyright in 2006, 2007 by The OpenDocumentPHP Team
  41.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  42.  * @version     SVN: $Id: Worksheet.php 260 2007-08-03 13:42:29Z nmarkgraf $
  43.  * @link        http://opendocumentphp.org
  44.  * @link        http://pear.php.net/package/Spreadsheet_Excel_Writer
  45.  * @since       0.6.0 - 22. Jul. 2007
  46.  */
  47.  
  48. /**
  49.  * 
  50.  */
  51. require_once 'OpenDocumentPHP/OpenDocumentArchive.php';
  52.  
  53. /**
  54.  * Spreadsheet_OpenDocument_Worksheet
  55.  * 
  56.  * @category    File Formats
  57.  * @package     OpenDocumentPHP
  58.  * @subpackage  util_Spreadsheet
  59.  * @author      Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  60.  * @copyright   Copyright in 2006, 2007 by The OpenDocumentPHP Team
  61.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  62.  * @version     Release: @package_version@
  63.  * @link        http://opendocumentphp.org
  64.  * @link        http://pear.php.net/package/Spreadsheet_Excel_Writer
  65.  * @since       0.6.0 - 22. Jul. 2007
  66.  */
  67.  
  68.     /**
  69.      * The storage of the worksheet.
  70.      * 
  71.      * We simple put all the information in this multi-dimensional array.
  72.      * 
  73.      * <ul>
  74.      *  <li>$_sheet[$row][$col]['token'] contains the content.</li>
  75.      *  <li>$_sheet[$row][$col}['format'] contains (if set) some additional format informations.</li>
  76.      * </ul>
  77.      * 
  78.      * The worksheets will not be writen by this class. This work is up to the workbook class.
  79.      * 
  80.      * @var array 
  81.      * @access private
  82.      */
  83.     private $_sheet;
  84.     
  85.     /**
  86.      * first used column
  87.      * 
  88.      * @var integer 
  89.      * @access private
  90.      */
  91.     private $firstCol;
  92.  
  93.     /**
  94.      * last used column
  95.      * 
  96.      * @var integer 
  97.      * @access private
  98.      */
  99.     private $lastCol;
  100.     
  101.     /**
  102.      * Here we store the non-default colStyles
  103.      * 
  104.      * @var     array 
  105.      * @access  private
  106.      */
  107.     private $colStyles;
  108.     
  109.     /**
  110.      * @access public
  111.      * @var string Name of the worksheet
  112.      */
  113.     public  $name;
  114.     
  115.     /**
  116.      * @access public
  117.      * @var bool is this worksheet protected by a password
  118.      */
  119.     public $protected = false;
  120.     
  121.     /**
  122.      * @access public
  123.      * @var string the password, if this sheet is protected
  124.      */
  125.     public $protectionKey = null;
  126.     
  127.     /**
  128.      * @access  protected
  129.      * @var     Workbook 
  130.      */
  131.     protected $workbook;
  132.     
  133.     /**
  134.      * @param   string $name Name of the current worksheet
  135.      */
  136.     function __construct ($name$workbook=null
  137.     {
  138.         /*
  139.          * Initialize this _sheet
  140.          */
  141.         $this->_sheet array();
  142.         /*
  143.          * Store the name of this worksheet
  144.          */
  145.         $this->name = $name;
  146.         /*
  147.          * initialize first and last used column
  148.          */
  149.         $this->firstCol 999999;
  150.         $this->lastCol = -1;
  151.         /*
  152.          * initialize non-default column styles
  153.          */
  154.         $this->colStyles array();       
  155.         /*
  156.          * 
  157.          */
  158.         $this->workbook = $workbook;
  159.         /*
  160.          * $this->automaticStyles = ?????;
  161.          */
  162.     }
  163.     
  164.     /**
  165.      * Retrieve the worksheet name.
  166.      * 
  167.      * This is usefull when creating worksheets without a name.
  168.      * @return  The worksheet's name
  169.      * @access  public
  170.      * @since   0.6.0 - 23. Jul. 2007
  171.      */
  172.     function getName(
  173.     {
  174.         return $this->name;
  175.     }
  176.     
  177.     /**
  178.      * We have to keep $firstCol and $lastCol in sync, so this will
  179.      * check if it is in sync and updated it, if needed
  180.      * 
  181.      * @access  private
  182.      * @param   integer $col    a column that we use.
  183.      */
  184.     private function _updateColumns($col
  185.     {
  186.         if ($col $this->firstCol{
  187.             $this->firstCol $col;
  188.         }
  189.         if ($col $this->lastCol{
  190.             $this->lastCol $col;
  191.         }
  192.     }
  193.     
  194.     /**
  195.      * Map to the appropriate write method acording to the token recieved.
  196.      * 
  197.      * @access  public
  198.      * @param   integer $row    The row of the cell we are writing to
  199.      * @param   integer $col    The column of the cell we are writing to
  200.      * @param   mixed   $token  What we are writing
  201.      * @param   mixed   $format The optional format to apply to the cell
  202.      * @since   0.6.0 - 23. Jul. 2007
  203.      */
  204.     function write($row$col$token$format=null
  205.     {
  206.         /*
  207.          * Check if we need to update firstCol or lastCol
  208.          */    
  209.         $this->_updateColumns($col);
  210.         /*
  211.          * Check if row exists on sheet
  212.          */
  213.         $tmp $this->_sheet[$row];
  214.         if (!isset($tmp)) {
  215.             /*
  216.              * If it does not, add the row. 
  217.              */
  218.             $this->_sheet[$rowarray();
  219.         }
  220.         /*
  221.          * Check if the col exists in that row
  222.          */
  223.         $tmp $this->_sheet[$row][$col];
  224.         if (!isset($tmp)) {
  225.             /*
  226.              * If the col does not, add the col to the row
  227.              */
  228.             $this->_sheet[$row][$colarray();
  229.         }
  230.         /*
  231.          * Add token to the sheet in that row and col
  232.          */
  233.         $this->_sheet[$row][$col]['token'"$token";
  234.         /*
  235.          * Check if will need to add a format too
  236.          */
  237.         if (isset($format&& !(is_null($format))) {
  238.             /*
  239.              * If so, add the given format to the row and col
  240.              */
  241.             $this->_sheet[$row][$col]['format'$format;
  242.         }
  243.     }    
  244.     /**
  245.      * Write a hyperlink.
  246.      * 
  247.      * This is comprised of two elements: the visible label and the invisible link.
  248.      * The visible label is the same as the link unless an alternative string is specified.
  249.      * The label is written using the writeString() method. Therefore the 255 characters
  250.      * string limit applies. $string and $format are optional.
  251.      * 
  252.      * The hyperlink can be to a http, ftp, mail, internal sheet (not yet),
  253.      * or external directory url.
  254.      * 
  255.      * @access  public
  256.      * @return  integer 0 : normal termination -2 : row or column out of range -3 : long string truncated to 255 chars
  257.      * @param   integer $row    Row
  258.      * @param   integer $col    Column
  259.      * @param   string  $url    URL string
  260.      * @param   string  $string Alternative label
  261.      * @param   mixed   $format The cell format
  262.      * 
  263.      */
  264.     function writeUrl($row$col$url$string '',  $format null)
  265.     {    
  266.         /*
  267.          * Check if we need to update firstCol or lastCol
  268.          */    
  269.         $this->_updateColumns($col);    
  270.         /*
  271.          * If no $string was given, use $url as display
  272.          */
  273.         if ($string === ''{
  274.             $string $url;
  275.         }
  276.         /*
  277.          * We should do some checks here ... maybe ...
  278.          */
  279.         /*
  280.          * Add content to _sheets
  281.          */
  282.         $ret $this->write($row$col$string$format);
  283.         /*
  284.          * Mark this as hyperlink ('type' = 'url')
  285.          */
  286.         $this->_sheet[$row][$col]['type''url';
  287.         /*
  288.          * Add url to use ('url' = '$url);
  289.          */
  290.         $this->_sheet[$row][$col]['url'$url;
  291.         return $ret;            
  292.     }
  293.  
  294.     /**
  295.      * Set the worksheet protection flag to prevent accidental modification and to hide formulas if the locked and hidden format properties have been set.
  296.      * 
  297.      * @access  public
  298.      * @param   string  $password   The password to use for protecting the sheet.
  299.      * @since   0.6.0 - 23. Jul. 2007
  300.      */
  301.     function protect($password)
  302.     {
  303.         /*
  304.          * Set protected flag and password, to store it later (at close) in the
  305.          * OpenDocument archive
  306.          */
  307.         $this->protected = true;
  308.         $this->protectionKey = $password;    
  309.     }
  310.     
  311.     /**
  312.      * Set the width of a single column or a range of columns.
  313.      * 
  314.      * @param   integer $firstcol - first column on the range
  315.      * @param   integer $lastcol - last column on the range
  316.      * @param   float $width - width to set
  317.      * @param   mixed $format - The optional XF format to apply to the columns
  318.      * @param   integer $hidden - The optional hidden atribute
  319.      */
  320.     function setColumn($firstcol$lastcol$width$format=null $hidden=null
  321.     {
  322.         /*
  323.          * Check if we need to update firstCol or lastCol
  324.          */    
  325.         $this->_updateColumns($firstCol);
  326.         $this->_updateColumns($lastCol);
  327.             
  328.         /*
  329.          * Create a new automatic style with a table-col-property and set width to $width
  330.          */
  331.         $newColStyle $this->workbook->addNewAutomaticStyle();
  332.         $newColStyle->setFamily('table-column');
  333.         
  334.         $colStyleName $newColStyle->getStyleName();        
  335.         
  336.         $newTableColProp new TableColumnProperties();
  337.         $newColStyle->appendChild($newTableColProp);
  338.         
  339.         $newTableColProp->setColumnWidth($width);
  340.         
  341.         
  342.         /*
  343.          * Add all columns to this new property.
  344.          */
  345.         for($i $firstcol$i $lastcol$i++{
  346.             $this->colStyles[$i$colStyleName;
  347.             /*
  348.              * 
  349.              */
  350.         }
  351.     }
  352.     
  353.     /**
  354.      * 
  355.      */
  356.     function getCells()
  357.     {
  358.         return $this->_sheet;
  359.     }
  360.     
  361.     /**
  362.      *
  363.      * @access  public
  364.      * @since   0.6.0 - 23. Jul. 2007
  365.      */
  366.     function close()
  367.     {
  368.         
  369.     }
  370. }

Documentation generated on Wed, 18 Jun 2008 06:34:38 +0200 by phpDocumentor 1.3.2