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() {
$this->_parent->updateCacheData($this);
return $this;
}

View File

@ -86,7 +86,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
if ($matches[1] == '-') $value = 0 - $value;
$cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( '??/??' );
return true;
} 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;
$cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( '# ??/??' );
return true;
}
@ -106,7 +106,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$value = (float) str_replace('%', '', $value) / 100;
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00 );
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));
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode(
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));
$cell->setValueExplicit( $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE );
return true;
}
@ -142,7 +142,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$days = $h / 24 + $m / 1440;
$cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
return true;
}
@ -155,7 +155,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
// Convert value to number
$cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 );
return true;
}
@ -170,7 +170,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
} else {
$formatCode = 'yyyy-mm-dd';
}
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getNumberFormat()->setFormatCode($formatCode);
return true;
}
@ -180,7 +180,7 @@ class PHPExcel_Cell_AdvancedValueBinder extends PHPExcel_Cell_DefaultValueBinder
$value = PHPExcel_Shared_String::SanitizeUTF8($value);
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
// Set style
$cell->getParent()->getStyle( $cell->getCoordinate() )
$cell->getWorksheet()->getStyle( $cell->getCoordinate() )
->getAlignment()->setWrapText(TRUE);
return true;
}

View File

@ -35,7 +35,16 @@ class AdvancedValueBinderTest extends PHPUnit_Framework_TestCase
*/
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())
->method('getStyle')
@ -47,12 +56,15 @@ class AdvancedValueBinderTest extends PHPUnit_Framework_TestCase
->method('setFormatCode')
->with($format)
->will($this->returnSelf());
$sheet->expects($this->any())
->method('getCellCacheController')
->will($this->returnValue($cache));
PHPExcel_Shared_String::setCurrencyCode($currencyCode);
PHPExcel_Shared_String::setDecimalSeparator($decimalSeparator);
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->bindValue($cell, $value);

View File

@ -18,6 +18,12 @@ class AutoFilterTest extends PHPUnit_Framework_TestCase
$this->_mockWorksheetObject = $this->getMockBuilder('PHPExcel_Worksheet')
->disableOriginalConstructor()
->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->_testInitialRange,

View File

@ -35,11 +35,11 @@
"22 August 98", 36029
"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!"
"1 Jan", 40909
"31/12", 41274
"1 Jan", 41275
"31/12", 41639
"12/31", 11658 // Excel reads as 1st December 1931, not 31st December in current year
"5-JUL", 41095
"5 Jul", 41095
"5-JUL", 41460
"5 Jul", 41460
"12/2008", 39783
"10/32", 11963
11, "#VALUE!"