Support for missing styles XML file in XLSX
Some files don't have a "styles.xml" file. Excel supports these files, Spout should do too.
This commit is contained in:
parent
6c4086cf97
commit
72fce3aada
@ -48,7 +48,10 @@ class StyleManager
|
||||
/** @var string Path of the XLSX file being read */
|
||||
protected $filePath;
|
||||
|
||||
/** @var string Path of the styles XML file */
|
||||
/** @var bool Whether the XLSX file contains a styles XML file */
|
||||
protected $hasStylesXMLFile;
|
||||
|
||||
/** @var string|null Path of the styles XML file */
|
||||
protected $stylesXMLFilePath;
|
||||
|
||||
/** @var InternalEntityFactory Factory to create entities */
|
||||
@ -76,7 +79,10 @@ class StyleManager
|
||||
$this->filePath = $filePath;
|
||||
$this->entityFactory = $entityFactory;
|
||||
$this->builtinNumFmtIdIndicatingDates = array_keys(self::$builtinNumFmtIdToNumFormatMapping);
|
||||
$this->stylesXMLFilePath = $workbookRelationshipsManager->getStylesXMLFilePath();
|
||||
$this->hasStylesXMLFile = $workbookRelationshipsManager->hasStylesXMLFile();
|
||||
if ($this->hasStylesXMLFile) {
|
||||
$this->stylesXMLFilePath = $workbookRelationshipsManager->getStylesXMLFilePath();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,6 +94,10 @@ class StyleManager
|
||||
*/
|
||||
public function shouldFormatNumericValueAsDate($styleId)
|
||||
{
|
||||
if (!$this->hasStylesXMLFile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stylesAttributes = $this->getStylesAttributes();
|
||||
|
||||
// Default style (0) does not format numeric values as timestamps. Only custom styles do.
|
||||
|
@ -75,7 +75,17 @@ class WorkbookRelationshipsManager
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null The path of the styles XML file
|
||||
* @return bool Whether the XLSX file contains a styles XML file
|
||||
*/
|
||||
public function hasStylesXMLFile()
|
||||
{
|
||||
$workbookRelationships = $this->getWorkbookRelationships();
|
||||
|
||||
return isset($workbookRelationships[self::RELATIONSHIP_TYPE_STYLES]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The path of the styles XML file
|
||||
*/
|
||||
public function getStylesXMLFilePath()
|
||||
{
|
||||
|
@ -19,6 +19,7 @@ class StyleManagerTest extends TestCase
|
||||
{
|
||||
$entityFactory = $this->createMock(InternalEntityFactory::class);
|
||||
$workbookRelationshipsManager = $this->createMock(WorkbookRelationshipsManager::class);
|
||||
$workbookRelationshipsManager->method('hasStylesXMLFile')->willReturn(true);
|
||||
|
||||
/** @var StyleManager $styleManager */
|
||||
$styleManager = $this->getMockBuilder('\Box\Spout\Reader\XLSX\Manager\StyleManager')
|
||||
|
@ -473,6 +473,19 @@ class ReaderTest extends TestCase
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testReadShouldSupportMissingStylesXMLFile()
|
||||
{
|
||||
$allRows = $this->getAllRowsForFile('file_with_no_styles_in_workbook_xml.xlsx');
|
||||
|
||||
$expectedRows = [
|
||||
['s1--A1', 's1--B1'],
|
||||
];
|
||||
$this->assertEquals($expectedRows, $allRows);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
|
BIN
tests/resources/xlsx/file_with_no_styles_in_workbook_xml.xlsx
Normal file
BIN
tests/resources/xlsx/file_with_no_styles_in_workbook_xml.xlsx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user