|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Exception\IOException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Worksheet |
|
9
|
|
|
* Entity describing a Worksheet |
|
10
|
|
|
*/ |
|
11
|
|
|
class Worksheet |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var string Path to the XML file that will contain the sheet data */ |
|
14
|
|
|
private $filePath; |
|
15
|
|
|
|
|
16
|
|
|
/** @var resource Pointer to the sheet data file (e.g. xl/worksheets/sheet1.xml) */ |
|
17
|
|
|
private $filePointer; |
|
18
|
|
|
|
|
19
|
|
|
/** @var Sheet The "external" sheet */ |
|
20
|
|
|
private $externalSheet; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int Maximum number of columns among all the written rows */ |
|
23
|
|
|
private $maxNumColumns; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int Index of the last written row */ |
|
26
|
|
|
private $lastWrittenRowIndex; |
|
27
|
|
|
|
|
28
|
|
|
private $colWidths; |
|
29
|
|
|
|
|
30
|
|
|
private $defaultColWidth; |
|
31
|
|
|
|
|
32
|
|
|
private $defaultRowHeight; |
|
33
|
|
|
|
|
34
|
|
|
private $merges; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Worksheet constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $worksheetFilePath |
|
40
|
|
|
* @param Sheet $externalSheet |
|
41
|
|
|
*/ |
|
42
|
67 |
|
public function __construct($worksheetFilePath, Sheet $externalSheet) |
|
43
|
|
|
{ |
|
44
|
67 |
|
$this->filePath = $worksheetFilePath; |
|
45
|
67 |
|
$this->filePointer = null; |
|
46
|
67 |
|
$this->externalSheet = $externalSheet; |
|
47
|
67 |
|
$this->maxNumColumns = 0; |
|
48
|
67 |
|
$this->lastWrittenRowIndex = 0; |
|
49
|
67 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
57 |
|
public function getFilePath() |
|
55
|
|
|
{ |
|
56
|
57 |
|
return $this->filePath; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return resource |
|
61
|
|
|
*/ |
|
62
|
60 |
|
public function getFilePointer() |
|
63
|
|
|
{ |
|
64
|
60 |
|
return $this->filePointer; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param resource $filePointer |
|
69
|
|
|
*/ |
|
70
|
55 |
|
public function setFilePointer($filePointer) |
|
71
|
|
|
{ |
|
72
|
55 |
|
$this->filePointer = $filePointer; |
|
73
|
55 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Sheet |
|
77
|
|
|
*/ |
|
78
|
62 |
|
public function getExternalSheet() |
|
79
|
|
|
{ |
|
80
|
62 |
|
return $this->externalSheet; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return int |
|
85
|
|
|
*/ |
|
86
|
56 |
|
public function getMaxNumColumns() |
|
87
|
|
|
{ |
|
88
|
56 |
|
return $this->maxNumColumns; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $maxNumColumns |
|
93
|
|
|
*/ |
|
94
|
53 |
|
public function setMaxNumColumns($maxNumColumns) |
|
95
|
|
|
{ |
|
96
|
53 |
|
$this->maxNumColumns = $maxNumColumns; |
|
97
|
53 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return int |
|
101
|
|
|
*/ |
|
102
|
55 |
|
public function getLastWrittenRowIndex() |
|
103
|
|
|
{ |
|
104
|
55 |
|
return $this->lastWrittenRowIndex; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param int $lastWrittenRowIndex |
|
109
|
|
|
*/ |
|
110
|
53 |
|
public function setLastWrittenRowIndex($lastWrittenRowIndex) |
|
111
|
|
|
{ |
|
112
|
53 |
|
$this->lastWrittenRowIndex = $lastWrittenRowIndex; |
|
113
|
53 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return int The ID of the worksheet |
|
117
|
|
|
*/ |
|
118
|
60 |
|
public function getId() |
|
119
|
|
|
{ |
|
120
|
|
|
// sheet index is zero-based, while ID is 1-based |
|
121
|
60 |
|
return $this->externalSheet->getIndex() + 1; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet |
|
126
|
|
|
* @param string $col in letter format eg A or AC |
|
|
|
|
|
|
127
|
|
|
* @param float $width |
|
128
|
|
|
* @throws IOException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setColWidth(int $colFrom, int $colTo, float $width) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->throwIfSheetFilePointerIsAlreadyCreated(); |
|
133
|
|
|
$this->colWidths[] = [$colFrom,$colTo,$width]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet |
|
138
|
|
|
* @param float $width |
|
139
|
|
|
* @throws IOException |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setDefaultColWidth(float $width) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->throwIfSheetFilePointerIsAlreadyCreated(); |
|
144
|
|
|
$this->defaultColWidth = $width; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* sets default row height --- Must be set before WorksheetManager->startSheet() is called on this sheet |
|
149
|
|
|
* @param float $height |
|
150
|
|
|
* @throws IOException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setDefaultRowHeight(float $height) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->throwIfSheetFilePointerIsAlreadyCreated(); |
|
155
|
|
|
$this->defaultRowHeight = $height; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* merge cells params should be letter and number cell reference eg A3, A5 |
|
160
|
|
|
* @param string $leftCell |
|
161
|
|
|
* @param string $rightCell |
|
162
|
|
|
*/ |
|
163
|
|
|
public function mergeCells(string $leftCell, string $rightCell) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->merges[] = $leftCell . ':' . $rightCell; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* used by WorksheetManager to get default row height and width xml to inject into worksheet xml file |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
27 |
|
public function getDefaultXML() : string |
|
173
|
|
|
{ |
|
174
|
27 |
|
if (empty($this->defaultColWidth) && empty($this->defaultRowHeight)) { |
|
175
|
27 |
|
return ''; |
|
176
|
|
|
} |
|
177
|
|
|
return '<sheetFormatPr' . |
|
178
|
|
|
(empty($this->defaultColWidth) ? '' : ' defaultColWidth="'.$this->defaultColWidth.'"') . |
|
179
|
|
|
(empty($this->defaultRowHeight) ? '' : ' defaultRowHeight="'.$this->defaultRowHeight.'"') . |
|
180
|
|
|
'/>'; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* used by WorksheetManager to get column width references xml to inject into worksheet xml file |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
27 |
|
public function getColWidthXML() |
|
188
|
|
|
{ |
|
189
|
27 |
|
if (empty($this->colWidths)) { |
|
190
|
27 |
|
return ''; |
|
191
|
|
|
} |
|
192
|
|
|
$xml = '<cols>'; |
|
193
|
|
|
foreach ($this->colWidths as $entry) { |
|
194
|
|
|
$xml .= '<col min="'.$entry[0].'" max="'.$entry[1].'" width="'.$entry[2].'" style="1" customWidth="1"/>'; //style and customWidth may be unnecessary ?? |
|
195
|
|
|
} |
|
196
|
|
|
$xml .= '</cols>'; |
|
197
|
|
|
return $xml; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* used by WorksheetManager to get merged cell references xml to inject into worksheet xml file |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
27 |
|
public function getMergeXML() : string |
|
205
|
|
|
{ |
|
206
|
27 |
|
if (empty($this->merges)) { |
|
207
|
27 |
|
return ''; |
|
208
|
|
|
} |
|
209
|
|
|
$xml = '<mergeCells count="'.count($this->merges).'">'; |
|
210
|
|
|
foreach ($this->merges as $merge) { |
|
211
|
|
|
$xml .= '<mergeCell ref="'.$merge.'"/>'; |
|
212
|
|
|
} |
|
213
|
|
|
$xml .= '</mergeCells>'; |
|
214
|
|
|
return $xml; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Checks if the sheet has already been started - throws exception |
|
219
|
|
|
* |
|
220
|
|
|
* @throws IOException If the sheet data file is already opened |
|
221
|
|
|
* @return void |
|
222
|
|
|
*/ |
|
223
|
|
|
private function throwIfSheetFilePointerIsAlreadyCreated() |
|
224
|
|
|
{ |
|
225
|
|
|
if (!empty($this->filePointer)) { |
|
226
|
|
|
throw new IOException('Trying to add default or column width settings after sheet is created'); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.