-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Excel\Templating\Service\Util; | ||
|
||
/** | ||
* Class SharedStringReader | ||
*/ | ||
class SharedStringReader | ||
{ | ||
public function read(\ZipArchive $zip) | ||
{ | ||
$xml = $zip->getFromName('xl/sharedStrings.xml'); | ||
|
||
$dom = new \DOMDocument; | ||
$dom->loadXML($xml); | ||
$xpath = new \DOMXPath($dom); | ||
$xpath->registerNamespace('s', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); | ||
|
||
$strings = []; | ||
$count = 1; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
foreach ($xpath->query('//s:ssi/s:si/s:t') as $t) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
/** @var \DOMElement $t */ | ||
$strings[$count] = $t->nodeValue; | ||
$count++; | ||
} | ||
|
||
return $strings; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Excel\Templating\Service\Util; | ||
|
||
/** | ||
* Class StringLocator | ||
*/ | ||
class StringLocator | ||
{ | ||
/** | ||
* @var SharedStringReader | ||
*/ | ||
private $reader; | ||
|
||
/** | ||
* @param SharedStringReader $reader | ||
*/ | ||
public function __construct(SharedStringReader $reader) | ||
{ | ||
$this->reader = $reader; | ||
} | ||
|
||
/** | ||
* @param \ZipArchive $zip | ||
* @param string $sheetName | ||
* @return array | ||
*/ | ||
public function getLocation(\ZipArchive $zip, $sheetName) | ||
{ | ||
$sharedStrings = $this->reader->read($zip); | ||
|
||
$sheets = SheetUtil::convertNamesToXmls([$sheetName]); | ||
$xmlPath = $sheets[$sheetName]; | ||
|
||
return $this->makeMap($zip->getFromName($xmlPath), $sharedStrings); | ||
} | ||
|
||
/** | ||
* @param string $xml | ||
* @param array $sharedStrings | ||
* @return array | ||
*/ | ||
private function makeMap($xml, array $sharedStrings) | ||
{ | ||
$dom = new \DOMDocument; | ||
$dom->loadXML($xml); | ||
$xpath = new \DOMXPath($dom); | ||
$xpath->registerNamespace('s', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'); | ||
|
||
$map = []; | ||
foreach ($xpath->query('//s:worksheet/s:sheetData/s:row/s:c[@t=s]/s:v') as $stringCellValue) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
$stringId = $stringCellValue->nodeValue; | ||
if (!isset($sharedStrings[$stringId])) { | ||
continue; | ||
} | ||
|
||
$map[$stringCellValue->parentNode->getAttribute('r')] = $sharedStrings[$stringId]; | ||
} | ||
|
||
return $map; | ||
} | ||
} |
$count must start from 0(not 1)