Skip to content

Commit

Permalink
Support for missing styles XML file in XLSX
Browse files Browse the repository at this point in the history
Some files don't have a "styles.xml" file. Excel supports these files, Spout should do too.
  • Loading branch information
adrilo committed Jul 20, 2019
1 parent 6c4086c commit 1bbfd45
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Spout/Reader/XLSX/Manager/StyleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -76,7 +79,10 @@ public function __construct($filePath, $workbookRelationshipsManager, $entityFac
$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();
}
}

/**
Expand All @@ -88,6 +94,10 @@ public function __construct($filePath, $workbookRelationshipsManager, $entityFac
*/
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.
Expand Down
12 changes: 11 additions & 1 deletion src/Spout/Reader/XLSX/Manager/WorkbookRelationshipsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ public function hasSharedStringsXMLFile()
}

/**
* @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()
{
Expand Down
1 change: 1 addition & 0 deletions tests/Spout/Reader/XLSX/Manager/StyleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private function getStyleManagerMock($styleAttributes = [], $customNumberFormats
{
$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')
Expand Down
13 changes: 13 additions & 0 deletions tests/Spout/Reader/XLSX/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,19 @@ public function testReadShouldSupportEmptySharedString()
$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
*/
Expand Down
Binary file not shown.

0 comments on commit 1bbfd45

Please sign in to comment.