PHPExcel_Cell
[ class tree: PHPExcel_Cell ] [ index: PHPExcel_Cell ] [ all elements ]

Source for file AdvancedValueBinder.php

Documentation is available at AdvancedValueBinder.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Cell
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.7, 2009-04-22
  26.  */
  27.  
  28.  
  29. /** PHPExcel_Cell */
  30. require_once 'PHPExcel/Cell.php';
  31.  
  32. /** PHPExcel_Cell_IValueBinder */
  33. require_once 'PHPExcel/Cell/IValueBinder.php';
  34.  
  35. /** PHPExcel_Cell_DefaultValueBinder */
  36. require_once 'PHPExcel/Cell/DefaultValueBinder.php';
  37.  
  38. /** PHPExcel_Style_NumberFormat */
  39. require_once 'PHPExcel/Style/NumberFormat.php';
  40.  
  41. /** PHPExcel_Shared_Date */
  42. require_once 'PHPExcel/Shared/Date.php';
  43.  
  44.  
  45. /**
  46.  * PHPExcel_Cell_AdvancedValueBinder
  47.  *
  48.  * @category   PHPExcel
  49.  * @package    PHPExcel_Cell
  50.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  51.  */
  52. {
  53.     /**
  54.      * Bind value to a cell
  55.      *
  56.      * @param PHPExcel_Cell $cell    Cell to bind value to
  57.      * @param mixed $value            Value to bind in cell
  58.      * @return boolean 
  59.      */
  60.     public function bindValue(PHPExcel_Cell $cell$value null)
  61.     {
  62.         // Find out data type
  63.         $dataType parent::dataTypeForValue($value);
  64.         
  65.         // Style logic - strings
  66.         if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText{
  67.             // Check for percentage
  68.             if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/'$value)) {
  69.                 // Convert value to number
  70.                 $cell->setValueExplicit(float)str_replace('%'''$value100PHPExcel_Cell_DataType::TYPE_NUMERIC);
  71.                 
  72.                 // Set style
  73.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE );
  74.                 
  75.                 return true;
  76.             }
  77.             
  78.             // Check for time e.g. '9:45', '09:45'
  79.             if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/'$value)) {
  80.                 list($h$mexplode(':'$value);
  81.                 $days $h 24 $m 1440;
  82.                 
  83.                 // Convert value to number
  84.                 $cell->setValueExplicit($daysPHPExcel_Cell_DataType::TYPE_NUMERIC);
  85.                 
  86.                 // Set style
  87.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
  88.                 
  89.                 return true;
  90.             }
  91.             
  92.             // Check for date
  93.             if (strtotime($value!== false{
  94.                 // make sure we have UTC for the sake of strtotime
  95.                 $saveTimeZone date_default_timezone_get();
  96.                 date_default_timezone_set('UTC');
  97.                 
  98.                 // Convert value to Excel date
  99.                 $cell->setValueExplicitPHPExcel_Shared_Date::PHPToExcel(strtotime($value))PHPExcel_Cell_DataType::TYPE_NUMERIC);
  100.                 
  101.                 // Set style
  102.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2 );
  103.                 
  104.                 // restore original value for timezone
  105.                 date_default_timezone_set($saveTimeZone);
  106.                 
  107.                 return true;
  108.             }
  109.         }
  110.         
  111.         // Style logic - Numbers
  112.         if ($dataType === PHPExcel_Cell_DataType::TYPE_NUMERIC{
  113.             // Leading zeroes?
  114.             if (preg_match('/^\-?[0]+[0-9]*\.?[0-9]*$/'$value)) {
  115.                 // Convert value to string
  116.                 $cell->setValueExplicit$valuePHPExcel_Cell_DataType::TYPE_STRING);
  117.                 
  118.                 // Set style
  119.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_TEXT );
  120.                 
  121.                 return true;
  122.             }
  123.         }
  124.         
  125.         // Not bound yet? Use parent...
  126.         return parent::bindValue($cell$value);
  127.     }
  128. }

Documentation generated on Wed, 22 Apr 2009 08:55:08 +0200 by phpDocumentor 1.4.1