ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
PHPExcel_Style_NumberFormat Class Reference
+ Inheritance diagram for PHPExcel_Style_NumberFormat:
+ Collaboration diagram for PHPExcel_Style_NumberFormat:

Public Member Functions

 __construct ($isSupervisor=FALSE, $isConditional=FALSE)
 Create a new PHPExcel_Style_NumberFormat. More...
 
 getSharedComponent ()
 Get the shared style component for the currently active cell in currently active sheet. More...
 
 getStyleArray ($array)
 Build style array from subcomponents. More...
 
 applyFromArray ($pStyles=null)
 Apply styles from array. More...
 
 getFormatCode ()
 Get Format Code. More...
 
 setFormatCode ($pValue=PHPExcel_Style_NumberFormat::FORMAT_GENERAL)
 Set Format Code. More...
 
 getBuiltInFormatCode ()
 Get Built-In Format Code. More...
 
 setBuiltInFormatCode ($pValue=0)
 Set Built-In Format Code. More...
 
 getHashCode ()
 Get hash code. More...
 
- Public Member Functions inherited from PHPExcel_Style_Supervisor
 __construct ($isSupervisor=FALSE)
 Create a new PHPExcel_Style_Alignment. More...
 
 bindParent ($parent, $parentPropertyName=NULL)
 Bind parent. More...
 
 getIsSupervisor ()
 Is this a supervisor or a cell style component? More...
 
 getActiveSheet ()
 Get the currently active sheet. More...
 
 getSelectedCells ()
 Get the currently active cell coordinate in currently active sheet. More...
 
 getActiveCell ()
 Get the currently active cell coordinate in currently active sheet. More...
 
 __clone ()
 Implement PHP __clone to create a deep clone, not just a shallow copy. More...
 

Static Public Member Functions

static builtInFormatCode ($pIndex)
 Get built-in format code. More...
 
static builtInFormatCodeIndex ($formatCode)
 Get built-in format code index. More...
 
static toFormattedString ($value='0', $format=PHPExcel_Style_NumberFormat::FORMAT_GENERAL, $callBack=null)
 Convert a value in a pre-defined format to a PHP string. More...
 

Data Fields

const FORMAT_GENERAL = 'General'
 
const FORMAT_TEXT = '@'
 
const FORMAT_NUMBER = '0'
 
const FORMAT_NUMBER_00 = '0.00'
 
const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'
 
const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'
 
const FORMAT_PERCENTAGE = '0%'
 
const FORMAT_PERCENTAGE_00 = '0.00%'
 
const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'
 
const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'
 
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'
 
const FORMAT_DATE_DMYSLASH = 'd/m/y'
 
const FORMAT_DATE_DMYMINUS = 'd-m-y'
 
const FORMAT_DATE_DMMINUS = 'd-m'
 
const FORMAT_DATE_MYMINUS = 'm-y'
 
const FORMAT_DATE_XLSX14 = 'mm-dd-yy'
 
const FORMAT_DATE_XLSX15 = 'd-mmm-yy'
 
const FORMAT_DATE_XLSX16 = 'd-mmm'
 
const FORMAT_DATE_XLSX17 = 'mmm-yy'
 
const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'
 
const FORMAT_DATE_DATETIME = 'd/m/y h:mm'
 
const FORMAT_DATE_TIME1 = 'h:mm AM/PM'
 
const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'
 
const FORMAT_DATE_TIME3 = 'h:mm'
 
const FORMAT_DATE_TIME4 = 'h:mm:ss'
 
const FORMAT_DATE_TIME5 = 'mm:ss'
 
const FORMAT_DATE_TIME6 = 'h:mm:ss'
 
const FORMAT_DATE_TIME7 = 'i:s.S'
 
const FORMAT_DATE_TIME8 = 'h:mm:ss;@'
 
const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'
 
const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'
 
const FORMAT_CURRENCY_USD = '$#,##0_-'
 
const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'
 

Protected Attributes

 $_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL
 
 $_builtInFormatCode = 0
 
- Protected Attributes inherited from PHPExcel_Style_Supervisor
 $_isSupervisor
 
 $_parent
 

