Feature: Added fraction tests to advanced value binder

This commit is contained in:
Mark Baker 2012-09-18 22:43:09 +01:00
parent d7e4aaf80b
commit 9f042a8540
3 changed files with 89 additions and 35 deletions

View File

@ -66,10 +66,10 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) { if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) {
// Test for booleans using locale-setting // Test for booleans using locale-setting
if ($value == PHPExcel_Calculation::getTRUE()) { if ($value == PHPExcel_Calculation::getTRUE()) {
$cell->setValueExplicit( True, PHPExcel_Cell_DataType::TYPE_BOOL); $cell->setValueExplicit( TRUE, PHPExcel_Cell_DataType::TYPE_BOOL);
return true; return true;
} elseif($value == PHPExcel_Calculation::getFALSE()) { } elseif($value == PHPExcel_Calculation::getFALSE()) {
$cell->setValueExplicit( False, PHPExcel_Cell_DataType::TYPE_BOOL); $cell->setValueExplicit( FALSE, PHPExcel_Cell_DataType::TYPE_BOOL);
return true; return true;
} }
@ -79,29 +79,54 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
return true; 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 // Check for percentage
if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) { if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) {
// Convert value to number // 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 // 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; return true;
} }
// Check for currency // Check for currency
$currencyCode = PHPExcel_Shared_String::getCurrencyCode(); $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 // 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 // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getParent()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( ->getNumberFormat()->setFormatCode(
str_replace('$', $currencyCode, PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ) str_replace('$', $currencyCode, PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE )
); );
return true; 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 // 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 // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getParent()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ); ->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' // 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)) { if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) {
// Convert value to number
list($h, $m) = explode(':', $value); list($h, $m) = explode(':', $value);
$days = $h / 24 + $m / 1440; $days = $h / 24 + $m / 1440;
// Convert value to number
$cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // 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; return true;
} }
// Check for time with seconds '9:45:59', '09:45:59' // 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)) { 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); list($h, $m, $s) = explode(':', $value);
$days = $h / 24 + $m / 1440 + $s / 86400; $days = $h / 24 + $m / 1440 + $s / 86400;
// Convert value to number // Convert value to number
$cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // 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; return true;
} }
@ -140,16 +168,18 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
} else { } else {
$formatCode = 'yyyy-mm-dd'; $formatCode = 'yyyy-mm-dd';
} }
$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode($formatCode); $cell->getParent()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode($formatCode);
return true; return true;
} }
// Check for newline character "\n" // Check for newline character "\n"
if (strpos($value, "\n") !== false) { if (strpos($value, "\n") !== FALSE) {
$value = PHPExcel_Shared_String::SanitizeUTF8($value); $value = PHPExcel_Shared_String::SanitizeUTF8($value);
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )->getAlignment()->setWrapText(true); $cell->getParent()->getStyle( $cell->getCoordinate() )
->getAlignment()->setWrapText(TRUE);
return true; return true;
} }
} }

View File

@ -98,7 +98,7 @@ $objPHPExcel->getActiveSheet()->setCellValue('A9', 'Numeric value #8:')
->setCellValue('B9', '-1.234e+5'); ->setCellValue('B9', '-1.234e+5');
$objPHPExcel->getActiveSheet()->setCellValue('A10', 'Boolean value:') $objPHPExcel->getActiveSheet()->setCellValue('A10', 'Boolean value:')
->setCellValue('B10', true); ->setCellValue('B10', 'TRUE');
$objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:') $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:')
->setCellValue('B11', '10%'); ->setCellValue('B11', '10%');
@ -106,35 +106,53 @@ $objPHPExcel->getActiveSheet()->setCellValue('A11', 'Percentage value #1:')
$objPHPExcel->getActiveSheet()->setCellValue('A12', 'Percentage value #2:') $objPHPExcel->getActiveSheet()->setCellValue('A12', 'Percentage value #2:')
->setCellValue('B12', '12.5%'); ->setCellValue('B12', '12.5%');
$objPHPExcel->getActiveSheet()->setCellValue('A13', 'Currency value:') $objPHPExcel->getActiveSheet()->setCellValue('A13', 'Fraction value #1:')
->setCellValue('B13', '$12345'); ->setCellValue('B13', '-1/2');
$objPHPExcel->getActiveSheet()->setCellValue('A14', 'Date value #1:') $objPHPExcel->getActiveSheet()->setCellValue('A14', 'Fraction value #2:')
->setCellValue('B14', '21 December 1983'); ->setCellValue('B14', '3 1/2');
$objPHPExcel->getActiveSheet()->setCellValue('A15', 'Date value #2:') $objPHPExcel->getActiveSheet()->setCellValue('A15', 'Fraction value #3:')
->setCellValue('B15', '19-Dec-1960'); ->setCellValue('B15', '-12 3/4');
$objPHPExcel->getActiveSheet()->setCellValue('A16', 'Date value #3:') $objPHPExcel->getActiveSheet()->setCellValue('A16', 'Fraction value #4:')
->setCellValue('B16', '19/12/1960'); ->setCellValue('B16', '13/4');
$objPHPExcel->getActiveSheet()->setCellValue('A17', 'Date value #4:') $objPHPExcel->getActiveSheet()->setCellValue('A17', 'Currency value #1:')
->setCellValue('B17', '19-12-1960'); ->setCellValue('B17', '$12345');
$objPHPExcel->getActiveSheet()->setCellValue('A18', 'Date value #5:') $objPHPExcel->getActiveSheet()->setCellValue('A18', 'Currency value #2:')
->setCellValue('B18', '1-Jan'); ->setCellValue('B18', '$12345.67');
$objPHPExcel->getActiveSheet()->setCellValue('A19', 'Time value #1:') $objPHPExcel->getActiveSheet()->setCellValue('A19', 'Currency value #3:')
->setCellValue('B19', '01:30'); ->setCellValue('B19', '$12,345.67');
$objPHPExcel->getActiveSheet()->setCellValue('A20', 'Time value #2:') $objPHPExcel->getActiveSheet()->setCellValue('A20', 'Date value #1:')
->setCellValue('B20', '01:30:15'); ->setCellValue('B20', '21 December 1983');
$objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date/Time value:') $objPHPExcel->getActiveSheet()->setCellValue('A21', 'Date value #2:')
->setCellValue('B21', '19-Dec-1960 01:30'); ->setCellValue('B21', '19-Dec-1960');
$objPHPExcel->getActiveSheet()->setCellValue('A22', 'Formula:') $objPHPExcel->getActiveSheet()->setCellValue('A22', 'Date value #3:')
->setCellValue('B22', '=SUM(B2:B9)'); ->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 // Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL; echo date('H:i:s') , " Rename worksheet" , EOL;
@ -146,6 +164,11 @@ $objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file // 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; echo date('H:i:s') , " Write to Excel5 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__)); $objWriter->save(str_replace('.php', '.xls', __FILE__));

View File

@ -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. 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. 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) 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: (alexgann) Add Currency detection to the Advanced Value Binder
- General: (MBaker) Work item 18404 - setCellValueExplicitByColumnAndRow() do not return PHPExcel_Worksheet - 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 - General: (MBaker) Work item 18324 - Reader factory doesn't read anymore XLTX and XLT files