From db75c5981aaf78c0a6193e2019e86df778a272d8 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 21 Feb 2013 11:29:32 +0000 Subject: [PATCH] Fixes to Advanced Value Binder for cell restructuring --- Classes/PHPExcel/Cell.php | 3 ++- Classes/PHPExcel/Cell/AdvancedValueBinder.php | 18 +++++++++--------- .../PHPExcel/Cell/AdvancedValueBinderTest.php | 16 ++++++++++++++-- .../PHPExcel/Worksheet/AutoFilterTest.php | 6 ++++++ .../Calculation/DateTime/DATEVALUE.data | 8 ++++---- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Classes/PHPExcel/Cell.php b/Classes/PHPExcel/Cell.php index e21674aa..1b944bbc 100644 --- a/Classes/PHPExcel/Cell.php +++ b/Classes/PHPExcel/Cell.php @@ -104,6 +104,7 @@ class PHPExcel_Cell **/ public function notifyCacheController() { $this->_parent->updateCacheData($this); + return $this; } @@ -133,7 +134,7 @@ class PHPExcel_Cell // Set worksheet cache $this->_parent = $pSheet->getCellCacheController(); - + // Set datatype? if ($pDataType !== NULL) { if ($pDataType == PHPExcel_Cell_DataType::TYPE_STRING2) diff --git a/Classes/PHPExcel/Cell/AdvancedValueBinder.php b/Classes/PHPExcel/Cell/AdvancedValueBinder.php index 0b5efec0..9c96a1b9 100644 --- a/Classes/PHPExcel/Cell/AdvancedValueBinder.php +++ b/Classes/PHPExcel/Cell/AdvancedValueBinder.php @@ -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; } diff --git a/unitTests/Classes/PHPExcel/Cell/AdvancedValueBinderTest.php b/unitTests/Classes/PHPExcel/Cell/AdvancedValueBinderTest.php index a76a2b25..0a6ca54d 100644 --- a/unitTests/Classes/PHPExcel/Cell/AdvancedValueBinderTest.php +++ b/unitTests/Classes/PHPExcel/Cell/AdvancedValueBinderTest.php @@ -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); diff --git a/unitTests/Classes/PHPExcel/Worksheet/AutoFilterTest.php b/unitTests/Classes/PHPExcel/Worksheet/AutoFilterTest.php index 6a6d5ae8..9907eabc 100644 --- a/unitTests/Classes/PHPExcel/Worksheet/AutoFilterTest.php +++ b/unitTests/Classes/PHPExcel/Worksheet/AutoFilterTest.php @@ -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, diff --git a/unitTests/rawTestData/Calculation/DateTime/DATEVALUE.data b/unitTests/rawTestData/Calculation/DateTime/DATEVALUE.data index 281c707f..43970c28 100644 --- a/unitTests/rawTestData/Calculation/DateTime/DATEVALUE.data +++ b/unitTests/rawTestData/Calculation/DateTime/DATEVALUE.data @@ -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!"