Static Protected Attributes

static $_builtInFormats
 
static $_flippedBuiltInFormats
 

Static Private Member Functions

static fillBuiltInFormatCodes ()
 Fill built-in format codes. More...
 
static _formatAsDate (&$value, &$format)
 
static _formatAsPercentage (&$value, &$format)
 
static _formatAsFraction (&$value, &$format)
 
static _complexNumberFormatMask ($number, $mask, $level=0)
 

Static Private Attributes

static $_dateFormatReplacements
 
static $_dateFormatReplacements24
 
static $_dateFormatReplacements12
 

Detailed Description

Definition at line 36 of file NumberFormat.php.

Constructor & Destructor Documentation

◆ __construct()

PHPExcel_Style_NumberFormat::__construct (   $isSupervisor = FALSE,
  $isConditional = FALSE 
)

Create a new PHPExcel_Style_NumberFormat.

Parameters
boolean$isSupervisorFlag indicating if this is a supervisor or not Leave this value at default unless you understand exactly what its ramifications are
boolean$isConditionalFlag indicating if this is a conditional style or not Leave this value at default unless you understand exactly what its ramifications are

Definition at line 116 of file NumberFormat.php.

117  {
118  // Supervisor?
119  parent::__construct($isSupervisor);
120 
121  if ($isConditional) {
122  $this->_formatCode = NULL;
123  $this->_builtInFormatCode = FALSE;
124  }
125  }

Member Function Documentation

◆ _complexNumberFormatMask()

static PHPExcel_Style_NumberFormat::_complexNumberFormatMask (   $number,
  $mask,
  $level = 0 
)
staticprivate

Definition at line 501 of file NumberFormat.php.

References $mask, $r, $result, and $size.

501  {
502  $sign = ($number < 0.0);
503  $number = abs($number);
504  if (strpos($mask,'.') !== false) {
505  $numbers = explode('.', $number . '.0');
506  $masks = explode('.', $mask . '.0');
507  $result1 = self::_complexNumberFormatMask($numbers[0], $masks[0], 1);
508  $result2 = strrev(self::_complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]), 1));
509  return (($sign) ? '-' : '') . $result1 . '.' . $result2;
510  }
511 
512  $r = preg_match_all('/0+/', $mask, $result, PREG_OFFSET_CAPTURE);
513  if ($r > 1) {
514  $result = array_reverse($result[0]);
515 
516  foreach($result as $block) {
517  $divisor = 1 . $block[0];
518  $size = strlen($block[0]);
519  $offset = $block[1];
520 
521  $blockValue = sprintf(
522  '%0' . $size . 'd',
523  fmod($number, $divisor)
524  );
525  $number = floor($number / $divisor);
526  $mask = substr_replace($mask, $blockValue, $offset, $size);
527  }
528  if ($number > 0) {
529  $mask = substr_replace($mask, $number, $offset, 0);
530  }
531  $result = $mask;
532  } else {
533  $result = $number;
534  }
535 
536  return (($sign) ? '-' : '') . $result;
537  }
$size
Definition: RandomTest.php:79
$result
$r
Definition: example_031.php:79
$mask
Definition: example_042.php:90

◆ _formatAsDate()

static PHPExcel_Style_NumberFormat::_formatAsDate ( $value,
$format 
)
staticprivate

Definition at line 435 of file NumberFormat.php.

References PHPExcel_Shared_Date\ExcelToPHPObject().

436  {
437  // dvc: convert Excel formats to PHP date formats
438 
439  // strip off first part containing e.g. [$-F800] or [$USD-409]
440  // general syntax: [$<Currency string>-<language info>]
441  // language info is in hexadecimal
442  $format = preg_replace('/^(\[\$[A-Z]*-[0-9A-F]*\])/i', '', $format);
443 
444  // OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case
445  $format = strtolower($format);
446 
447  $format = strtr($format,self::$_dateFormatReplacements);
448  if (!strpos($format,'A')) { // 24-hour time format
449  $format = strtr($format,self::$_dateFormatReplacements24);
450  } else { // 12-hour time format
451  $format = strtr($format,self::$_dateFormatReplacements12);
452  }
453 
454  $dateObj = PHPExcel_Shared_Date::ExcelToPHPObject($value);
455  $value = $dateObj->format($format);
456  }
static ExcelToPHPObject($dateValue=0)
Convert a date from Excel to a PHP Date/Time object.
Definition: Date.php:160
+ Here is the call graph for this function:

