From 013c18b85958942fc4984b855af416322c654245 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 25 Jul 2012 13:15:27 +0100 Subject: [PATCH] General - merged (with some localisation mods) from a pull request by alexgann - Add Currency detection to the Advanced Value Binder --- Classes/PHPExcel/Cell/AdvancedValueBinder.php | 20 ++++++++++ Classes/PHPExcel/Shared/String.php | 6 +-- Tests/29advancedvaluebinder.php | 39 ++++++++++--------- changelog.txt | 1 + 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Classes/PHPExcel/Cell/AdvancedValueBinder.php b/Classes/PHPExcel/Cell/AdvancedValueBinder.php index cf0ed14..17a7334 100644 --- a/Classes/PHPExcel/Cell/AdvancedValueBinder.php +++ b/Classes/PHPExcel/Cell/AdvancedValueBinder.php @@ -88,6 +88,26 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder return true; } + // Check for currency + $currencyCode = PHPExcel_Shared_String::getCurrencyCode(); + if (preg_match('/^'.preg_quote($currencyCode).' ?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) { + // Convert value to number + $cell->setValueExplicit( trim(str_replace($currencyCode, '', $value)), PHPExcel_Cell_DataType::TYPE_NUMERIC); + // Set style + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( + str_replace('$', $currencyCode, PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ) + ); + return true; + } elseif (preg_match('/^\$ ?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/', $value)) { + // Convert value to number + $cell->setValueExplicit( trim(str_replace('$', '', $value)), PHPExcel_Cell_DataType::TYPE_NUMERIC); + // Set style + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ); + return true; + } + // Check for time without seconds e.g. '9:45', '09:45' if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) { list($h, $m) = explode(':', $value); diff --git a/Classes/PHPExcel/Shared/String.php b/Classes/PHPExcel/Shared/String.php index a875341..cee3ea4 100644 --- a/Classes/PHPExcel/Shared/String.php +++ b/Classes/PHPExcel/Shared/String.php @@ -612,7 +612,7 @@ class PHPExcel_Shared_String { if (!isset(self::$_decimalSeparator)) { $localeconv = localeconv(); - self::$_decimalSeparator = $localeconv['decimal_point'] != '' + self::$_decimalSeparator = ($localeconv['decimal_point'] != '') ? $localeconv['decimal_point'] : $localeconv['mon_decimal_point']; if (self::$_decimalSeparator == '') { @@ -644,7 +644,7 @@ class PHPExcel_Shared_String { if (!isset(self::$_thousandsSeparator)) { $localeconv = localeconv(); - self::$_thousandsSeparator = $localeconv['thousands_sep'] != '' + self::$_thousandsSeparator = ($localeconv['thousands_sep'] != '') ? $localeconv['thousands_sep'] : $localeconv['mon_thousands_sep']; } return self::$_thousandsSeparator; @@ -671,7 +671,7 @@ class PHPExcel_Shared_String { if (!isset(self::$_currencyCode)) { $localeconv = localeconv(); - self::$_currencyCode = $localeconv['currency_symbol'] != '' + self::$_currencyCode = ($localeconv['currency_symbol'] != '') ? $localeconv['currency_symbol'] : $localeconv['int_curr_symbol']; if (self::$_currencyCode == '') { diff --git a/Tests/29advancedvaluebinder.php b/Tests/29advancedvaluebinder.php index c58911b..e2e7d12 100644 --- a/Tests/29advancedvaluebinder.php +++ b/Tests/29advancedvaluebinder.php @@ -104,32 +104,35 @@ $objPHPExcel->getActiveSheet()->setCellValue('B11', '10%'); $objPHPExcel->getActiveSheet()->setCellValue('A12', 'Percentage value #2:'); $objPHPExcel->getActiveSheet()->setCellValue('B12', '12.5%'); -$objPHPExcel->getActiveSheet()->setCellValue('A13', 'Date value #1:'); -$objPHPExcel->getActiveSheet()->setCellValue('B13', '21 December 1983'); +$objPHPExcel->getActiveSheet()->setCellValue('A13', 'Currency value:'); +$objPHPExcel->getActiveSheet()->setCellValue('B13', '$12345'); -$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Date value #2:'); -$objPHPExcel->getActiveSheet()->setCellValue('B14', '19-Dec-1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Date value #1:'); +$objPHPExcel->getActiveSheet()->setCellValue('B14', '21 December 1983'); -$objPHPExcel->getActiveSheet()->setCellValue('A15', 'Date value #3:'); -$objPHPExcel->getActiveSheet()->setCellValue('B15', '19/12/1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A15', 'Date value #2:'); +$objPHPExcel->getActiveSheet()->setCellValue('B15', '19-Dec-1960'); -$objPHPExcel->getActiveSheet()->setCellValue('A16', 'Date value #4:'); -$objPHPExcel->getActiveSheet()->setCellValue('B16', '19-12-1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A16', 'Date value #3:'); +$objPHPExcel->getActiveSheet()->setCellValue('B16', '19/12/1960'); -$objPHPExcel->getActiveSheet()->setCellValue('A17', 'Date value #5:'); -$objPHPExcel->getActiveSheet()->setCellValue('B17', '1-Jan'); +$objPHPExcel->getActiveSheet()->setCellValue('A17', 'Date value #4:'); +$objPHPExcel->getActiveSheet()->setCellValue('B17', '19-12-1960'); -$objPHPExcel->getActiveSheet()->setCellValue('A18', 'Time value #1:'); -$objPHPExcel->getActiveSheet()->setCellValue('B18', '01:30'); +$objPHPExcel->getActiveSheet()->setCellValue('A18', 'Date value #5:'); +$objPHPExcel->getActiveSheet()->setCellValue('B18', '1-Jan'); -$objPHPExcel->getActiveSheet()->setCellValue('A19', 'Time value #2:'); -$objPHPExcel->getActiveSheet()->setCellValue('B19', '01:30:15'); +$objPHPExcel->getActiveSheet()->setCellValue('A19', 'Time value #1:'); +$objPHPExcel->getActiveSheet()->setCellValue('B19', '01:30'); -$objPHPExcel->getActiveSheet()->setCellValue('A20', 'Date/Time value:'); -$objPHPExcel->getActiveSheet()->setCellValue('B20', '19-Dec-1960 01:30'); +$objPHPExcel->getActiveSheet()->setCellValue('A20', 'Time value #2:'); +$objPHPExcel->getActiveSheet()->setCellValue('B20', '01:30:15'); -$objPHPExcel->getActiveSheet()->setCellValue('A21', 'Formula:'); -$objPHPExcel->getActiveSheet()->setCellValue('B21', '=SUM(B2:B9)'); +$objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date/Time value:'); +$objPHPExcel->getActiveSheet()->setCellValue('B21', '19-Dec-1960 01:30'); + +$objPHPExcel->getActiveSheet()->setCellValue('A22', 'Formula:'); +$objPHPExcel->getActiveSheet()->setCellValue('B22', '=SUM(B2:B9)'); // Rename worksheet echo date('H:i:s') , " Rename worksheet" , PHP_EOL; diff --git a/changelog.txt b/changelog.txt index 092b3eb..dea27dd 100644 --- a/changelog.txt +++ b/changelog.txt @@ -79,6 +79,7 @@ Fixed in develop branch: - Feature: (MBaker) Initial version of HTML Reader - Feature: (Progi1984) & (blazzy) Work items 9605 - Implement support for AutoFilter in PHPExcel_Writer_Excel5 - Feature: (MBaker) Modified ERF and ERFC Engineering functions to accept Excel 2010's modified acceptance of negative arguments +- General: (alexgann) Add Currency detection to the Advanced Value Binder - Bugfix: (cyberconte) Patch 12318 - OOCalc cells containing inside the tag - Bugfix: (schir1964) Fix to listWorksheetInfo() method for OOCalc Reader - Bugfix: (MBaker) Support for "e" (epoch) date format mask