diff --git a/Classes/PHPExcel/Cell/AdvancedValueBinder.php b/Classes/PHPExcel/Cell/AdvancedValueBinder.php index 17a7334f..7e176aef 100644 --- a/Classes/PHPExcel/Cell/AdvancedValueBinder.php +++ b/Classes/PHPExcel/Cell/AdvancedValueBinder.php @@ -66,10 +66,10 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) { // Test for booleans using locale-setting if ($value == PHPExcel_Calculation::getTRUE()) { - $cell->setValueExplicit( True, PHPExcel_Cell_DataType::TYPE_BOOL); + $cell->setValueExplicit( TRUE, PHPExcel_Cell_DataType::TYPE_BOOL); return true; } elseif($value == PHPExcel_Calculation::getFALSE()) { - $cell->setValueExplicit( False, PHPExcel_Cell_DataType::TYPE_BOOL); + $cell->setValueExplicit( FALSE, PHPExcel_Cell_DataType::TYPE_BOOL); return true; } @@ -79,29 +79,54 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder return true; } + // Check for fraction + if (preg_match('/^([+-]?) *([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) { + // Convert value to number + $value = $matches[2] / $matches[3]; + if ($matches[1] == '-') $value = 0 - $value; + $cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); + // Set style + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( '??/??' ); + return true; + } elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) { + // Convert value to number + $value = $matches[2] + ($matches[3] / $matches[4]); + if ($matches[1] == '-') $value = 0 - $value; + $cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); + // Set style + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( '# ??/??' ); + return true; + } + // Check for percentage if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) { // Convert value to number - $cell->setValueExplicit( (float)str_replace('%', '', $value) / 100, PHPExcel_Cell_DataType::TYPE_NUMERIC); + $value = (float) str_replace('%', '', $value) / 100; + $cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); // Set style - $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE ); + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00 ); 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)) { + 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); + $value = (float) trim(str_replace(array($currencyCode,','), '', $value)); + $cell->setValueExplicit( $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)) { + } 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); + $value = (float) trim(str_replace(array('$',','), '', $value)); + $cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); // Set style $cell->getParent()->getStyle( $cell->getCoordinate() ) ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ); @@ -110,23 +135,26 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder // 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)) { + // Convert value to number list($h, $m) = explode(':', $value); $days = $h / 24 + $m / 1440; - // Convert value to number $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC); // Set style - $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 ); + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 ); return true; } // Check for time with seconds '9:45:59', '09:45:59' if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) { + // Convert value to number list($h, $m, $s) = explode(':', $value); $days = $h / 24 + $m / 1440 + $s / 86400; // Convert value to number $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC); // Set style - $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 ); + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 ); return true; } @@ -140,16 +168,18 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder } else { $formatCode = 'yyyy-mm-dd'; } - $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode($formatCode); + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getNumberFormat()->setFormatCode($formatCode); return true; } // Check for newline character "\n" - if (strpos($value, "\n") !== false) { + if (strpos($value, "\n") !== FALSE) { $value = PHPExcel_Shared_String::SanitizeUTF8($value); $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); // Set style - $cell->getParent()->getStyle( $cell->getCoordinate() )->getAlignment()->setWrapText(true); + $cell->getParent()->getStyle( $cell->getCoordinate() ) + ->getAlignment()->setWrapText(TRUE); return true; } } diff --git a/Tests/29advancedvaluebinder.php b/Tests/29advancedvaluebinder.php index 3e34264b..71307e69 100644 --- a/Tests/29advancedvaluebinder.php +++ b/Tests/29advancedvaluebinder.php @@ -98,7 +98,7 @@ $objPHPExcel->getActiveSheet()->setCellValue('A9', 'Numeric value #8:') ->setCellValue('B9', '-1.234e+5'); $objPHPExcel->getActiveSheet()->setCellValue('A10', 'Boolean value:') - ->setCellValue('B10', true); + ->setCellValue('B10', 'TRUE'); $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:') ->setCellValue('B11', '10%'); @@ -106,35 +106,53 @@ $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:') $objPHPExcel->getActiveSheet()->setCellValue('A12', 'Percentage value #2:') ->setCellValue('B12', '12.5%'); -$objPHPExcel->getActiveSheet()->setCellValue('A13', 'Currency value:') - ->setCellValue('B13', '$12345'); +$objPHPExcel->getActiveSheet()->setCellValue('A13', 'Fraction value #1:') + ->setCellValue('B13', '-1/2'); -$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Date value #1:') - ->setCellValue('B14', '21 December 1983'); +$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Fraction value #2:') + ->setCellValue('B14', '3 1/2'); -$objPHPExcel->getActiveSheet()->setCellValue('A15', 'Date value #2:') - ->setCellValue('B15', '19-Dec-1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A15', 'Fraction value #3:') + ->setCellValue('B15', '-12 3/4'); -$objPHPExcel->getActiveSheet()->setCellValue('A16', 'Date value #3:') - ->setCellValue('B16', '19/12/1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A16', 'Fraction value #4:') + ->setCellValue('B16', '13/4'); -$objPHPExcel->getActiveSheet()->setCellValue('A17', 'Date value #4:') - ->setCellValue('B17', '19-12-1960'); +$objPHPExcel->getActiveSheet()->setCellValue('A17', 'Currency value #1:') + ->setCellValue('B17', '$12345'); -$objPHPExcel->getActiveSheet()->setCellValue('A18', 'Date value #5:') - ->setCellValue('B18', '1-Jan'); +$objPHPExcel->getActiveSheet()->setCellValue('A18', 'Currency value #2:') + ->setCellValue('B18', '$12345.67'); -$objPHPExcel->getActiveSheet()->setCellValue('A19', 'Time value #1:') - ->setCellValue('B19', '01:30'); +$objPHPExcel->getActiveSheet()->setCellValue('A19', 'Currency value #3:') + ->setCellValue('B19', '$12,345.67'); -$objPHPExcel->getActiveSheet()->setCellValue('A20', 'Time value #2:') - ->setCellValue('B20', '01:30:15'); +$objPHPExcel->getActiveSheet()->setCellValue('A20', 'Date value #1:') + ->setCellValue('B20', '21 December 1983'); -$objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date/Time value:') - ->setCellValue('B21', '19-Dec-1960 01:30'); +$objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date value #2:') + ->setCellValue('B21', '19-Dec-1960'); -$objPHPExcel->getActiveSheet()->setCellValue('A22', 'Formula:') - ->setCellValue('B22', '=SUM(B2:B9)'); +$objPHPExcel->getActiveSheet()->setCellValue('A22', 'Date value #3:') + ->setCellValue('B22', '07/12/1982'); + +$objPHPExcel->getActiveSheet()->setCellValue('A23', 'Date value #4:') + ->setCellValue('B23', '24-11-1950'); + +$objPHPExcel->getActiveSheet()->setCellValue('A24', 'Date value #5:') + ->setCellValue('B24', '17-Mar'); + +$objPHPExcel->getActiveSheet()->setCellValue('A25', 'Time value #1:') + ->setCellValue('B25', '01:30'); + +$objPHPExcel->getActiveSheet()->setCellValue('A26', 'Time value #2:') + ->setCellValue('B26', '01:30:15'); + +$objPHPExcel->getActiveSheet()->setCellValue('A27', 'Date/Time value:') + ->setCellValue('B27', '19-Dec-1960 01:30'); + +$objPHPExcel->getActiveSheet()->setCellValue('A28', 'Formula:') + ->setCellValue('B28', '=SUM(B2:B9)'); // Rename worksheet echo date('H:i:s') , " Rename worksheet" , EOL; @@ -146,6 +164,11 @@ $objPHPExcel->setActiveSheetIndex(0); // Save Excel 2007 file +echo date('H:i:s') , " Write to Excel2007 format" , EOL; +$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); +$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); +echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; +// Save Excel5 file echo date('H:i:s') , " Write to Excel5 format" , EOL; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save(str_replace('.php', '.xls', __FILE__)); diff --git a/changelog.txt b/changelog.txt index fbcecf99..0fbf7062 100644 --- a/changelog.txt +++ b/changelog.txt @@ -85,6 +85,7 @@ Fixed in develop branch: While Excel 2010 can read CSV files with a simple UTF-8 BOM, Excel2007 and earlier require UTF-16LE encoded tab-separated files. The new setExcelCompatibility(TRUE) option for the CSV Writer will generate files with this formatting for easy import into Excel2007 and below. - Feature: (MBaker) Language implementations for Turkish (tr) +- Feature: (MBaker) Added fraction tests to advanced value binder - General: (alexgann) Add Currency detection to the Advanced Value Binder - General: (MBaker) Work item 18404 - setCellValueExplicitByColumnAndRow() do not return PHPExcel_Worksheet - General: (MBaker) Work item 18324 - Reader factory doesn't read anymore XLTX and XLT files