◆ _formatAsFraction()

static PHPExcel_Style_NumberFormat::_formatAsFraction ( $value,
$format 
)
staticprivate

Definition at line 476 of file NumberFormat.php.

References PHPExcel_Calculation_MathTrig\GCD().

477  {
478  $sign = ($value < 0) ? '-' : '';
479 
480  $integerPart = floor(abs($value));
481  $decimalPart = trim(fmod(abs($value),1),'0.');
482  $decimalLength = strlen($decimalPart);
483  $decimalDivisor = pow(10,$decimalLength);
484 
485  $GCD = PHPExcel_Calculation_MathTrig::GCD($decimalPart,$decimalDivisor);
486 
487  $adjustedDecimalPart = $decimalPart/$GCD;
488  $adjustedDecimalDivisor = $decimalDivisor/$GCD;
489 
490  if ((strpos($format,'0') !== false) || (strpos($format,'#') !== false) || (substr($format,0,3) == '? ?')) {
491  if ($integerPart == 0) {
492  $integerPart = '';
493  }
494  $value = "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
495  } else {
496  $adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
497  $value = "$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
498  }
499  }
+ Here is the call graph for this function:

◆ _formatAsPercentage()

static PHPExcel_Style_NumberFormat::_formatAsPercentage ( $value,
$format 
)
staticprivate

Definition at line 458 of file NumberFormat.php.

459  {
460  if ($format === self::FORMAT_PERCENTAGE) {
461  $value = round( (100 * $value), 0) . '%';
462  } else {
463  if (preg_match('/\.[#0]+/i', $format, $m)) {
464  $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1);
465  $format = str_replace($m[0], $s, $format);
466  }
467  if (preg_match('/^[#0]+/', $format, $m)) {
468  $format = str_replace($m[0], strlen($m[0]), $format);
469  }
470  $format = '%' . str_replace('%', 'f%%', $format);
471 
472  $value = sprintf($format, 100 * $value);
473  }
474  }

◆ applyFromArray()

PHPExcel_Style_NumberFormat::applyFromArray (   $pStyles = null)

Apply styles from array.

$objPHPExcel->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray( array( 'code' => PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE ) );

Parameters
array$pStylesArray containing style information
Exceptions
PHPExcel_Exception
Returns
PHPExcel_Style_NumberFormat

Definition at line 164 of file NumberFormat.php.

References PHPExcel_Style_Supervisor\getActiveSheet(), PHPExcel_Style_Supervisor\getSelectedCells(), getStyleArray(), and setFormatCode().

165  {
166  if (is_array($pStyles)) {
167  if ($this->_isSupervisor) {
168  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
169  } else {
170  if (array_key_exists('code', $pStyles)) {
171  $this->setFormatCode($pStyles['code']);
172  }
173  }
174  } else {
175  throw new PHPExcel_Exception("Invalid style array passed.");
176  }
177  return $this;
178  }
setFormatCode($pValue=PHPExcel_Style_NumberFormat::FORMAT_GENERAL)
Set Format Code.
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:103
getStyleArray($array)
Build style array from subcomponents.
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:92
+ Here is the call graph for this function:

◆ builtInFormatCode()

static PHPExcel_Style_NumberFormat::builtInFormatCode (   $pIndex)
static

Get built-in format code.

Parameters
int$pIndex
Returns
string

Definition at line 321 of file NumberFormat.php.

Referenced by PHPExcel_Reader_Excel5\_readXf(), and PHPExcel_Reader_Excel2007\load().

322  {
323  // Clean parameter
324  $pIndex = intval($pIndex);
325 
326  // Ensure built-in format codes are available
327  self::fillBuiltInFormatCodes();
328 
329  // Lookup format code
330  if (isset(self::$_builtInFormats[$pIndex])) {
331  return self::$_builtInFormats[$pIndex];
332  }
333 
334  return '';
335  }
+ Here is the caller graph for this function:

