30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
38 require_once PHPEXCEL_ROOT .
'PHPExcel/IComparable.php';
41 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/Date.php';
44 require_once PHPEXCEL_ROOT .
'PHPExcel/Calculation/Functions.php';
151 $this->_isSupervisor = $isSupervisor;
155 $this->_builtInFormatCode = 0;
166 $this->_parent = $parent;
187 return $this->_parent->getSharedComponent()->getNumberFormat();
197 return $this->_parent->getActiveSheet();
230 return array(
'numberformat' => $array);
249 if (is_array($pStyles)) {
250 if ($this->_isSupervisor) {
253 if (array_key_exists(
'code', $pStyles)) {
258 throw new Exception(
"Invalid style array passed.");
269 if ($this->_isSupervisor) {
272 if ($this->_builtInFormatCode !==
false)
289 if ($this->_isSupervisor) {
290 $styleArray = $this->
getStyleArray(array(
'code' => $pValue));
293 $this->_formatCode = $pValue;
305 if ($this->_isSupervisor) {
319 if ($this->_isSupervisor) {
320 $styleArray = $this->
getStyleArray(array(
'code' => self::builtInFormatCode($pValue)));
323 $this->_builtInFormatCode = $pValue;
335 if (is_null(self::$_builtInFormats)) {
336 self::$_builtInFormats = array();
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';
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';
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)';
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] =
'@';
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';
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# ??/??';
390 self::$_flippedBuiltInFormats = array_flip(self::$_builtInFormats);
402 $pIndex = intval($pIndex);
408 if (array_key_exists($pIndex, self::$_builtInFormats)) {
409 return self::$_builtInFormats[$pIndex];
426 if (array_key_exists($formatCode, self::$_flippedBuiltInFormats)) {
427 return self::$_flippedBuiltInFormats[$formatCode];
439 if ($this->_isSupervisor) {
444 . $this->_builtInFormatCode
477 $this->_hashIndex = $value;
484 $vars = get_object_vars($this);
485 foreach ($vars as
$key => $value) {
486 if (is_object($value)) {
487 $this->
$key = clone $value;
489 $this->
$key = $value;
503 if (!is_numeric($value))
return $value;
507 if ($format ===
'General') {
512 $parts = explode(
';', $format);
518 if (preg_match(
"/^[hmsdy]/i", $format)) {
523 $format = strtolower($format);
525 $format = str_replace(
'\\',
'', $format);
528 $format = str_replace(
'yyyy',
'Y', $format);
530 $format = str_replace(
'yy',
'y', $format);
532 $format = str_replace(
'mmmmm',
'M', $format);
534 $format = str_replace(
'mmmm',
'F', $format);
536 $format = str_replace(
'mmm',
'M', $format);
538 $format = str_replace(
':mm',
':i', $format);
540 $format = str_replace(
'mm',
'x', $format);
542 $format = str_replace(
'm',
'n', $format);
544 $format = str_replace(
'x',
'm', $format);
546 $format = str_replace(
'am/pm',
'A', $format);
548 $format = str_replace(
'dddd',
'l', $format);
550 $format = str_replace(
'ddd',
'D', $format);
552 $format = str_replace(
'dd',
'x', $format);
554 $format = str_replace(
'd',
'j', $format);
556 $format = str_replace(
'x',
'd', $format);
558 $format = str_replace(
'ss',
's', $format);
560 $format = str_replace(
'.s',
'', $format);
562 if (!strpos($format,
'A')) {
563 $format = str_replace(
'h',
'H', $format);
568 }
else if (preg_match(
'/%$/', $format)) {
569 if ($format === self::FORMAT_PERCENTAGE) {
570 return round( (100 * $value), 0) .
'%';
572 if (preg_match(
'/\.[#0]+/i', $format, $m)) {
573 $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1);
574 $format = str_replace($m[0], $s, $format);
576 if (preg_match(
'/^[#0]+/', $format, $m)) {
577 $format = str_replace($m[0], strlen($m[0]), $format);
579 $format =
'%' . str_replace(
'%',
'f%%', $format);
581 return sprintf($format, 100 * $value);
584 if (preg_match (
"/^([0-9.,-]+)$/", $value)) {
585 if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
586 return 'EUR ' . sprintf(
'%1.2f', $value);
590 $format = preg_replace(
'/_./',
'', $format);
593 $format = preg_replace(
"/\\\\/",
'', $format);
596 $format = preg_replace(
'/"/',
'', $format);
599 $format = preg_replace(
'/\\#/',
'0', $format);
602 $useThousands = preg_match(
'/,/', $format);
604 $format = preg_replace(
'/,/',
'', $format);
607 if (preg_match(
'/0?.*\?\/\?/', $format, $m)) {
609 $sign = ($value < 0) ?
'-' :
'';
611 $integerPart = floor(abs($value));
612 $decimalPart = trim(fmod(abs($value),1),
'0.');
613 $decimalLength = strlen($decimalPart);
614 $decimalDivisor = pow(10,$decimalLength);
618 $adjustedDecimalPart = $decimalPart/$GCD;
619 $adjustedDecimalDivisor = $decimalDivisor/$GCD;
621 if (strpos($format,
'0') !==
false) {
622 $value =
"$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
624 $adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
625 $value =
"$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
630 $number_regex =
"/(\d+)(\.?)(\d*)/";
631 if (preg_match($number_regex, $format, $matches)) {
634 $right = $matches[3];
636 $localeconv = localeconv();
637 if (($localeconv[
'thousands_sep'] ==
'') || ($localeconv[
'decimal_point'] ==
'')) {
638 $value = number_format($value, strlen($right), $localeconv[
'mon_decimal_point'], $localeconv[
'mon_thousands_sep']);
640 $value = number_format($value, strlen($right), $localeconv[
'decimal_point'], $localeconv[
'thousands_sep']);
643 $sprintf_pattern =
"%1." . strlen($right) .
"f";
644 $value = sprintf($sprintf_pattern, $value);
646 $value = preg_replace($number_regex, $value, $format);