PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Reader / Xls / ListFunctions.php

ListFunctions.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Reader/Xls/ListFunctions.php

298 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xls;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6 use TablePress\PhpOffice\PhpSpreadsheet\Reader\Xls;
7 use TablePress\PhpOffice\PhpSpreadsheet\Shared\File;
8 use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9 use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10
11 class ListFunctions extends Xls
12 {
13 /**
14 * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
15 *
16 * @return string[]
17 */
18 protected function listWorksheetNames2(string $filename, Xls $xls): array
19 {
20 File::assertFile($filename);
21
22 $worksheetNames = [];
23
24 // Read the OLE file
25 $xls->loadOLE($filename);
26
27 // total byte size of Excel data (workbook global substream + sheet substreams)
28 $xls->dataSize = strlen($xls->data);
29
30 $xls->pos = 0;
31 $xls->sheets = [];
32
33 // Parse Workbook Global Substream
34 while ($xls->pos < $xls->dataSize) {
35 $code = self::getUInt2d($xls->data, $xls->pos);
36
37 switch ($code) {
38 case self::XLS_TYPE_BOF:
39 $xls->readBof();
40 break;
41 case self::XLS_TYPE_SHEET:
42 $xls->readSheet();
43 break;
44 case self::XLS_TYPE_EOF:
45 $xls->readDefault();
46 break;
47 case self::XLS_TYPE_CODEPAGE:
48 $xls->readCodepage();
49 break;
50 default:
51 $xls->readDefault();
52 break;
53 }
54
55 if ($code === self::XLS_TYPE_EOF) {
56 break;
57 }
58 }
59
60 foreach ($xls->sheets as $sheet) {
61 if ($sheet['sheetType'] === 0x00) {
62 // 0x00: Worksheet, 0x02: Chart, 0x06: Visual Basic module
63 $worksheetNames[] = $sheet['name'];
64 }
65 }
66
67 return $worksheetNames;
68 }
69
70 /**
71 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
72 *
73 * @return array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}>
74 */
75 protected function listWorksheetInfo2(string $filename, Xls $xls): array
76 {
77 File::assertFile($filename);
78
79 $worksheetInfo = [];
80
81 // Read the OLE file
82 $xls->loadOLE($filename);
83
84 // total byte size of Excel data (workbook global substream + sheet substreams)
85 $xls->dataSize = strlen($xls->data);
86
87 // initialize
88 $xls->pos = 0;
89 $xls->sheets = [];
90
91 // Parse Workbook Global Substream
92 while ($xls->pos < $xls->dataSize) {
93 $code = self::getUInt2d($xls->data, $xls->pos);
94
95 switch ($code) {
96 case self::XLS_TYPE_BOF:
97 $xls->readBof();
98 break;
99 case self::XLS_TYPE_SHEET:
100 $xls->readSheet();
101 break;
102 case self::XLS_TYPE_EOF:
103 $xls->readDefault();
104 break;
105 case self::XLS_TYPE_CODEPAGE:
106 $xls->readCodepage();
107 break;
108 default:
109 $xls->readDefault();
110 break;
111 }
112
113 if ($code === self::XLS_TYPE_EOF) {
114 break;
115 }
116 }
117
118 // Parse the individual sheets
119 foreach ($xls->sheets as $sheet) {
120 if ($sheet['sheetType'] !== 0x00) {
121 // 0x00: Worksheet
122 // 0x02: Chart
123 // 0x06: Visual Basic module
124 continue;
125 }
126
127 $tmpInfo = [];
128 $tmpInfo['worksheetName'] = StringHelper::convertToString($sheet['name']);
129 $tmpInfo['lastColumnLetter'] = 'A';
130 $tmpInfo['lastColumnIndex'] = 0;
131 $tmpInfo['totalRows'] = 0;
132 $tmpInfo['totalColumns'] = 0;
133 $tmpInfo['sheetState'] = StringHelper::convertToString($sheet['sheetState']);
134
135 $xls->pos = $sheet['offset'];
136
137 while ($xls->pos <= $xls->dataSize - 4) {
138 $code = self::getUInt2d($xls->data, $xls->pos);
139
140 switch ($code) {
141 case self::XLS_TYPE_RK:
142 case self::XLS_TYPE_LABELSST:
143 case self::XLS_TYPE_NUMBER:
144 case self::XLS_TYPE_FORMULA:
145 case self::XLS_TYPE_BOOLERR:
146 case self::XLS_TYPE_LABEL:
147 case self::XLS_TYPE_MULRK:
148 $length = self::getUInt2d($xls->data, $xls->pos + 2);
149 $recordData = $xls->readRecordData($xls->data, $xls->pos + 4, $length);
150
151 // move stream pointer to next record
152 $xls->pos += 4 + $length;
153
154 $rowIndex = self::getUInt2d($recordData, 0) + 1;
155 if ($code === self::XLS_TYPE_MULRK) {
156 $columnIndex = self::getUInt2d($recordData, $length - 2);
157 } else {
158 $columnIndex = self::getUInt2d($recordData, 2);
159 }
160
161 $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
162 $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
163
164 break;
165 case self::XLS_TYPE_BOF:
166 $xls->readBof();
167
168 break;
169 case self::XLS_TYPE_EOF:
170 $xls->readDefault();
171
172 break 2;
173 default:
174 $xls->readDefault();
175
176 break;
177 }
178 }
179
180 $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1, true);
181 $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
182
183 $worksheetInfo[] = $tmpInfo;
184 }
185
186 return $worksheetInfo;
187 }
188
189 /**
190 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
191 *
192 * @return array<int, array{worksheetName: string, dimensionsMinR: int, dimensionsMinC: int, dimensionsMaxR: int, dimensionsMaxC: int, lastColumnLetter: string}>
193 */
194 protected function listWorksheetDimensions2(string $filename, Xls $xls): array
195 {
196 File::assertFile($filename);
197
198 $worksheetInfo = [];
199
200 // Read the OLE file
201 $xls->loadOLE($filename);
202
203 // total byte size of Excel data (workbook global substream + sheet substreams)
204 $xls->dataSize = strlen($xls->data);
205
206 // initialize
207 $xls->pos = 0;
208 $xls->sheets = [];
209
210 // Parse Workbook Global Substream
211 while ($xls->pos < $xls->dataSize) {
212 $code = self::getUInt2d($xls->data, $xls->pos);
213
214 switch ($code) {
215 case self::XLS_TYPE_BOF:
216 $xls->readBof();
217 break;
218 case self::XLS_TYPE_SHEET:
219 $xls->readSheet();
220 break;
221 case self::XLS_TYPE_EOF:
222 $xls->readDefault();
223 break;
224 case self::XLS_TYPE_CODEPAGE:
225 $xls->readCodepage();
226 break;
227 default:
228 $xls->readDefault();
229 break;
230 }
231
232 if ($code === self::XLS_TYPE_EOF) {
233 break;
234 }
235 }
236
237 // Parse the individual sheets
238 foreach ($xls->sheets as $sheet) {
239 if ($sheet['sheetType'] !== 0x00) {
240 // 0x00: Worksheet
241 // 0x02: Chart
242 // 0x06: Visual Basic module
243 continue;
244 }
245
246 $tmpInfo = [];
247 $tmpInfo['worksheetName'] = StringHelper::convertToString($sheet['name']);
248 $tmpInfo['dimensionsMinR'] = -1;
249 $tmpInfo['dimensionsMaxR'] = -1;
250 $tmpInfo['dimensionsMinC'] = -1;
251 $tmpInfo['dimensionsMaxC'] = -1;
252 $tmpInfo['lastColumnLetter'] = '';
253
254 $xls->pos = $sheet['offset'];
255
256 while ($xls->pos <= $xls->dataSize - 4) {
257 $code = self::getUInt2d($xls->data, $xls->pos);
258
259 switch ($code) {
260 case self::XLS_TYPE_BOF:
261 $xls->readBof();
262
263 break;
264 case self::XLS_TYPE_EOF:
265 $xls->readDefault();
266
267 break 2;
268 case self::XLS_TYPE_DIMENSION:
269 $length = self::getUInt2d($xls->data, $xls->pos + 2);
270 if ($length === 14) {
271 $dimensionsData = (string) substr($xls->data, $xls->pos + 4, $length);
272 $data = unpack('VrwMic/VrwMac/vcolMic/vcolMac/vreserved', $dimensionsData);
273 if (is_array($data)) {
274 /** @var int[] $data */
275 $tmpInfo['dimensionsMinR'] = $data['rwMic'];
276 $tmpInfo['dimensionsMaxR'] = $data['rwMac'];
277 $tmpInfo['dimensionsMinC'] = $data['colMic'];
278 $tmpInfo['dimensionsMaxC'] = $data['colMac'];
279 $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['dimensionsMaxC'], true);
280 }
281 }
282 $xls->readDefault();
283
284 break;
285 default:
286 $xls->readDefault();
287
288 break;
289 }
290 }
291
292 $worksheetInfo[] = $tmpInfo;
293 }
294
295 return $worksheetInfo;
296 }
297 }
298