◆ builtInFormatCodeIndex()

static PHPExcel_Style_NumberFormat::builtInFormatCodeIndex (   $formatCode)
static

Get built-in format code index.

Parameters
string$formatCode
Returns
int|boolean

Definition at line 343 of file NumberFormat.php.

344  {
345  // Ensure built-in format codes are available
346  self::fillBuiltInFormatCodes();
347 
348  // Lookup format code
349  if (isset(self::$_flippedBuiltInFormats[$formatCode])) {
350  return self::$_flippedBuiltInFormats[$formatCode];
351  }
352 
353  return false;
354  }

◆ fillBuiltInFormatCodes()

static PHPExcel_Style_NumberFormat::fillBuiltInFormatCodes ( )
staticprivate

Fill built-in format codes.

Definition at line 253 of file NumberFormat.php.

References array, and FORMAT_GENERAL.

254  {
255  // Built-in format codes
256  if (is_null(self::$_builtInFormats)) {
257  self::$_builtInFormats = array();
258 
259  // General
260  self::$_builtInFormats[0] = PHPExcel_Style_NumberFormat::FORMAT_GENERAL;
261  self::$_builtInFormats[1] = '0';
262  self::$_builtInFormats[2] = '0.00';
263  self::$_builtInFormats[3] = '#,##0';
264  self::$_builtInFormats[4] = '#,##0.00';
265 
266  self::$_builtInFormats[9] = '0%';
267  self::$_builtInFormats[10] = '0.00%';
268  self::$_builtInFormats[11] = '0.00E+00';
269  self::$_builtInFormats[12] = '# ?/?';
270  self::$_builtInFormats[13] = '# ??/??';
271  self::$_builtInFormats[14] = 'mm-dd-yy';
272  self::$_builtInFormats[15] = 'd-mmm-yy';
273  self::$_builtInFormats[16] = 'd-mmm';
274  self::$_builtInFormats[17] = 'mmm-yy';
275  self::$_builtInFormats[18] = 'h:mm AM/PM';
276  self::$_builtInFormats[19] = 'h:mm:ss AM/PM';
277  self::$_builtInFormats[20] = 'h:mm';
278  self::$_builtInFormats[21] = 'h:mm:ss';
279  self::$_builtInFormats[22] = 'm/d/yy h:mm';
280 
281  self::$_builtInFormats[37] = '#,##0 ;(#,##0)';
282  self::$_builtInFormats[38] = '#,##0 ;[Red](#,##0)';
283  self::$_builtInFormats[39] = '#,##0.00;(#,##0.00)';
284  self::$_builtInFormats[40] = '#,##0.00;[Red](#,##0.00)';
285 
286  self::$_builtInFormats[44] = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
287  self::$_builtInFormats[45] = 'mm:ss';
288  self::$_builtInFormats[46] = '[h]:mm:ss';
289  self::$_builtInFormats[47] = 'mmss.0';
290  self::$_builtInFormats[48] = '##0.0E+0';
291  self::$_builtInFormats[49] = '@';
292 
293  // CHT
294  self::$_builtInFormats[27] = '[$-404]e/m/d';
295  self::$_builtInFormats[30] = 'm/d/yy';
296  self::$_builtInFormats[36] = '[$-404]e/m/d';
297  self::$_builtInFormats[50] = '[$-404]e/m/d';
298  self::$_builtInFormats[57] = '[$-404]e/m/d';
299 
300  // THA
301  self::$_builtInFormats[59] = 't0';
302  self::$_builtInFormats[60] = 't0.00';
303  self::$_builtInFormats[61] = 't#,##0';
304  self::$_builtInFormats[62] = 't#,##0.00';
305  self::$_builtInFormats[67] = 't0%';
306  self::$_builtInFormats[68] = 't0.00%';
307  self::$_builtInFormats[69] = 't# ?/?';
308  self::$_builtInFormats[70] = 't# ??/??';
309 
310  // Flip array (for faster lookups)
311  self::$_flippedBuiltInFormats = array_flip(self::$_builtInFormats);
312  }
313  }
Create styles array
The data for the language used.

