Fixes to Advanced Value Binder for cell restructuring

This commit is contained in:
Mark Baker 2013-02-21 11:29:32 +00:00
parent 76ba3cbcd3
commit db75c5981a
5 changed files with 35 additions and 16 deletions

View File

@ -104,6 +104,7 @@ class PHPExcel_Cell
**/ **/
public function notifyCacheController() { public function notifyCacheController() {
$this->_parent->updateCacheData($this); $this->_parent->updateCacheData($this);
return $this; return $this;
} }
@ -133,7 +134,7 @@ class PHPExcel_Cell
// Set worksheet cache // Set worksheet cache
$this->_parent = $pSheet->getCellCacheController(); $this->_parent = $pSheet->getCellCacheController();
// Set datatype? // Set datatype?
if ($pDataType !== NULL) { if ($pDataType !== NULL) {
if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2) if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2)

View File

@ -86,7 +86,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
if ($matches[1] == '-') $value = 0 - $value; if ($matches[1] == '-') $value = 0 - $value;
$cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( '??/??' ); ->getNumberFormat()->setFormatCode( '??/??' );
return true; return true;
} elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) { } elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) {
@ -95,7 +95,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
if ($matches[1] == '-') $value = 0 - $value; if ($matches[1] == '-') $value = 0 - $value;
$cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( '# ??/??' ); ->getNumberFormat()->setFormatCode( '# ??/??' );
return true; return true;
} }
@ -106,7 +106,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$value = (float) str_replace('%', '', $value) / 100; $value = (float) str_replace('%', '', $value) / 100;
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00 ); ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00 );
return true; return true;
} }
@ -120,7 +120,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$value = (float) trim(str_replace(array($currencyCode, $thousandsSeparator, $decimalSeparator), array('', '', '.'), $value)); $value = (float) trim(str_replace(array($currencyCode, $thousandsSeparator, $decimalSeparator), array('', '', '.'), $value));
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->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 )
); );
@ -130,7 +130,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$value = (float) trim(str_replace(array('$',','), '', $value)); $value = (float) trim(str_replace(array('$',','), '', $value));
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC); $cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style // Set style
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE ); ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE );
return true; return true;
} }
@ -142,7 +142,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$days = $h / 24 + $m / 1440; $days = $h / 24 + $m / 1440;
$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() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 ); ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
return true; return true;
} }
@ -155,7 +155,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
// 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() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 ); ->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 );
return true; return true;
} }
@ -170,7 +170,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
} else { } else {
$formatCode = 'yyyy-mm-dd'; $formatCode = 'yyyy-mm-dd';
} }
$cell->getParent()->getStyle( $cell->getCoordinate() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode($formatCode); ->getNumberFormat()->setFormatCode($formatCode);
return true; return true;
} }
@ -180,7 +180,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$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() ) $cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getAlignment()->setWrapText(TRUE); ->getAlignment()->setWrapText(TRUE);
return true; return true;
} }

View File

@ -35,7 +35,16 @@ class AdvancedValueBinderTest extends PHPUnit_Framework_TestCase
*/ */
public function testCurrency($value, $valueBinded, $format, $thousandsSeparator, $decimalSeparator, $currencyCode) public function testCurrency($value, $valueBinded, $format, $thousandsSeparator, $decimalSeparator, $currencyCode)
{ {
$sheet = $this->getMock('PHPExcel_Worksheet', array('getStyle', 'getNumberFormat', 'setFormatCode')); $sheet = $this->getMock(
'PHPExcel_Worksheet',
array('getStyle', 'getNumberFormat', 'setFormatCode','getCellCacheController')
);
$cache = $this->getMockBuilder('PHPExcel_CachedObjectStorage_Memory')
->disableOriginalConstructor()
->getMock();
$cache->expects($this->any())
->method('getParent')
->will($this->returnValue($sheet));
$sheet->expects($this->once()) $sheet->expects($this->once())
->method('getStyle') ->method('getStyle')
@ -47,12 +56,15 @@ class AdvancedValueBinderTest extends PHPUnit_Framework_TestCase
->method('setFormatCode') ->method('setFormatCode')
->with($format) ->with($format)
->will($this->returnSelf()); ->will($this->returnSelf());
$sheet->expects($this->any())
->method('getCellCacheController')
->will($this->returnValue($cache));
PHPExcel_Shared_String::setCurrencyCode($currencyCode); PHPExcel_Shared_String::setCurrencyCode($currencyCode);
PHPExcel_Shared_String::setDecimalSeparator($decimalSeparator); PHPExcel_Shared_String::setDecimalSeparator($decimalSeparator);
PHPExcel_Shared_String::setThousandsSeparator($thousandsSeparator); PHPExcel_Shared_String::setThousandsSeparator($thousandsSeparator);
$cell = new PHPExcel_Cell('A', 1, null, PHPExcel_Cell_DataType::TYPE_STRING, $sheet); $cell = new PHPExcel_Cell(NULL, PHPExcel_Cell_DataType::TYPE_STRING, $sheet);
$binder = new PHPExcel_Cell_AdvancedValueBinder(); $binder = new PHPExcel_Cell_AdvancedValueBinder();
$binder->bindValue($cell, $value); $binder->bindValue($cell, $value);

View File

@ -18,6 +18,12 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
$this->_mockWorksheetObject = $this->getMockBuilder('PHPExcel_Worksheet') $this->_mockWorksheetObject = $this->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->_mockCacheController = $this->getMockBuilder('PHPExcel_CachedObjectStorage_Memory')
->disableOriginalConstructor()
->getMock();
$this->_mockWorksheetObject->expects($this->any())
->method('getCellCacheController')
->will($this->returnValue($this->_mockCacheController));
$this->_testAutoFilterObject = new PHPExcel_Worksheet_AutoFilter( $this->_testAutoFilterObject = new PHPExcel_Worksheet_AutoFilter(
$this->_testInitialRange, $this->_testInitialRange,

View File

@ -35,11 +35,11 @@
"22 August 98", 36029 "22 August 98", 36029
"1st March 2007", 39142 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date "1st March 2007", 39142 // MS Excel will fail with a #VALUE return, but PHPExcel can parse this date
"The 1st day of March 2007", "#VALUE!" "The 1st day of March 2007", "#VALUE!"
"1 Jan", 40909 "1 Jan", 41275
"31/12", 41274 "31/12", 41639
"12/31", 11658 // Excel reads as 1st December 1931, not 31st December in current year "12/31", 11658 // Excel reads as 1st December 1931, not 31st December in current year
"5-JUL", 41095 "5-JUL", 41460
"5 Jul", 41095 "5 Jul", 41460
"12/2008", 39783 "12/2008", 39783
"10/32", 11963 "10/32", 11963
11, "#VALUE!" 11, "#VALUE!"