blob: 240bb3b1a7d0270c12101dadc42e1710348bd8c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php
namespace PHPExcelFixer\StyleFixer\Util;
class Book
{
/**
* 人間可読なシート名 => シートのリレーションファイルのパス の連想配列を作る
*
* @param \ZipArchive $zip
* @return array
*/
public function makeSheetRelationMap(\ZipArchive $zip)
{
$sheetMap = $this->makeSheetMap($zip);
array_walk($sheetMap, function(&$xmlPath){
$xmlPath = str_replace('worksheets/', 'worksheets/_rels/', $xmlPath).'.rels';
});
return $sheetMap;
}
/**
* 人間可読なシート名 => シートファイルのパス の連想配列を作る
*
* @param \ZipArchive $zip
* @return array
*/
public function makeSheetMap(\ZipArchive $zip)
{
$relXml = $zip->getFromName('xl/_rels/workbook.xml.rels');
$relDom = new \DOMDocument;
$relDom->loadXml($relXml);
$relXPath = new \DOMXPath($relDom);
$relXPath->registerNamespace('r', XmlNamespace::RELATIONSHIPS_NS_URL);
$sheetRelationIdMaps = $this->makeSheetRelationIdMap($zip);
$map = [];
foreach ($sheetRelationIdMaps as $sheetName => $relId) {
/** @var null|\DOMElement $relEntry */
$relEntry = $relXPath->query('//r:Relationships/r:Relationship[@Id="'.$relId.'"]')->item(0);
if ($relEntry) {
$map[$sheetName] = 'xl/'.$relEntry->getAttribute('Target');
}
}
return $map;
}
/**
* 人間可読なシート名 => relationship id の連想配列を作る
* @param \ZipArchive $zip
* @return array
*/
private function makeSheetRelationIdMap(\ZipArchive $zip)
{
$xml = $zip->getFromName('xl/workbook.xml');
$dom = new \DOMDocument;
$dom->loadXML($xml);
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
$map = [];
foreach ($xpath->query('//s:workbook/s:sheets/s:sheet') as $sheet) {
/** @var \DOMElement $sheet */
$map[$sheet->getAttribute('name')] = $sheet->getAttribute('r:id');
}
return $map;
}
/**
* 人間可読なシート名 => 印刷範囲 の連想配列を作る
*
* @param \ZipArchive $zip
* @return array
*/
public function makePrintAreaMap(\ZipArchive $zip)
{
$xml = $zip->getFromName('xl/workbook.xml');
$dom = new \DOMDocument;
$dom->loadXML($xml);
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
$sheets = $xpath->query('//s:workbook/s:sheets/s:sheet');
$printAreas = $xpath->query('//s:workbook/s:definedNames/s:definedName[@name="_xlnm.Print_Area"]');
$map = [];
$localSheetId = 0;
foreach ($sheets as $sheet) {
/** @var \DOMElement $definedName */
$definedName = $printAreas->item($localSheetId);
if ($definedName) {
/** @var \DOMElement $sheet */
$map[$sheet->getAttribute('name')] = $definedName->nodeValue;
}
$localSheetId++;
}
return $map;
}
/**
* 人間可読なシート名 => drawing.xmlファイルの配列 の連想配列を作る
*
* @param \ZipArchive $zip
* @return array
*/
public function makeDrawingMap(\ZipArchive $zip)
{
$map = [];
$sheetRelationMap = $this->makeSheetRelationMap($zip);
foreach ($sheetRelationMap as $sheetName => $sheetRelFile) {
$drawings = [];
$relXml = $zip->getFromName($sheetRelFile);
$dom = new \DOMDocument();
$dom->loadXML($relXml);
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('r', XmlNamespace::RELATIONSHIPS_NS_URL);
$drawingFiles = $xpath->query('//r:Relationships/r:Relationship[@Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"]');
foreach ($drawingFiles as $drawingFile) {
/** @var \DOMElement $drawingFile */
$drawings[] = str_replace('../', 'xl/', $drawingFile->getAttribute('Target'));
}
$xpath = null;
$dom = null;
if (0 !== count($drawings)) {
$map[$sheetName] = $drawings;
}
}
return $map;
}
}
|