◆ getBuiltInFormatCode()

PHPExcel_Style_NumberFormat::getBuiltInFormatCode ( )

Get Built-In Format Code.

Returns
int

Definition at line 223 of file NumberFormat.php.

References $_builtInFormatCode, and getSharedComponent().

224  {
225  if ($this->_isSupervisor) {
226  return $this->getSharedComponent()->getBuiltInFormatCode();
227  }
229  }
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
+ Here is the call graph for this function:

◆ getFormatCode()

PHPExcel_Style_NumberFormat::getFormatCode ( )

Get Format Code.

Returns
string

Definition at line 185 of file NumberFormat.php.

References $_formatCode, and getSharedComponent().

Referenced by PHPExcel_Shared_Date\isDateTimeFormat().

186  {
187  if ($this->_isSupervisor) {
188  return $this->getSharedComponent()->getFormatCode();
189  }
190  if ($this->_builtInFormatCode !== false)
191  {
192  return self::builtInFormatCode($this->_builtInFormatCode);
193  }
194  return $this->_formatCode;
195  }
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getHashCode()

PHPExcel_Style_NumberFormat::getHashCode ( )

Get hash code.

Returns
string Hash code

Implements PHPExcel_IComparable.

Definition at line 361 of file NumberFormat.php.

References getSharedComponent().

362  {
363  if ($this->_isSupervisor) {
364  return $this->getSharedComponent()->getHashCode();
365  }
366  return md5(
367  $this->_formatCode
368  . $this->_builtInFormatCode
369  . __CLASS__
370  );
371  }
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
+ Here is the call graph for this function:

◆ getSharedComponent()

PHPExcel_Style_NumberFormat::getSharedComponent ( )

Get the shared style component for the currently active cell in currently active sheet.

Only used for style supervisor

Returns
PHPExcel_Style_NumberFormat

Definition at line 133 of file NumberFormat.php.

Referenced by getBuiltInFormatCode(), getFormatCode(), and getHashCode().

134  {
135  return $this->_parent->getSharedComponent()->getNumberFormat();
136  }
+ Here is the caller graph for this function:

◆ getStyleArray()

PHPExcel_Style_NumberFormat::getStyleArray (   $array)

Build style array from subcomponents.

Parameters
array$array
Returns
array

Definition at line 144 of file NumberFormat.php.

References array.

Referenced by applyFromArray(), setBuiltInFormatCode(), and setFormatCode().

