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 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Cell_AdvancedValueBinder
  41.  *
  42.  * @category   PHPExcel
  43.  * @package    PHPExcel_Cell
  44.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46. {
  47.     /**
  48.      * Bind value to a cell
  49.      *
  50.      * @param PHPExcel_Cell $cell    Cell to bind value to
  51.      * @param mixed $value            Value to bind in cell
  52.      * @return boolean 
  53.      */
  54.     public function bindValue(PHPExcel_Cell $cell$value null)
  55.     {
  56.         // sanitize UTF-8 strings
  57.         if (is_string($value)) {
  58.             $value PHPExcel_Shared_String::SanitizeUTF8($value);
  59.         }
  60.  
  61.         // Find out data type
  62.         $dataType parent::dataTypeForValue($value);
  63.  
  64.         // Style logic - strings
  65.         if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText{
  66.             //    Test for booleans using locale-setting
  67.             if ($value == PHPExcel_Calculation::getTRUE()) {
  68.                 $cell->setValueExplicitTruePHPExcel_Cell_DataType::TYPE_BOOL);
  69.                 return true;
  70.             elseif($value == PHPExcel_Calculation::getFALSE()) {
  71.                 $cell->setValueExplicitFalsePHPExcel_Cell_DataType::TYPE_BOOL);
  72.                 return true;
  73.             }
  74.  
  75.             // Check for number in scientific format
  76.             if (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NUMBER.'$/'$value)) {
  77.                 $cell->setValueExplicit(float) $valuePHPExcel_Cell_DataType::TYPE_NUMERIC);
  78.                 return true;
  79.             }
  80.  
  81.             // Check for percentage
  82.             if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/'$value)) {
  83.                 // Convert value to number
  84.                 $cell->setValueExplicit(float)str_replace('%'''$value100PHPExcel_Cell_DataType::TYPE_NUMERIC);
  85.                 // Set style
  86.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE );
  87.                 return true;
  88.             }
  89.  
  90.             // Check for time without seconds e.g. '9:45', '09:45'
  91.             if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/'$value)) {
  92.                 list($h$mexplode(':'$value);
  93.                 $days $h 24 $m 1440;
  94.                 // Convert value to number
  95.                 $cell->setValueExplicit($daysPHPExcel_Cell_DataType::TYPE_NUMERIC);
  96.                 // Set style
  97.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
  98.                 return true;
  99.             }
  100.  
  101.             // Check for time with seconds '9:45:59', '09:45:59'
  102.             if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/'$value)) {
  103.                 list($h$m$sexplode(':'$value);
  104.                 $days $h 24 $m 1440 $s 86400;
  105.                 // Convert value to number
  106.                 $cell->setValueExplicit($daysPHPExcel_Cell_DataType::TYPE_NUMERIC);
  107.                 // Set style
  108.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCodePHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 );
  109.                 return true;
  110.             }
  111.  
  112.             // Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10'
  113.             if (($d PHPExcel_Shared_Date::stringToExcel($value)) !== false{
  114.                 // Convert value to number
  115.                 $cell->setValueExplicit($dPHPExcel_Cell_DataType::TYPE_NUMERIC);
  116.                 // Determine style. Either there is a time part or not. Look for ':'
  117.                 if (strpos($value':'!== false{
  118.                     $formatCode 'yyyy-mm-dd h:mm';
  119.                 else {
  120.                     $formatCode 'yyyy-mm-dd';
  121.                 }
  122.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getNumberFormat()->setFormatCode($formatCode);
  123.                 return true;
  124.             }
  125.  
  126.             // Check for newline character "\n"
  127.             if (strpos($value"\n"!== false{
  128.                 $value PHPExcel_Shared_String::SanitizeUTF8($value);
  129.                 $cell->setValueExplicit($valuePHPExcel_Cell_DataType::TYPE_STRING);
  130.                 // Set style
  131.                 $cell->getParent()->getStyle$cell->getCoordinate() )->getAlignment()->setWrapText(true);
  132.                 return true;
  133.             }
  134.         }
  135.  
  136.         // Not bound yet? Use parent...
  137.         return parent::bindValue($cell$value);
  138.     }
  139. }

Documentation generated on Sun, 27 Feb 2011 16:26:46 -0800 by phpDocumentor 1.4.3