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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel_Cell */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/Cell.php';
  39.  
  40. /** PHPExcel_Cell_IValueBinder */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Cell/IValueBinder.php';
  42.  
  43. /** PHPExcel_Cell_DefaultValueBinder */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Cell/DefaultValueBinder.php';
  45.  
  46. /** PHPExcel_Style_NumberFormat */
  47. require_once PHPEXCEL_ROOT 'PHPExcel/Style/NumberFormat.php';
  48.  
  49. /** PHPExcel_Shared_Date */
  50. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Date.php';
  51.  
  52. /** PHPExcel_Shared_String */
  53. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/String.php';
  54.  
  55.  
  56. /**
  57.  * PHPExcel_Cell_AdvancedValueBinder
  58.  *
  59.  * @category   PHPExcel
  60.  * @package    PHPExcel_Cell
  61.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  62.  */
  63. {
  64.     /**
  65.      * Bind value to a cell
  66.      *
  67.      * @param PHPExcel_Cell $cell    Cell to bind value to
  68.      * @param mixed $value            Value to bind in cell
  69.      * @return boolean 
  70.      */
  71.     public function bindValue(PHPExcel_Cell $cell$value null)
  72.     {
  73.         // sanitize UTF-8 strings
  74.         if (is_string($value)) {
  75.             $value PHPExcel_Shared_String::SanitizeUTF8($value);
  76.         }
  77.  
  78.         // Find out data type
  79.         $dataType parent::dataTypeForValue($value);
  80.         
  81.         // Style logic - strings
  82.         if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText{
  83.             // Check for percentage
  84.             if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/'$value)) {
  85.                 // Convert value to number
  86.                 $cell->setValueExplicit(float)str_replace('%'''$value100PHPExcel_Cell_DataType::TYPE_NUMERIC);
  87.                 
  88.                 // Set style
  89.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE );
  90.                 
  91.                 return true;
  92.             }
  93.             
  94.             // Check for time e.g. '9:45', '09:45'
  95.             if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/'$value)) {
  96.                 list($h$mexplode(':'$value);
  97.                 $days $h 24 $m 1440;
  98.                 
  99.                 // Convert value to number
  100.                 $cell->setValueExplicit($daysPHPExcel_Cell_DataType::TYPE_NUMERIC);
  101.                 
  102.                 // Set style
  103.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
  104.                 
  105.                 return true;
  106.             }
  107.             
  108.             // Check for date
  109.             if (strtotime($value!== false{
  110.                 // make sure we have UTC for the sake of strtotime
  111.                 $saveTimeZone date_default_timezone_get();
  112.                 date_default_timezone_set('UTC');
  113.                 
  114.                 // Convert value to Excel date
  115.                 $cell->setValueExplicitPHPExcel_Shared_Date::PHPToExcel(strtotime($value))PHPExcel_Cell_DataType::TYPE_NUMERIC);
  116.                 
  117.                 // Set style
  118.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2 );
  119.                 
  120.                 // restore original value for timezone
  121.                 date_default_timezone_set($saveTimeZone);
  122.                 
  123.                 return true;
  124.             }
  125.         }
  126.  
  127.         // Not bound yet? Use parent...
  128.         return parent::bindValue($cell$value);
  129.     }
  130. }

Documentation generated on Mon, 11 Jan 2010 08:03:38 +0100 by phpDocumentor 1.4.1