145  {
146  return array('numberformat' => $array);
147  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ setBuiltInFormatCode()

PHPExcel_Style_NumberFormat::setBuiltInFormatCode (   $pValue = 0)

Set Built-In Format Code.

Parameters
int$pValue
Returns
PHPExcel_Style_NumberFormat

Definition at line 237 of file NumberFormat.php.

References array, PHPExcel_Style_Supervisor\getActiveSheet(), PHPExcel_Style_Supervisor\getSelectedCells(), and getStyleArray().

238  {
239 
240  if ($this->_isSupervisor) {
241  $styleArray = $this->getStyleArray(array('code' => self::builtInFormatCode($pValue)));
242  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
243  } else {
244  $this->_builtInFormatCode = $pValue;
245  $this->_formatCode = self::builtInFormatCode($pValue);
246  }
247  return $this;
248  }
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:103
getStyleArray($array)
Build style array from subcomponents.
Create styles array
The data for the language used.
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:92
+ Here is the call graph for this function:

◆ setFormatCode()

PHPExcel_Style_NumberFormat::setFormatCode (   $pValue = PHPExcel_Style_NumberFormat::FORMAT_GENERAL)

Set Format Code.

Parameters
string$pValue
Returns
PHPExcel_Style_NumberFormat

Definition at line 203 of file NumberFormat.php.

References array, FORMAT_GENERAL, PHPExcel_Style_Supervisor\getActiveSheet(), PHPExcel_Style_Supervisor\getSelectedCells(), and getStyleArray().

Referenced by applyFromArray().

204  {
205  if ($pValue == '') {
207  }
208  if ($this->_isSupervisor) {
209  $styleArray = $this->getStyleArray(array('code' => $pValue));
210  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
211  } else {
212  $this->_formatCode = $pValue;
213  $this->_builtInFormatCode = self::builtInFormatCodeIndex($pValue);
214  }
215  return $this;
216  }
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:103
getStyleArray($array)
Build style array from subcomponents.
Create styles array
The data for the language used.
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:92
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ toFormattedString()

static PHPExcel_Style_NumberFormat::toFormattedString (   $value = '0',
  $format = PHPExcel_Style_NumberFormat::FORMAT_GENERAL,
  $callBack = null 
)
static

Convert a value in a pre-defined format to a PHP string.

Parameters
mixed$valueValue to format
string$formatFormat code
array$callBackCallback function for additional formatting of string
Returns
string Formatted string

Definition at line 547 of file NumberFormat.php.

References $n, array, FORMAT_GENERAL, FORMAT_TEXT, PHPExcel_Shared_String\getCurrencyCode(), PHPExcel_Shared_String\getDecimalSeparator(), and PHPExcel_Shared_String\getThousandsSeparator().

Referenced by PHPExcel_Chart_Renderer_jpgraph\_formatDataSetLabels(), PHPExcel_Writer_HTML\_generateRow(), PHPExcel_Worksheet\calculateColumnWidths(), PHPExcel_Calculation_TextData\DOLLAR(), PHPExcel_Cell\getFormattedValue(), PHPExcel_Worksheet\rangeToArray(), and PHPExcel_Calculation_TextData\TEXTFORMAT().

548  {
549  // For now we do not treat strings although section 4 of a format code affects strings
550  if (!is_numeric($value)) return $value;
551 
552  // For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
553  // it seems to round numbers to a total of 10 digits.
555  return $value;
556  }
557 
558  // Get the sections, there can be up to four sections
559  $sections = explode(';', $format);
560 
561  // Fetch the relevant section depending on whether number is positive, negative, or zero?
562  // Text not supported yet.
563  // Here is how the sections apply to various values in Excel:
564  // 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
565  // 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
566  // 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
567  // 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
568  switch (count($sections)) {
569  case 1:
570  $format = $sections[0];
571  break;
572 
573  case 2:
574  $format = ($value >= 0) ? $sections[0] : $sections[1];
575  $value = abs($value); // Use the absolute value
576  break;
577 
578  case 3:
579  $format = ($value > 0) ?
580  $sections[0] : ( ($value < 0) ?
581  $sections[1] : $sections[2]);
582  $value = abs($value); // Use the absolute value
583  break;
584 
585  case 4:
586  $format = ($value > 0) ?
587  $sections[0] : ( ($value < 0) ?
588  $sections[1] : $sections[2]);
589  $value = abs($value); // Use the absolute value
590  break;
591 
592  default:
593  // something is wrong, just use first section
594  $format = $sections[0];
595  break;
596  }
597 
598  // Save format with color information for later use below
599  $formatColor = $format;
600 
601  // Strip color information
602  $color_regex = '/^\\[[a-zA-Z]+\\]/';
603  $format = preg_replace($color_regex, '', $format);
604 
605  // Let's begin inspecting the format and converting the value to a formatted string
606  if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $format)) { // datetime format
607  self::_formatAsDate($value, $format);
608  } else if (preg_match('/%$/', $format)) { // % number format
609  self::_formatAsPercentage($value, $format);
610  } else {
611  if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
612  $value = 'EUR ' . sprintf('%1.2f', $value);
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 // Handle escaped characters, such as \" to display a literal " or \\ to display a literal \
620 // $format = preg_replace('/(?<!\\\\)\"/', '', $format);
621 // $format = str_replace(array('\\"', '*'), array('"', ''), $format);
622 
623  // Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
624  $format = str_replace(array('"', '*'), '', $format);
625 
626  // Find out if we need thousands separator
627  // This is indicated by a comma enclosed by a digit placeholder:
628  // #,# or 0,0
629  $useThousands = preg_match('/(#,#|0,0)/', $format);
630  if ($useThousands) {
631  $format = preg_replace('/0,0/', '00', $format);
632  $format = preg_replace('/#,#/', '##', $format);
633  }
634 
635  // Scale thousands, millions,...
636  // This is indicated by a number of commas after a digit placeholder:
637  // #, or 0.0,,
638  $scale = 1; // same as no scale
639  $matches = array();
640  if (preg_match('/(#|0)(,+)/', $format, $matches)) {
641  $scale = pow(1000, strlen($matches[2]));
642 
643  // strip the commas
644  $format = preg_replace('/0,+/', '0', $format);
645  $format = preg_replace('/#,+/', '#', $format);
646  }
647 
648  if (preg_match('/#?.*\?\/\?/', $format, $m)) {
649  //echo 'Format mask is fractional '.$format.' <br />';
650  if ($value != (int)$value) {
651  self::_formatAsFraction($value, $format);
652  }
653 
654  } else {
655  // Handle the number itself
656 
657  // scale number
658  $value = $value / $scale;
659 
660  // Strip #
661  $format = preg_replace('/\\#/', '0', $format);
662 
663  $n = "/\[[^\]]+\]/";
664  $m = preg_replace($n, '', $format);
665  $number_regex = "/(0+)(\.?)(0*)/";
666  if (preg_match($number_regex, $m, $matches)) {
667  $left = $matches[1];
668  $dec = $matches[2];
669  $right = $matches[3];
670 
671  // minimun width of formatted number (including dot)
672  $minWidth = strlen($left) + strlen($dec) + strlen($right);
673  if ($useThousands) {
674  $value = number_format(
675  $value
676  , strlen($right)
679  );
680  $value = preg_replace($number_regex, $value, $format);
681  } else {
682  if (preg_match('/[0#]E[+-]0/i', $format)) {
683  // Scientific format
684  $value = sprintf('%5.2E', $value);
685  } elseif (preg_match('/0([^\d\.]+)0/', $format)) {
686  $value = self::_complexNumberFormatMask($value, $format);
687  } else {
688  $sprintf_pattern = "%0$minWidth." . strlen($right) . "f";
689  $value = sprintf($sprintf_pattern, $value);
690  $value = preg_replace($number_regex, $value, $format);
691  }
692  }
693  }
694  }
695  if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
696  // Currency or Accounting
697  $currencyFormat = $m[0];
698  $currencyCode = $m[1];
699  list($currencyCode) = explode('-',$currencyCode);
700  if ($currencyCode == '') {
701  $currencyCode = PHPExcel_Shared_String::getCurrencyCode();
702  }
703  $value = preg_replace('/\[\$([^\]]*)\]/u',$currencyCode,$value);
704  }
705  }
706  }
707 
708  // Additional formatting provided by callback function
709  if ($callBack !== null) {
710  list($writerInstance, $function) = $callBack;
711  $value = $writerInstance->$function($value, $formatColor);
712  }
713 
714  return $value;
715  }
static getCurrencyCode()
Get the currency code.
Definition: String.php:751
static getThousandsSeparator()
Get the thousands separator.
Definition: String.php:719
$n
Definition: RandomTest.php:80
Create styles array
The data for the language used.
static getDecimalSeparator()
Get the decimal separator.
Definition: String.php:687
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $_builtInFormatCode

