Better support for Date custom format (#316)
- To determine if a style should apply a date format, the presence of "applyNumberFormat" attribute on the "cellXfs" section of styles.xml is now optional. We only look at the "numFmtId" attribute (but early return if "applyNumberFormat" is set to "0"). - The format code can contain lowercase AND now uppercase characters as its pattern. - "General" format code used as a custom format is now supported. It seems to be used by a bunch of programs...
This commit is contained in:
1 parent
30aa1b87e2
commit
cc07072cbb
6 files changed
+174
-80
No files matched your search
@@ -11,16 +11,16 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $styleAttributes
|
||||
* @param array|void $styleAttributes
|
||||
* @param array|void $customNumberFormats
|
||||
* @return StyleHelper
|
||||
*/
|
||||
private function getStyleHelperMock($styleAttributes, $customNumberFormats = [])
|
||||
private function getStyleHelperMock($styleAttributes = [], $customNumberFormats = [])
|
||||
{
|
||||
/** @var StyleHelper $styleHelper */
|
||||
$styleHelper = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Helper\StyleHelper')
|
||||
->setConstructorArgs(['/path/to/file.xlsx'])
|
||||
->setMethods(['getCustomNumberFormats', 'getStylesAttributes'])
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$styleHelper->method('getStylesAttributes')->willReturn($styleAttributes);
|
||||
@@ -34,27 +34,17 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithDefaultStyle()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([]);
|
||||
$styleHelper = $this->getStyleHelperMock();
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(0);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenStyleIdNotListed()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([['applyNumberFormat' => true]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenShouldNotApplyNumberFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false]]);
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => false, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
@@ -69,6 +59,26 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithNonDateBuiltinFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWithNoNumFmtId()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => true, 'numFmtId' => null]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -84,6 +94,28 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinDateFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 14]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertTrue($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testShouldFormatNumericValueAsDateWhenApplyNumberFormatNotSetAndUsingBuiltinNonDateFormat()
|
||||
{
|
||||
$styleHelper = $this->getStyleHelperMock([[], ['applyNumberFormat' => null, 'numFmtId' => 9]]);
|
||||
$shouldFormatAsDate = $styleHelper->shouldFormatNumericValueAsDate(1);
|
||||
|
||||
$this->assertFalse($shouldFormatAsDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -106,6 +138,7 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
['[$-409]d\-mmm\-yy;@', true],
|
||||
['[$-409]d\-mmm\-yyyy;@', true],
|
||||
['mm/dd/yy;@', true],
|
||||
['MM/DD/YY;@', true],
|
||||
['[$-F800]dddd\,\ mmmm\ dd\,\ yyyy', true],
|
||||
['m/d;@', true],
|
||||
['m/d/yy;@', true],
|
||||
@@ -117,9 +150,11 @@ class StyleHelperTest extends \PHPUnit_Framework_TestCase
|
||||
['[$-409]m/d/yy\ h:mm\ AM/PM;@', true],
|
||||
['m/d/yy\ h:mm;@', true],
|
||||
['[$-409]mmmmm;@', true],
|
||||
['[$-409]MMmmM;@', true],
|
||||
['[$-409]mmmmm\-yy;@', true],
|
||||
['m/d/yyyy;@', true],
|
||||
['[$-409]m/d/yy\--h:mm;@', true],
|
||||
['General', false],
|
||||
['GENERAL', false],
|
||||
['\ma\yb\e', false],
|
||||
['[Red]foo;', false],
|
||||
|
||||
@@ -279,6 +279,21 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldApplyCustomDateFormatNumberEvenIfApplyNumberFormatNotSpecified()
|
||||
{
|
||||
$shouldFormatDates = true;
|
||||
$allRows = $this->getAllRowsForFile('sheet_with_custom_date_formats_and_no_apply_number_format.xlsx', $shouldFormatDates);
|
||||
|
||||
$expectedRows = [
|
||||
// "General", "GENERAL", "MM/DD/YYYY", "MM/dd/YYYY", "H:MM:SS"
|
||||
['42382', '42382', '01/13/2016', '01/13/2016', '4:43:25'],
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user