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

Source for file NumberFormat.php

Documentation is available at NumberFormat.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_Style
  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_IComparable */
  38. require_once PHPEXCEL_ROOT 'PHPExcel/IComparable.php';
  39.  
  40. /** PHPExcel_Shared_Date */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/Date.php';
  42.  
  43. /** PHPExcel_Calculation_Functions */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Calculation/Functions.php';
  45.  
  46.  
  47. /**
  48.  * PHPExcel_Style_NumberFormat
  49.  *
  50.  * @category   PHPExcel
  51.  * @package    PHPExcel_Style
  52.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  53.  */
  54. class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
  55. {
  56.     /* Pre-defined formats */
  57.     const FORMAT_GENERAL                    'General';
  58.  
  59.     const FORMAT_TEXT                        '@';
  60.  
  61.     const FORMAT_NUMBER                        '0';
  62.     const FORMAT_NUMBER_00                    '0.00';
  63.     const FORMAT_NUMBER_COMMA_SEPARATED1    '#,##0.00';
  64.     const FORMAT_NUMBER_COMMA_SEPARATED2    '#,##0.00_-';
  65.  
  66.     const FORMAT_PERCENTAGE                    '0%';
  67.     const FORMAT_PERCENTAGE_00                '0.00%';
  68.  
  69.     const FORMAT_DATE_YYYYMMDD2                'yyyy-mm-dd';
  70.     const FORMAT_DATE_YYYYMMDD                'yy-mm-dd';
  71.     const FORMAT_DATE_DDMMYYYY                'dd/mm/yy';
  72.     const FORMAT_DATE_DMYSLASH                'd/m/y';
  73.     const FORMAT_DATE_DMYMINUS                'd-m-y';
  74.     const FORMAT_DATE_DMMINUS                'd-m';
  75.     const FORMAT_DATE_MYMINUS                'm-y';
  76.     const FORMAT_DATE_XLSX14                'mm-dd-yy';
  77.     const FORMAT_DATE_XLSX15                'd-mmm-yy';
  78.     const FORMAT_DATE_XLSX16                'd-mmm';
  79.     const FORMAT_DATE_XLSX17                'mmm-yy';
  80.     const FORMAT_DATE_XLSX22                'm/d/yy h:mm';
  81.     const FORMAT_DATE_DATETIME                'd/m/y h:mm';
  82.     const FORMAT_DATE_TIME1                    'h:mm AM/PM';
  83.     const FORMAT_DATE_TIME2                    'h:mm:ss AM/PM';
  84.     const FORMAT_DATE_TIME3                    'h:mm';
  85.     const FORMAT_DATE_TIME4                    'h:mm:ss';
  86.     const FORMAT_DATE_TIME5                    'mm:ss';
  87.     const FORMAT_DATE_TIME6                    'h:mm:ss';
  88.     const FORMAT_DATE_TIME7                    'i:s.S';
  89.     const FORMAT_DATE_TIME8                    'h:mm:ss;@';
  90.     const FORMAT_DATE_YYYYMMDDSLASH            'yy/mm/dd;@';
  91.  
  92.     const FORMAT_CURRENCY_USD_SIMPLE        '"$"#,##0.00_-';
  93.     const FORMAT_CURRENCY_USD                '$#,##0_-';
  94.     const FORMAT_CURRENCY_EUR_SIMPLE        '[$EUR ]#,##0.00_-';
  95.  
  96.     /**
  97.      * Excel built-in number formats
  98.      *
  99.      * @var array 
  100.      */
  101.     private static $_builtInFormats;
  102.  
  103.     /**
  104.      * Excel built-in number formats (flipped, for faster lookups)
  105.      *
  106.      * @var array 
  107.      */
  108.     private static $_flippedBuiltInFormats;
  109.  
  110.     /**
  111.      * Format Code
  112.      *
  113.      * @var string 
  114.      */
  115.     private $_formatCode;
  116.  
  117.     /**
  118.      * Built-in format Code
  119.      *
  120.      * @var string 
  121.      */
  122.     private $_builtInFormatCode;
  123.  
  124.     /**
  125.      * Parent Borders
  126.      *
  127.      * @var _parentPropertyName string
  128.      */
  129.     private $_parentPropertyName;
  130.  
  131.     /**
  132.      * Supervisor?
  133.      *
  134.      * @var boolean 
  135.      */
  136.     private $_isSupervisor;
  137.  
  138.     /**
  139.      * Parent. Only used for supervisor
  140.      *
  141.      * @var PHPExcel_Style 
  142.      */
  143.     private $_parent;
  144.  
  145.     /**
  146.      * Create a new PHPExcel_Style_NumberFormat
  147.      */
  148.     public function __construct($isSupervisor false)
  149.     {
  150.         // Supervisor?
  151.         $this->_isSupervisor = $isSupervisor;
  152.  
  153.         // Initialise values
  154.         $this->_formatCode            = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  155.         $this->_builtInFormatCode    = 0;
  156.     }
  157.  
  158.     /**
  159.      * Bind parent. Only used for supervisor
  160.      *
  161.      * @param PHPExcel_Style $parent 
  162.      * @return PHPExcel_Style_NumberFormat 
  163.      */
  164.     public function bindParent($parent)
  165.     {
  166.         $this->_parent = $parent;
  167.     }
  168.  
  169.     /**
  170.      * Is this a supervisor or a real style component?
  171.      *
  172.      * @return boolean 
  173.      */
  174.     public function getIsSupervisor()
  175.     {
  176.         return $this->_isSupervisor;
  177.     }
  178.  
  179.     /**
  180.      * Get the shared style component for the currently active cell in currently active sheet.
  181.      * Only used for style supervisor
  182.      *
  183.      * @return PHPExcel_Style_NumberFormat 
  184.      */
  185.     public function getSharedComponent()
  186.     {
  187.         return $this->_parent->getSharedComponent()->getNumberFormat();
  188.     }
  189.  
  190.     /**
  191.      * Get the currently active sheet. Only used for supervisor
  192.      *
  193.      * @return PHPExcel_Worksheet 
  194.      */
  195.     public function getActiveSheet()
  196.     {
  197.         return $this->_parent->getActiveSheet();
  198.     }
  199.  
  200.     /**
  201.      * Get the currently active cell coordinate in currently active sheet.
  202.      * Only used for supervisor
  203.      *
  204.      * @return string E.g. 'A1'
  205.      */
  206.     public function getSelectedCells()
  207.     {
  208.         return $this->getActiveSheet()->getSelectedCells();
  209.     }
  210.  
  211.     /**
  212.      * Get the currently active cell coordinate in currently active sheet.
  213.      * Only used for supervisor
  214.      *
  215.      * @return string E.g. 'A1'
  216.      */
  217.     public function getActiveCell()
  218.     {
  219.         return $this->getActiveSheet()->getActiveCell();
  220.     }
  221.  
  222.     /**
  223.      * Build style array from subcomponents
  224.      *
  225.      * @param array $array 
  226.      * @return array 
  227.      */
  228.     public function getStyleArray($array)
  229.     {
  230.         return array('numberformat' => $array);
  231.     }
  232.  
  233.     /**
  234.      * Apply styles from array
  235.      *
  236.      * <code>
  237.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
  238.      *         array(
  239.      *             'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
  240.      *         )
  241.      * );
  242.      * </code>
  243.      *
  244.      * @param    array    $pStyles    Array containing style information
  245.      * @throws    Exception
  246.      * @return PHPExcel_Style_NumberFormat 
  247.      */
  248.     public function applyFromArray($pStyles null{
  249.         if (is_array($pStyles)) {
  250.             if ($this->_isSupervisor{
  251.                 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  252.             else {
  253.                 if (array_key_exists('code'$pStyles)) {
  254.                     $this->setFormatCode($pStyles['code']);
  255.                 }
  256.             }
  257.         else {
  258.             throw new Exception("Invalid style array passed.");
  259.         }
  260.         return $this;
  261.     }
  262.  
  263.     /**
  264.      * Get Format Code
  265.      *
  266.      * @return string 
  267.      */
  268.     public function getFormatCode({
  269.         if ($this->_isSupervisor{
  270.             return $this->getSharedComponent()->getFormatCode();
  271.         }
  272.         if ($this->_builtInFormatCode !== false)
  273.         {
  274.             return self::builtInFormatCode($this->_builtInFormatCode);
  275.         }
  276.         return $this->_formatCode;
  277.     }
  278.  
  279.     /**
  280.      * Set Format Code
  281.      *
  282.      * @param string $pValue 
  283.      * @return PHPExcel_Style_NumberFormat 
  284.      */
  285.     public function setFormatCode($pValue PHPExcel_Style_NumberFormat::FORMAT_GENERAL{
  286.         if ($pValue == ''{
  287.             $pValue PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
  288.         }
  289.         if ($this->_isSupervisor{
  290.             $styleArray $this->getStyleArray(array('code' => $pValue));
  291.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  292.         else {
  293.             $this->_formatCode = $pValue;
  294.             $this->_builtInFormatCode = self::builtInFormatCodeIndex($pValue);
  295.         }
  296.         return $this;
  297.     }
  298.  
  299.     /**
  300.      * Get Built-In Format Code
  301.      *
  302.      * @return int 
  303.      */
  304.     public function getBuiltInFormatCode({
  305.         if ($this->_isSupervisor{
  306.             return $this->getSharedComponent()->getBuiltInFormatCode();
  307.         }
  308.         return $this->_builtInFormatCode;
  309.     }
  310.  
  311.     /**
  312.      * Set Built-In Format Code
  313.      *
  314.      * @param int $pValue 
  315.      * @return PHPExcel_Style_NumberFormat 
  316.      */
  317.     public function setBuiltInFormatCode($pValue 0{
  318.  
  319.         if ($this->_isSupervisor{
  320.             $styleArray $this->getStyleArray(array('code' => self::builtInFormatCode($pValue)));
  321.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  322.         else {
  323.             $this->_builtInFormatCode = $pValue;
  324.             $this->_formatCode = self::builtInFormatCode($pValue);
  325.         }
  326.         return $this;
  327.     }
  328.  
  329.     /**
  330.      * Fill built-in format codes
  331.      */
  332.     private static function fillBuiltInFormatCodes()
  333.     {
  334.         // Built-in format codes
  335.         if (is_null(self::$_builtInFormats)) {
  336.             self::$_builtInFormats array();
  337.  
  338.             // General
  339.             self::$_builtInFormats[0'General';
  340.             self::$_builtInFormats[1'0';
  341.             self::$_builtInFormats[2'0.00';
  342.             self::$_builtInFormats[3'#,##0';
  343.             self::$_builtInFormats[4'#,##0.00';
  344.  
  345.             self::$_builtInFormats[9'0%';
  346.             self::$_builtInFormats[10'0.00%';
  347.             self::$_builtInFormats[11'0.00E+00';
  348.             self::$_builtInFormats[12'# ?/?';
  349.             self::$_builtInFormats[13'# ??/??';
  350.             self::$_builtInFormats[14'mm-dd-yy';
  351.             self::$_builtInFormats[15'd-mmm-yy';
  352.             self::$_builtInFormats[16'd-mmm';
  353.             self::$_builtInFormats[17'mmm-yy';
  354.             self::$_builtInFormats[18'h:mm AM/PM';
  355.             self::$_builtInFormats[19'h:mm:ss AM/PM';
  356.             self::$_builtInFormats[20'h:mm';
  357.             self::$_builtInFormats[21'h:mm:ss';
  358.             self::$_builtInFormats[22'm/d/yy h:mm';
  359.  
  360.             self::$_builtInFormats[37'#,##0 ;(#,##0)';
  361.             self::$_builtInFormats[38'#,##0 ;[Red](#,##0)';
  362.             self::$_builtInFormats[39'#,##0.00;(#,##0.00)';
  363.             self::$_builtInFormats[40'#,##0.00;[Red](#,##0.00)';
  364.  
  365.             self::$_builtInFormats[44'_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
  366.             self::$_builtInFormats[45'mm:ss';
  367.             self::$_builtInFormats[46'[h]:mm:ss';
  368.             self::$_builtInFormats[47'mmss.0';
  369.             self::$_builtInFormats[48'##0.0E+0';
  370.             self::$_builtInFormats[49'@';
  371.  
  372.             // CHT
  373.             self::$_builtInFormats[27'[$-404]e/m/d';
  374.             self::$_builtInFormats[30'm/d/yy';
  375.             self::$_builtInFormats[36'[$-404]e/m/d';
  376.             self::$_builtInFormats[50'[$-404]e/m/d';
  377.             self::$_builtInFormats[57'[$-404]e/m/d';
  378.  
  379.             // THA
  380.             self::$_builtInFormats[59't0';
  381.             self::$_builtInFormats[60't0.00';
  382.             self::$_builtInFormats[61't#,##0';
  383.             self::$_builtInFormats[62't#,##0.00';
  384.             self::$_builtInFormats[67't0%';
  385.             self::$_builtInFormats[68't0.00%';
  386.             self::$_builtInFormats[69't# ?/?';
  387.             self::$_builtInFormats[70't# ??/??';
  388.  
  389.             // Flip array (for faster lookups)
  390.             self::$_flippedBuiltInFormats array_flip(self::$_builtInFormats);
  391.         }
  392.     }
  393.  
  394.     /**
  395.      * Get built-in format code
  396.      *
  397.      * @param    int        $pIndex 
  398.      * @return    string 
  399.      */
  400.     public static function builtInFormatCode($pIndex{
  401.         // Clean parameter
  402.         $pIndex intval($pIndex);
  403.  
  404.         // Ensure built-in format codes are available
  405.         self::fillBuiltInFormatCodes();
  406.  
  407.         // Lookup format code
  408.         if (array_key_exists($pIndexself::$_builtInFormats)) {
  409.             return self::$_builtInFormats[$pIndex];
  410.         }
  411.  
  412.         return '';
  413.     }
  414.  
  415.     /**
  416.      * Get built-in format code index
  417.      *
  418.      * @param    string        $formatCode 
  419.      * @return    int|boolean
  420.      */
  421.     public static function builtInFormatCodeIndex($formatCode{
  422.         // Ensure built-in format codes are available
  423.         self::fillBuiltInFormatCodes();
  424.  
  425.         // Lookup format code
  426.         if (array_key_exists($formatCodeself::$_flippedBuiltInFormats)) {
  427.             return self::$_flippedBuiltInFormats[$formatCode];
  428.         }
  429.  
  430.         return false;
  431.     }
  432.  
  433.     /**
  434.      * Get hash code
  435.      *
  436.      * @return string    Hash code
  437.      */
  438.     public function getHashCode({
  439.         if ($this->_isSupervisor{
  440.             return $this->getSharedComponent()->getHashCode();
  441.         }
  442.         return md5(
  443.               $this->_formatCode
  444.             . $this->_builtInFormatCode
  445.             . __CLASS__
  446.         );
  447.     }
  448.  
  449.     /**
  450.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  451.      */
  452.     public function __clone({
  453.         $vars get_object_vars($this);
  454.         foreach ($vars as $key => $value{
  455.             if (is_object($value)) {
  456.                 $this->$key clone $value;
  457.             else {
  458.                 $this->$key $value;
  459.             }
  460.         }
  461.     }
  462.  
  463.     private static $_dateFormatReplacements array(
  464.             // first remove escapes related to non-format characters
  465.             '\\'    => '',
  466.             //    12-hour suffix
  467.             'am/pm'    => 'A',
  468.             //    4-digit year
  469.             'yyyy'    => 'Y',
  470.             //    2-digit year
  471.             'yy'    => 'y',
  472.             //    first letter of month - no php equivalent
  473.             'mmmmm'    => 'M',
  474.             //    full month name
  475.             'mmmm'    => 'F',
  476.             //    short month name
  477.             'mmm'    => 'M',
  478.             //    mm is minutes if time or month w/leading zero
  479.             ':mm'    => ':i',
  480.             //    month leading zero
  481.             'mm'    => 'm',
  482.             //    month no leading zero
  483.             'm'        => 'n',
  484.             //    full day of week name
  485.             'dddd'    => 'l',
  486.             //    short day of week name
  487.             'ddd'    => 'D',
  488.             //    days leading zero
  489.             'dd'    => 'd',
  490.             //    days no leading zero
  491.             'd'        => 'j',
  492.             //    seconds
  493.             'ss'    => 's',
  494.             //    fractional seconds - no php equivalent
  495.             '.s'    => ''
  496.         );
  497.     private static $_dateFormatReplacements24 array(
  498.             'hh'    => 'H',
  499.             'h'        => 'G'
  500.         );
  501.     private static $_dateFormatReplacements12 array(
  502.             'hh'    => 'h',
  503.             'h'        => 'g'
  504.         );
  505.  
  506.     /**
  507.      * Convert a value in a pre-defined format to a PHP string
  508.      *
  509.      * @param mixed     $value        Value to format
  510.      * @param string     $format        Format code
  511.      * @param array        $callBack    Callback function for additional formatting of string
  512.      * @return string    Formatted string
  513.      */
  514.     public static function toFormattedString($value ''$format ''$callBack null{
  515.         // For now we do not treat strings although section 4 of a format code affects strings
  516.         if (!is_numeric($value)) return $value;
  517.  
  518.         // For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
  519.         // it seems to round numbers to a total of 10 digits.
  520.         if ($format === 'General'{
  521.             return $value;
  522.         }
  523.  
  524.         // Get the sections, there can be up to four sections
  525.         $sections explode(';'$format);
  526.  
  527.         // Fetch the relevant section depending on whether number is positive, negative, or zero?
  528.         // Text not supported yet.
  529.         // Here is how the sections apply to various values in Excel:
  530.         //   1 section:   [POSITIVE/NEGATIVE/ZERO/TEXT]
  531.         //   2 sections:  [POSITIVE/ZERO/TEXT] [NEGATIVE]
  532.         //   3 sections:  [POSITIVE/TEXT] [NEGATIVE] [ZERO]
  533.         //   4 sections:  [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
  534.         switch (count($sections)) {
  535.             case 1:
  536.                 $format $sections[0];
  537.                 break;
  538.  
  539.             case 2:
  540.                 $format ($value >= 0$sections[0$sections[1];
  541.                 $value abs($value)// Use the absolute value
  542.                 break;
  543.  
  544.             case 3:
  545.                 $format ($value 0?
  546.                     $sections[0( ($value 0?
  547.                         $sections[1$sections[2]);
  548.                 $value abs($value)// Use the absolute value
  549.                 break;
  550.  
  551.             case 4:
  552.                 $format ($value 0?
  553.                     $sections[0( ($value 0?
  554.                         $sections[1$sections[2]);
  555.                 $value abs($value)// Use the absolute value
  556.                 break;
  557.  
  558.             default:
  559.                 // something is wrong, just use first section
  560.                 $format $sections[0];
  561.                 break;
  562.         }
  563.  
  564.         // Save format with color information for later use below
  565.         $formatColor $format;
  566.  
  567.         // Strip color information
  568.         $color_regex '/^\\[[a-zA-Z]+\\]/';
  569.         $format preg_replace($color_regex''$format);
  570.  
  571.         // Let's begin inspecting the format and converting the value to a formatted string
  572.         if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i'$format)) // datetime format
  573.             // dvc: convert Excel formats to PHP date formats
  574.  
  575.             // strip off first part containing e.g. [$-F800] or [$USD-409]
  576.             // general syntax: [$<Currency string>-<language info>]
  577.             // language info is in hexadecimal
  578.             $format preg_replace('/^(\[\$[A-Z]*-[0-9A-F]*\])/i'''$format);
  579.  
  580.             // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case
  581.             $format strtolower($format);
  582.  
  583.             $format strtr($format,self::$_dateFormatReplacements);
  584.             if (!strpos($format,'A')) {    // 24-hour time format
  585.                 $format strtr($format,self::$_dateFormatReplacements24);
  586.             else {                    // 12-hour time format
  587.                 $format strtr($format,self::$_dateFormatReplacements12);
  588.             }
  589.  
  590.             $value gmdate($formatPHPExcel_Shared_Date::ExcelToPHP($value));
  591.  
  592.         else if (preg_match('/%$/'$format)) // % number format
  593.             if ($format === self::FORMAT_PERCENTAGE{
  594.                 $value round( (100 $value)0'%';
  595.             else {
  596.                 if (preg_match('/\.[#0]+/i'$format$m)) {
  597.                     $s substr($m[0]01(strlen($m[0]1);
  598.                     $format str_replace($m[0]$s$format);
  599.                 }
  600.                 if (preg_match('/^[#0]+/'$format$m)) {
  601.                     $format str_replace($m[0]strlen($m[0])$format);
  602.                 }
  603.                 $format '%' str_replace('%''f%%'$format);
  604.  
  605.                 $value sprintf($format100 $value);
  606.             }
  607.  
  608.         else {
  609.             if (preg_match ("/^([0-9.,-]+)$/"$value)) {
  610.                  if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE{
  611.                      $value 'EUR ' sprintf('%1.2f'$value);
  612.  
  613.                 else {
  614.                     // In Excel formats, "_" is used to add spacing, which we can't do in HTML
  615.                     $format preg_replace('/_./'''$format);
  616.  
  617.                     // Some non-number characters are escaped with \, which we don't need
  618.                     $format preg_replace("/\\\\/"''$format);
  619.  
  620.                     // Some non-number strings are quoted, so we'll get rid of the quotes
  621.                     $format preg_replace('/"/'''$format);
  622.  
  623.                     // TEMPORARY - Convert # to 0
  624.                     $format preg_replace('/\\#/''0'$format);
  625.  
  626.                     // Find out if we need thousands separator
  627.                     $useThousands preg_match('/,/'$format);
  628.                     if ($useThousands{
  629.                         $format preg_replace('/,/'''$format);
  630.                     }
  631.  
  632.                     if (preg_match('/0?.*\?\/\?/'$format$m)) {
  633.                         //echo 'Format mask is fractional '.$format.' <br />';
  634.                         $sign ($value 0'-' '';
  635.  
  636.                         $integerPart floor(abs($value));
  637.                         $decimalPart trim(fmod(abs($value),1),'0.');
  638.                         $decimalLength strlen($decimalPart);
  639.                         $decimalDivisor pow(10,$decimalLength);
  640.  
  641.                         $GCD PHPExcel_Calculation_Functions::GCD($decimalPart,$decimalDivisor);
  642.  
  643.                         $adjustedDecimalPart $decimalPart/$GCD;
  644.                         $adjustedDecimalDivisor $decimalDivisor/$GCD;
  645.  
  646.                         if ((strpos($format,'0'!== false|| (substr($format,0,3== '? ?')) {
  647.                             if ($integerPart == 0$integerPart ''}
  648.                             $value "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
  649.                         else {
  650.                             $adjustedDecimalPart += $integerPart $adjustedDecimalDivisor;
  651.                             $value "$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
  652.                         }
  653.  
  654.                     else {
  655.                         // Handle the number itself
  656.                         $number_regex "/(\d+)(\.?)(\d*)/";
  657.                         if (preg_match($number_regex$format$matches)) {
  658.                             $left $matches[1];
  659.                             $dec $matches[2];
  660.                             $right $matches[3];
  661.                             if ($useThousands{
  662.                                 $value number_format(
  663.                                     $value
  664.                                     strlen($right)
  665.                                     PHPExcel_Shared_String::getDecimalSeparator()
  666.                                     PHPExcel_Shared_String::getThousandsSeparator()
  667.                                 );
  668.  
  669.                             else {
  670.                                 $sprintf_pattern "%1." strlen($right"f";
  671.                                 $value sprintf($sprintf_pattern$value);
  672.                             }
  673.                             $value preg_replace($number_regex$value$format);
  674.                         }
  675.                     }
  676.                 }
  677.             }
  678.         }
  679.  
  680.         // Additional formatting provided by callback function
  681.         if ($callBack !== null{
  682.             list($writerInstance$function$callBack;
  683.             $value $writerInstance->$function($value$formatColor);
  684.         }
  685.  
  686.         return $value;
  687.     }
  688. }

Documentation generated on Mon, 11 Jan 2010 08:12:15 +0100 by phpDocumentor 1.4.1