PHPExcel_Style_NumberFormat::$_builtInFormatCode = 0
protected

Definition at line 104 of file NumberFormat.php.

Referenced by getBuiltInFormatCode().

◆ $_builtInFormats

PHPExcel_Style_NumberFormat::$_builtInFormats
staticprotected

Definition at line 83 of file NumberFormat.php.

◆ $_dateFormatReplacements

PHPExcel_Style_NumberFormat::$_dateFormatReplacements
staticprivate

Definition at line 378 of file NumberFormat.php.

◆ $_dateFormatReplacements12

PHPExcel_Style_NumberFormat::$_dateFormatReplacements12
staticprivate
Initial value:
'hh' => 'h',
'h' => 'g'
)

Definition at line 430 of file NumberFormat.php.

◆ $_dateFormatReplacements24

PHPExcel_Style_NumberFormat::$_dateFormatReplacements24
staticprivate
Initial value:
'hh' => 'H',
'h' => 'G'
)

Definition at line 421 of file NumberFormat.php.

◆ $_flippedBuiltInFormats

PHPExcel_Style_NumberFormat::$_flippedBuiltInFormats
staticprotected

Definition at line 90 of file NumberFormat.php.

◆ $_formatCode

PHPExcel_Style_NumberFormat::$_formatCode = PHPExcel_Style_NumberFormat::FORMAT_GENERAL
protected

