Feature: Improved toFormattedString() handling for Currency and Accounting format masks to render currency symbols

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@67614 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2011-01-31 17:18:10 +00:00
parent 1bf40dadf9
commit 8c0663486b
3 changed files with 61 additions and 12 deletions

View File

@ -69,6 +69,13 @@ class PHPExcel_Shared_String
*/ */
private static $_thousandsSeparator; private static $_thousandsSeparator;
/**
* Currency code
*
* @var string
*/
private static $_currencyCode;
/** /**
* Is mbstring extension avalable? * Is mbstring extension avalable?
* *
@ -598,8 +605,7 @@ class PHPExcel_Shared_String
self::$_decimalSeparator = $localeconv['decimal_point'] != '' self::$_decimalSeparator = $localeconv['decimal_point'] != ''
? $localeconv['decimal_point'] : $localeconv['mon_decimal_point']; ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point'];
if (self::$_decimalSeparator == '') if (self::$_decimalSeparator == '') {
{
// Default to . // Default to .
self::$_decimalSeparator = '.'; self::$_decimalSeparator = '.';
} }
@ -645,6 +651,38 @@ class PHPExcel_Shared_String
self::$_thousandsSeparator = $pValue; self::$_thousandsSeparator = $pValue;
} }
/**
* Get the currency code. If it has not yet been set explicitly, try to obtain the
* symbol information from locale.
*
* @return string
*/
public static function getCurrencyCode()
{
if (!isset(self::$_currencyCode)) {
$localeconv = localeconv();
self::$_currencyCode = $localeconv['currency_symbol'] != ''
? $localeconv['currency_symbol'] : $localeconv['int_curr_symbol'];
if (self::$_currencyCode == '') {
// Default to $
self::$_currencyCode = '$';
}
}
return self::$_currencyCode;
}
/**
* Set the currency code. Only used by PHPExcel_Style_NumberFormat::toFormattedString()
* to format output by PHPExcel_Writer_HTML and PHPExcel_Writer_PDF
*
* @param string $pValue Character for currency code
*/
public static function setCurrencyCode($pValue = '$')
{
self::$_currencyCode = $pValue;
}
/** /**
* Convert SYLK encoded string to UTF-8 * Convert SYLK encoded string to UTF-8
* *

View File

@ -605,8 +605,8 @@ class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
// Some non-number characters are escaped with \, which we don't need // Some non-number characters are escaped with \, which we don't need
$format = preg_replace("/\\\\/", '', $format); $format = preg_replace("/\\\\/", '', $format);
// Some non-number strings are quoted, so we'll get rid of the quotes // Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = preg_replace('/"/', '', $format); $format = str_replace(array('"','*'), '', $format);
// Find out if we need thousands separator // Find out if we need thousands separator
// This is indicated by a comma enclosed by a digit placeholder: // This is indicated by a comma enclosed by a digit placeholder:
@ -663,9 +663,10 @@ class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
// Strip # // Strip #
$format = preg_replace('/\\#/', '', $format); $format = preg_replace('/\\#/', '', $format);
$n = "/\[[^\]]+\]/";
$m = preg_replace($n, '', $format);
$number_regex = "/(0+)(\.?)(0*)/"; $number_regex = "/(0+)(\.?)(0*)/";
$matches = array(); if (preg_match($number_regex, $m, $matches)) {
if (preg_match($number_regex, $format, $matches)) {
$left = $matches[1]; $left = $matches[1];
$dec = $matches[2]; $dec = $matches[2];
$right = $matches[3]; $right = $matches[3];
@ -675,12 +676,11 @@ class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
if ($useThousands) { if ($useThousands) {
$value = number_format( $value = number_format(
$value $value
, strlen($right) , strlen($right)
, PHPExcel_Shared_String::getDecimalSeparator() , PHPExcel_Shared_String::getDecimalSeparator()
, PHPExcel_Shared_String::getThousandsSeparator() , PHPExcel_Shared_String::getThousandsSeparator()
); );
} else { } else {
$sprintf_pattern = "%0$minWidth." . strlen($right) . "f"; $sprintf_pattern = "%0$minWidth." . strlen($right) . "f";
$value = sprintf($sprintf_pattern, $value); $value = sprintf($sprintf_pattern, $value);
@ -689,6 +689,16 @@ class PHPExcel_Style_NumberFormat implements PHPExcel_IComparable
$value = preg_replace($number_regex, $value, $format); $value = preg_replace($number_regex, $value, $format);
} }
} }
if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
// Currency or Accounting
$currencyFormat = $m[0];
$currencyCode = $m[1];
list($currencyCode) = explode('-',$currencyCode);
if ($currencyCode == '') {
$currencyCode = PHPExcel_Shared_String::getCurrencyCode();
}
$value = preg_replace('/\[\$([^\]]*)\]/u',$currencyCode,$value);
}
} }
} }

View File

@ -34,6 +34,7 @@ Fixed in SVN:
Functionally, these are identical to the toArray() method, except that they take an additional first parameter of a Range (e.g. 'B2:C3') or a Named Range name. Functionally, these are identical to the toArray() method, except that they take an additional first parameter of a Range (e.g. 'B2:C3') or a Named Range name.
Modified the toArray() method so that it actually uses rangeToArray(). Modified the toArray() method so that it actually uses rangeToArray().
- Feature: (MBaker) Added support for cell comments in the OOCalc, Gnumeric and Excel2003XML Readers, and in the Excel5 Reader - Feature: (MBaker) Added support for cell comments in the OOCalc, Gnumeric and Excel2003XML Readers, and in the Excel5 Reader
- Feature: (MBaker) Improved toFormattedString() handling for Currency and Accounting formats to render currency symbols
- Bugfix: (MBaker) Work item 14888 - Simple =IF() formula disappears - Bugfix: (MBaker) Work item 14888 - Simple =IF() formula disappears
- Bugfix: (MBaker) Work item 14898 - PHP Warning: preg_match(): Compilation failed: PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X - Bugfix: (MBaker) Work item 14898 - PHP Warning: preg_match(): Compilation failed: PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X
- Bugfix: (MBaker) Work item 14901 - VLOOKUP choking on parameters in PHPExcel.1.7.5/PHPExcel_Writer_Excel2007 - Bugfix: (MBaker) Work item 14901 - VLOOKUP choking on parameters in PHPExcel.1.7.5/PHPExcel_Writer_Excel2007