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

Documentation generated on Tue, 01 Jun 2010 17:00:39 +0200 by phpDocumentor 1.4.3