Definition at line 97 of file NumberFormat.php.

Referenced by getFormatCode().

◆ FORMAT_CURRENCY_EUR_SIMPLE

const PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-'

Definition at line 76 of file NumberFormat.php.

◆ FORMAT_CURRENCY_USD

const PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD = '$#,##0_-'

Definition at line 75 of file NumberFormat.php.

◆ FORMAT_CURRENCY_USD_SIMPLE

const PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-'

◆ FORMAT_DATE_DATETIME

const PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME = 'd/m/y h:mm'

Definition at line 63 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_DDMMYYYY

const PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY = 'dd/mm/yy'

Definition at line 53 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_DMMINUS

const PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS = 'd-m'

Definition at line 56 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_DMYMINUS

const PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYMINUS = 'd-m-y'

Definition at line 55 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_DMYSLASH

const PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH = 'd/m/y'

Definition at line 54 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_MYMINUS

const PHPExcel_Style_NumberFormat::FORMAT_DATE_MYMINUS = 'm-y'

Definition at line 57 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME1

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME1 = 'h:mm AM/PM'

Definition at line 64 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME2

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM'

Definition at line 65 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME3

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 = 'h:mm'

◆ FORMAT_DATE_TIME4

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 = 'h:mm:ss'

◆ FORMAT_DATE_TIME5

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME5 = 'mm:ss'

Definition at line 68 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME6

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME6 = 'h:mm:ss'

Definition at line 69 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME7

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME7 = 'i:s.S'

Definition at line 70 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_TIME8

const PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME8 = 'h:mm:ss;@'

Definition at line 71 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_XLSX14

const PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX14 = 'mm-dd-yy'

Definition at line 58 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_XLSX15

const PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15 = 'd-mmm-yy'

◆ FORMAT_DATE_XLSX16

const PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX16 = 'd-mmm'

Definition at line 60 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_XLSX17

const PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX17 = 'mmm-yy'

Definition at line 61 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_XLSX22

const PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX22 = 'm/d/yy h:mm'

Definition at line 62 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_YYYYMMDD

const PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD = 'yy-mm-dd'

Definition at line 52 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_YYYYMMDD2

const PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd'

Definition at line 51 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_DATE_YYYYMMDDSLASH

const PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@'

Definition at line 72 of file NumberFormat.php.

Referenced by PHPExcel_Shared_Date\isDateTimeFormatCode().

◆ FORMAT_GENERAL

◆ FORMAT_NUMBER

const PHPExcel_Style_NumberFormat::FORMAT_NUMBER = '0'

Definition at line 43 of file NumberFormat.php.

◆ FORMAT_NUMBER_00

const PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00 = '0.00'

Definition at line 44 of file NumberFormat.php.

◆ FORMAT_NUMBER_COMMA_SEPARATED1

const PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00'

Definition at line 45 of file NumberFormat.php.

◆ FORMAT_NUMBER_COMMA_SEPARATED2

const PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-'

Definition at line 46 of file NumberFormat.php.

◆ FORMAT_PERCENTAGE

const PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE = '0%'

Definition at line 48 of file NumberFormat.php.

◆ FORMAT_PERCENTAGE_00

const PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00 = '0.00%'

◆ FORMAT_TEXT

const PHPExcel_Style_NumberFormat::FORMAT_TEXT = '@'

Definition at line 41 of file NumberFormat.php.

Referenced by toFormattedString().


The documentation for this class was generated from the following file: