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:
Adrien Loison 2019-07-20 16:44:10 +02:00 committed by Adrien Loison
parent 6c4086cf97
commit 1bbfd45b82
5 changed files with 37 additions and 3 deletions

View File

@ -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.

View File

@ -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()
{

View File

@ -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')

View File

@ -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
*/