|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use Composer\Pcre\Preg; |
|
7
|
|
|
use Generator; |
|
8
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
|
9
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
|
10
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AddressRange; |
|
11
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Cell; |
|
12
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\CellAddress; |
|
13
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
|
14
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
|
15
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataValidation; |
|
16
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink; |
|
17
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder; |
|
18
|
|
|
use PhpOffice\PhpSpreadsheet\Chart\Chart; |
|
19
|
|
|
use PhpOffice\PhpSpreadsheet\Collection\Cells; |
|
20
|
|
|
use PhpOffice\PhpSpreadsheet\Collection\CellsFactory; |
|
21
|
|
|
use PhpOffice\PhpSpreadsheet\Comment; |
|
22
|
|
|
use PhpOffice\PhpSpreadsheet\DefinedName; |
|
23
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
|
24
|
|
|
use PhpOffice\PhpSpreadsheet\ReferenceHelper; |
|
25
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText; |
|
26
|
|
|
use PhpOffice\PhpSpreadsheet\Shared; |
|
27
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
|
28
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
|
29
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
|
30
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
|
31
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
|
32
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
|
33
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Protection as StyleProtection; |
|
34
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
|
35
|
|
|
|
|
36
|
|
|
class Worksheet |
|
37
|
|
|
{ |
|
38
|
|
|
// Break types |
|
39
|
|
|
public const BREAK_NONE = 0; |
|
40
|
|
|
public const BREAK_ROW = 1; |
|
41
|
|
|
public const BREAK_COLUMN = 2; |
|
42
|
|
|
// Maximum column for row break |
|
43
|
|
|
public const BREAK_ROW_MAX_COLUMN = 16383; |
|
44
|
|
|
|
|
45
|
|
|
// Sheet state |
|
46
|
|
|
public const SHEETSTATE_VISIBLE = 'visible'; |
|
47
|
|
|
public const SHEETSTATE_HIDDEN = 'hidden'; |
|
48
|
|
|
public const SHEETSTATE_VERYHIDDEN = 'veryHidden'; |
|
49
|
|
|
|
|
50
|
|
|
public const MERGE_CELL_CONTENT_EMPTY = 'empty'; |
|
51
|
|
|
public const MERGE_CELL_CONTENT_HIDE = 'hide'; |
|
52
|
|
|
public const MERGE_CELL_CONTENT_MERGE = 'merge'; |
|
53
|
|
|
|
|
54
|
|
|
public const FUNCTION_LIKE_GROUPBY = '/\b(groupby|_xleta)\b/i'; // weird new syntax |
|
55
|
|
|
|
|
56
|
|
|
protected const SHEET_NAME_REQUIRES_NO_QUOTES = '/^[_\p{L}][_\p{L}\p{N}]*$/mui'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Maximum 31 characters allowed for sheet title. |
|
60
|
|
|
* |
|
61
|
|
|
* @var int |
|
62
|
|
|
*/ |
|
63
|
|
|
const SHEET_TITLE_MAXIMUM_LENGTH = 31; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Invalid characters in sheet title. |
|
67
|
|
|
*/ |
|
68
|
|
|
private const INVALID_CHARACTERS = ['*', ':', '/', '\\', '?', '[', ']']; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Parent spreadsheet. |
|
72
|
|
|
*/ |
|
73
|
|
|
private ?Spreadsheet $parent = null; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Collection of cells. |
|
77
|
|
|
*/ |
|
78
|
|
|
private Cells $cellCollection; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Collection of row dimensions. |
|
82
|
|
|
* |
|
83
|
|
|
* @var RowDimension[] |
|
84
|
|
|
*/ |
|
85
|
|
|
private array $rowDimensions = []; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Default row dimension. |
|
89
|
|
|
*/ |
|
90
|
|
|
private RowDimension $defaultRowDimension; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Collection of column dimensions. |
|
94
|
|
|
* |
|
95
|
|
|
* @var ColumnDimension[] |
|
96
|
|
|
*/ |
|
97
|
|
|
private array $columnDimensions = []; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Default column dimension. |
|
101
|
|
|
*/ |
|
102
|
|
|
private ColumnDimension $defaultColumnDimension; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Collection of drawings. |
|
106
|
|
|
* |
|
107
|
|
|
* @var ArrayObject<int, BaseDrawing> |
|
108
|
|
|
*/ |
|
109
|
|
|
private ArrayObject $drawingCollection; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Collection of Chart objects. |
|
113
|
|
|
* |
|
114
|
|
|
* @var ArrayObject<int, Chart> |
|
115
|
|
|
*/ |
|
116
|
|
|
private ArrayObject $chartCollection; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Collection of Table objects. |
|
120
|
|
|
* |
|
121
|
|
|
* @var ArrayObject<int, Table> |
|
122
|
|
|
*/ |
|
123
|
|
|
private ArrayObject $tableCollection; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Worksheet title. |
|
127
|
|
|
*/ |
|
128
|
|
|
private string $title = ''; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sheet state. |
|
132
|
|
|
*/ |
|
133
|
|
|
private string $sheetState; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Page setup. |
|
137
|
|
|
*/ |
|
138
|
|
|
private PageSetup $pageSetup; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Page margins. |
|
142
|
|
|
*/ |
|
143
|
|
|
private PageMargins $pageMargins; |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Page header/footer. |
|
147
|
|
|
*/ |
|
148
|
|
|
private HeaderFooter $headerFooter; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Sheet view. |
|
152
|
|
|
*/ |
|
153
|
|
|
private SheetView $sheetView; |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Protection. |
|
157
|
|
|
*/ |
|
158
|
|
|
private Protection $protection; |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Conditional styles. Indexed by cell coordinate, e.g. 'A1'. |
|
162
|
|
|
* |
|
163
|
|
|
* @var Conditional[][] |
|
164
|
|
|
*/ |
|
165
|
|
|
private array $conditionalStylesCollection = []; |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Collection of row breaks. |
|
169
|
|
|
* |
|
170
|
|
|
* @var PageBreak[] |
|
171
|
|
|
*/ |
|
172
|
|
|
private array $rowBreaks = []; |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Collection of column breaks. |
|
176
|
|
|
* |
|
177
|
|
|
* @var PageBreak[] |
|
178
|
|
|
*/ |
|
179
|
|
|
private array $columnBreaks = []; |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Collection of merged cell ranges. |
|
183
|
|
|
* |
|
184
|
|
|
* @var string[] |
|
185
|
|
|
*/ |
|
186
|
|
|
private array $mergeCells = []; |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Collection of protected cell ranges. |
|
190
|
|
|
* |
|
191
|
|
|
* @var ProtectedRange[] |
|
192
|
|
|
*/ |
|
193
|
|
|
private array $protectedCells = []; |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Autofilter Range and selection. |
|
197
|
|
|
*/ |
|
198
|
|
|
private AutoFilter $autoFilter; |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Freeze pane. |
|
202
|
|
|
*/ |
|
203
|
|
|
private ?string $freezePane = null; |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Default position of the right bottom pane. |
|
207
|
|
|
*/ |
|
208
|
|
|
private ?string $topLeftCell = null; |
|
209
|
|
|
|
|
210
|
|
|
private string $paneTopLeftCell = ''; |
|
211
|
|
|
|
|
212
|
|
|
private string $activePane = ''; |
|
213
|
|
|
|
|
214
|
|
|
private int $xSplit = 0; |
|
215
|
|
|
|
|
216
|
|
|
private int $ySplit = 0; |
|
217
|
|
|
|
|
218
|
|
|
private string $paneState = ''; |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Properties of the 4 panes. |
|
222
|
|
|
* |
|
223
|
|
|
* @var (null|Pane)[] |
|
224
|
|
|
*/ |
|
225
|
|
|
private array $panes = [ |
|
226
|
|
|
'bottomRight' => null, |
|
227
|
|
|
'bottomLeft' => null, |
|
228
|
|
|
'topRight' => null, |
|
229
|
|
|
'topLeft' => null, |
|
230
|
|
|
]; |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Show gridlines? |
|
234
|
|
|
*/ |
|
235
|
|
|
private bool $showGridlines = true; |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Print gridlines? |
|
239
|
|
|
*/ |
|
240
|
|
|
private bool $printGridlines = false; |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Show row and column headers? |
|
244
|
|
|
*/ |
|
245
|
|
|
private bool $showRowColHeaders = true; |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Show summary below? (Row/Column outline). |
|
249
|
|
|
*/ |
|
250
|
|
|
private bool $showSummaryBelow = true; |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Show summary right? (Row/Column outline). |
|
254
|
|
|
*/ |
|
255
|
|
|
private bool $showSummaryRight = true; |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Collection of comments. |
|
259
|
|
|
* |
|
260
|
|
|
* @var Comment[] |
|
261
|
|
|
*/ |
|
262
|
|
|
private array $comments = []; |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Active cell. (Only one!). |
|
266
|
|
|
*/ |
|
267
|
|
|
private string $activeCell = 'A1'; |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Selected cells. |
|
271
|
|
|
*/ |
|
272
|
|
|
private string $selectedCells = 'A1'; |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Cached highest column. |
|
276
|
|
|
*/ |
|
277
|
|
|
private int $cachedHighestColumn = 1; |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Cached highest row. |
|
281
|
|
|
*/ |
|
282
|
|
|
private int $cachedHighestRow = 1; |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Right-to-left? |
|
286
|
|
|
*/ |
|
287
|
|
|
private bool $rightToLeft = false; |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Hyperlinks. Indexed by cell coordinate, e.g. 'A1'. |
|
291
|
|
|
* |
|
292
|
|
|
* @var Hyperlink[] |
|
293
|
|
|
*/ |
|
294
|
|
|
private array $hyperlinkCollection = []; |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Data validation objects. Indexed by cell coordinate, e.g. 'A1'. |
|
298
|
|
|
* Index can include ranges, and multiple cells/ranges. |
|
299
|
|
|
* |
|
300
|
|
|
* @var DataValidation[] |
|
301
|
|
|
*/ |
|
302
|
|
|
private array $dataValidationCollection = []; |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Tab color. |
|
306
|
|
|
*/ |
|
307
|
|
|
private ?Color $tabColor = null; |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Hash. |
|
311
|
|
|
*/ |
|
312
|
|
|
private int $hash; |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* CodeName. |
|
316
|
|
|
*/ |
|
317
|
|
|
private ?string $codeName = null; |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Create a new worksheet. |
|
321
|
|
|
*/ |
|
322
|
10656 |
|
public function __construct(?Spreadsheet $parent = null, string $title = 'Worksheet') |
|
323
|
|
|
{ |
|
324
|
|
|
// Set parent and title |
|
325
|
10656 |
|
$this->parent = $parent; |
|
326
|
10656 |
|
$this->hash = spl_object_id($this); |
|
327
|
10656 |
|
$this->setTitle($title, false); |
|
328
|
|
|
// setTitle can change $pTitle |
|
329
|
10656 |
|
$this->setCodeName($this->getTitle()); |
|
330
|
10656 |
|
$this->setSheetState(self::SHEETSTATE_VISIBLE); |
|
331
|
|
|
|
|
332
|
10656 |
|
$this->cellCollection = CellsFactory::getInstance($this); |
|
333
|
|
|
// Set page setup |
|
334
|
10656 |
|
$this->pageSetup = new PageSetup(); |
|
335
|
|
|
// Set page margins |
|
336
|
10656 |
|
$this->pageMargins = new PageMargins(); |
|
337
|
|
|
// Set page header/footer |
|
338
|
10656 |
|
$this->headerFooter = new HeaderFooter(); |
|
339
|
|
|
// Set sheet view |
|
340
|
10656 |
|
$this->sheetView = new SheetView(); |
|
341
|
|
|
// Drawing collection |
|
342
|
10656 |
|
$this->drawingCollection = new ArrayObject(); |
|
343
|
|
|
// Chart collection |
|
344
|
10656 |
|
$this->chartCollection = new ArrayObject(); |
|
345
|
|
|
// Protection |
|
346
|
10656 |
|
$this->protection = new Protection(); |
|
347
|
|
|
// Default row dimension |
|
348
|
10656 |
|
$this->defaultRowDimension = new RowDimension(null); |
|
349
|
|
|
// Default column dimension |
|
350
|
10656 |
|
$this->defaultColumnDimension = new ColumnDimension(null); |
|
351
|
|
|
// AutoFilter |
|
352
|
10656 |
|
$this->autoFilter = new AutoFilter('', $this); |
|
353
|
|
|
// Table collection |
|
354
|
10656 |
|
$this->tableCollection = new ArrayObject(); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Disconnect all cells from this Worksheet object, |
|
359
|
|
|
* typically so that the worksheet object can be unset. |
|
360
|
|
|
*/ |
|
361
|
9702 |
|
public function disconnectCells(): void |
|
362
|
|
|
{ |
|
363
|
9702 |
|
if (isset($this->cellCollection)) { //* @phpstan-ignore-line |
|
364
|
9702 |
|
$this->cellCollection->unsetWorksheetCells(); |
|
365
|
9702 |
|
unset($this->cellCollection); |
|
366
|
|
|
} |
|
367
|
|
|
// detach ourself from the workbook, so that it can then delete this worksheet successfully |
|
368
|
9702 |
|
$this->parent = null; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
/** |
|
372
|
|
|
* Code to execute when this worksheet is unset(). |
|
373
|
|
|
*/ |
|
374
|
133 |
|
public function __destruct() |
|
375
|
|
|
{ |
|
376
|
133 |
|
Calculation::getInstanceOrNull($this->parent) |
|
377
|
133 |
|
?->clearCalculationCacheForWorksheet($this->title); |
|
378
|
|
|
|
|
379
|
133 |
|
$this->disconnectCells(); |
|
380
|
133 |
|
unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->drawingCollection, $this->chartCollection, $this->autoFilter); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
9 |
|
public function __wakeup(): void |
|
384
|
|
|
{ |
|
385
|
9 |
|
$this->hash = spl_object_id($this); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* Return the cell collection. |
|
390
|
|
|
*/ |
|
391
|
10300 |
|
public function getCellCollection(): Cells |
|
392
|
|
|
{ |
|
393
|
10300 |
|
return $this->cellCollection; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Get array of invalid characters for sheet title. |
|
398
|
|
|
* |
|
399
|
|
|
* @return string[] |
|
400
|
|
|
*/ |
|
401
|
1 |
|
public static function getInvalidCharacters(): array |
|
402
|
|
|
{ |
|
403
|
1 |
|
return self::INVALID_CHARACTERS; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Check sheet code name for valid Excel syntax. |
|
408
|
|
|
* |
|
409
|
|
|
* @param string $sheetCodeName The string to check |
|
410
|
|
|
* |
|
411
|
|
|
* @return string The valid string |
|
412
|
|
|
*/ |
|
413
|
10656 |
|
private static function checkSheetCodeName(string $sheetCodeName): string |
|
414
|
|
|
{ |
|
415
|
10656 |
|
$charCount = StringHelper::countCharacters($sheetCodeName); |
|
416
|
10656 |
|
if ($charCount == 0) { |
|
417
|
1 |
|
throw new Exception('Sheet code name cannot be empty.'); |
|
418
|
|
|
} |
|
419
|
|
|
// Some of the printable ASCII characters are invalid: * : / \ ? [ ] and first and last characters cannot be a "'" |
|
420
|
|
|
if ( |
|
421
|
10656 |
|
(str_replace(self::INVALID_CHARACTERS, '', $sheetCodeName) !== $sheetCodeName) |
|
422
|
10656 |
|
|| (StringHelper::substring($sheetCodeName, -1, 1) == '\'') |
|
423
|
10656 |
|
|| (StringHelper::substring($sheetCodeName, 0, 1) == '\'') |
|
424
|
|
|
) { |
|
425
|
1 |
|
throw new Exception('Invalid character found in sheet code name'); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
// Enforce maximum characters allowed for sheet title |
|
429
|
10656 |
|
if ($charCount > self::SHEET_TITLE_MAXIMUM_LENGTH) { |
|
430
|
1 |
|
throw new Exception('Maximum ' . self::SHEET_TITLE_MAXIMUM_LENGTH . ' characters allowed in sheet code name.'); |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
10656 |
|
return $sheetCodeName; |
|
434
|
|
|
} |
|
435
|
|
|
|
|
436
|
|
|
/** |
|
437
|
|
|
* Check sheet title for valid Excel syntax. |
|
438
|
|
|
* |
|
439
|
|
|
* @param string $sheetTitle The string to check |
|
440
|
|
|
* |
|
441
|
|
|
* @return string The valid string |
|
442
|
|
|
*/ |
|
443
|
10656 |
|
private static function checkSheetTitle(string $sheetTitle): string |
|
444
|
|
|
{ |
|
445
|
|
|
// Some of the printable ASCII characters are invalid: * : / \ ? [ ] |
|
446
|
10656 |
|
if (str_replace(self::INVALID_CHARACTERS, '', $sheetTitle) !== $sheetTitle) { |
|
447
|
2 |
|
throw new Exception('Invalid character found in sheet title'); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
// Enforce maximum characters allowed for sheet title |
|
451
|
10656 |
|
if (StringHelper::countCharacters($sheetTitle) > self::SHEET_TITLE_MAXIMUM_LENGTH) { |
|
452
|
3 |
|
throw new Exception('Maximum ' . self::SHEET_TITLE_MAXIMUM_LENGTH . ' characters allowed in sheet title.'); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
10656 |
|
return $sheetTitle; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Get a sorted list of all cell coordinates currently held in the collection by row and column. |
|
460
|
|
|
* |
|
461
|
|
|
* @param bool $sorted Also sort the cell collection? |
|
462
|
|
|
* |
|
463
|
|
|
* @return string[] |
|
464
|
|
|
*/ |
|
465
|
1454 |
|
public function getCoordinates(bool $sorted = true): array |
|
466
|
|
|
{ |
|
467
|
1454 |
|
if (!isset($this->cellCollection)) { //* @phpstan-ignore-line |
|
468
|
1 |
|
return []; |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
1454 |
|
if ($sorted) { |
|
472
|
525 |
|
return $this->cellCollection->getSortedCoordinates(); |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
1358 |
|
return $this->cellCollection->getCoordinates(); |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Get collection of row dimensions. |
|
480
|
|
|
* |
|
481
|
|
|
* @return RowDimension[] |
|
482
|
|
|
*/ |
|
483
|
1173 |
|
public function getRowDimensions(): array |
|
484
|
|
|
{ |
|
485
|
1173 |
|
return $this->rowDimensions; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* Get default row dimension. |
|
490
|
|
|
*/ |
|
491
|
1111 |
|
public function getDefaultRowDimension(): RowDimension |
|
492
|
|
|
{ |
|
493
|
1111 |
|
return $this->defaultRowDimension; |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
/** |
|
497
|
|
|
* Get collection of column dimensions. |
|
498
|
|
|
* |
|
499
|
|
|
* @return ColumnDimension[] |
|
500
|
|
|
*/ |
|
501
|
1179 |
|
public function getColumnDimensions(): array |
|
502
|
|
|
{ |
|
503
|
|
|
/** @var callable $callable */ |
|
504
|
1179 |
|
$callable = [self::class, 'columnDimensionCompare']; |
|
505
|
1179 |
|
uasort($this->columnDimensions, $callable); |
|
506
|
|
|
|
|
507
|
1179 |
|
return $this->columnDimensions; |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
95 |
|
private static function columnDimensionCompare(ColumnDimension $a, ColumnDimension $b): int |
|
511
|
|
|
{ |
|
512
|
95 |
|
return $a->getColumnNumeric() - $b->getColumnNumeric(); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Get default column dimension. |
|
517
|
|
|
*/ |
|
518
|
563 |
|
public function getDefaultColumnDimension(): ColumnDimension |
|
519
|
|
|
{ |
|
520
|
563 |
|
return $this->defaultColumnDimension; |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Get collection of drawings. |
|
525
|
|
|
* |
|
526
|
|
|
* @return ArrayObject<int, BaseDrawing> |
|
527
|
|
|
*/ |
|
528
|
1151 |
|
public function getDrawingCollection(): ArrayObject |
|
529
|
|
|
{ |
|
530
|
1151 |
|
return $this->drawingCollection; |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* Get collection of charts. |
|
535
|
|
|
* |
|
536
|
|
|
* @return ArrayObject<int, Chart> |
|
537
|
|
|
*/ |
|
538
|
100 |
|
public function getChartCollection(): ArrayObject |
|
539
|
|
|
{ |
|
540
|
100 |
|
return $this->chartCollection; |
|
541
|
|
|
} |
|
542
|
|
|
|
|
543
|
106 |
|
public function addChart(Chart $chart): Chart |
|
544
|
|
|
{ |
|
545
|
106 |
|
$chart->setWorksheet($this); |
|
546
|
106 |
|
$this->chartCollection[] = $chart; |
|
547
|
|
|
|
|
548
|
106 |
|
return $chart; |
|
549
|
|
|
} |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* Return the count of charts on this worksheet. |
|
553
|
|
|
* |
|
554
|
|
|
* @return int The number of charts |
|
555
|
|
|
*/ |
|
556
|
83 |
|
public function getChartCount(): int |
|
557
|
|
|
{ |
|
558
|
83 |
|
return count($this->chartCollection); |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* Get a chart by its index position. |
|
563
|
|
|
* |
|
564
|
|
|
* @param ?string $index Chart index position |
|
565
|
|
|
* |
|
566
|
|
|
* @return Chart|false |
|
567
|
|
|
*/ |
|
568
|
78 |
|
public function getChartByIndex(?string $index) |
|
569
|
|
|
{ |
|
570
|
78 |
|
$chartCount = count($this->chartCollection); |
|
571
|
78 |
|
if ($chartCount == 0) { |
|
572
|
|
|
return false; |
|
573
|
|
|
} |
|
574
|
78 |
|
if ($index === null) { |
|
575
|
|
|
$index = --$chartCount; |
|
576
|
|
|
} |
|
577
|
78 |
|
if (!isset($this->chartCollection[$index])) { |
|
578
|
|
|
return false; |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
78 |
|
return $this->chartCollection[$index]; |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
/** |
|
585
|
|
|
* Return an array of the names of charts on this worksheet. |
|
586
|
|
|
* |
|
587
|
|
|
* @return string[] The names of charts |
|
588
|
|
|
*/ |
|
589
|
5 |
|
public function getChartNames(): array |
|
590
|
|
|
{ |
|
591
|
5 |
|
$chartNames = []; |
|
592
|
5 |
|
foreach ($this->chartCollection as $chart) { |
|
593
|
5 |
|
$chartNames[] = $chart->getName(); |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
5 |
|
return $chartNames; |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* Get a chart by name. |
|
601
|
|
|
* |
|
602
|
|
|
* @param string $chartName Chart name |
|
603
|
|
|
* |
|
604
|
|
|
* @return Chart|false |
|
605
|
|
|
*/ |
|
606
|
6 |
|
public function getChartByName(string $chartName) |
|
607
|
|
|
{ |
|
608
|
6 |
|
foreach ($this->chartCollection as $index => $chart) { |
|
609
|
6 |
|
if ($chart->getName() == $chartName) { |
|
610
|
6 |
|
return $chart; |
|
611
|
|
|
} |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
1 |
|
return false; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
6 |
|
public function getChartByNameOrThrow(string $chartName): Chart |
|
618
|
|
|
{ |
|
619
|
6 |
|
$chart = $this->getChartByName($chartName); |
|
620
|
6 |
|
if ($chart !== false) { |
|
621
|
6 |
|
return $chart; |
|
622
|
|
|
} |
|
623
|
|
|
|
|
624
|
1 |
|
throw new Exception("Sheet does not have a chart named $chartName."); |
|
625
|
|
|
} |
|
626
|
|
|
|
|
627
|
|
|
/** |
|
628
|
|
|
* Refresh column dimensions. |
|
629
|
|
|
* |
|
630
|
|
|
* @return $this |
|
631
|
|
|
*/ |
|
632
|
25 |
|
public function refreshColumnDimensions(): static |
|
633
|
|
|
{ |
|
634
|
25 |
|
$newColumnDimensions = []; |
|
635
|
25 |
|
foreach ($this->getColumnDimensions() as $objColumnDimension) { |
|
636
|
25 |
|
$newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension; |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
25 |
|
$this->columnDimensions = $newColumnDimensions; |
|
640
|
|
|
|
|
641
|
25 |
|
return $this; |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
|
|
/** |
|
645
|
|
|
* Refresh row dimensions. |
|
646
|
|
|
* |
|
647
|
|
|
* @return $this |
|
648
|
|
|
*/ |
|
649
|
7 |
|
public function refreshRowDimensions(): static |
|
650
|
|
|
{ |
|
651
|
7 |
|
$newRowDimensions = []; |
|
652
|
7 |
|
foreach ($this->getRowDimensions() as $objRowDimension) { |
|
653
|
7 |
|
$newRowDimensions[$objRowDimension->getRowIndex()] = $objRowDimension; |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
7 |
|
$this->rowDimensions = $newRowDimensions; |
|
657
|
|
|
|
|
658
|
7 |
|
return $this; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Calculate worksheet dimension. |
|
663
|
|
|
* |
|
664
|
|
|
* @return string String containing the dimension of this worksheet |
|
665
|
|
|
*/ |
|
666
|
454 |
|
public function calculateWorksheetDimension(): string |
|
667
|
|
|
{ |
|
668
|
|
|
// Return |
|
669
|
454 |
|
return 'A1:' . $this->getHighestColumn() . $this->getHighestRow(); |
|
670
|
|
|
} |
|
671
|
|
|
|
|
672
|
|
|
/** |
|
673
|
|
|
* Calculate worksheet data dimension. |
|
674
|
|
|
* |
|
675
|
|
|
* @return string String containing the dimension of this worksheet that actually contain data |
|
676
|
|
|
*/ |
|
677
|
558 |
|
public function calculateWorksheetDataDimension(): string |
|
678
|
|
|
{ |
|
679
|
|
|
// Return |
|
680
|
558 |
|
return 'A1:' . $this->getHighestDataColumn() . $this->getHighestDataRow(); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
/** |
|
684
|
|
|
* Calculate widths for auto-size columns. |
|
685
|
|
|
* |
|
686
|
|
|
* @return $this |
|
687
|
|
|
*/ |
|
688
|
786 |
|
public function calculateColumnWidths(): static |
|
689
|
|
|
{ |
|
690
|
786 |
|
$activeSheet = $this->getParent()?->getActiveSheetIndex(); |
|
691
|
786 |
|
$selectedCells = $this->selectedCells; |
|
692
|
|
|
// initialize $autoSizes array |
|
693
|
786 |
|
$autoSizes = []; |
|
694
|
786 |
|
foreach ($this->getColumnDimensions() as $colDimension) { |
|
695
|
157 |
|
if ($colDimension->getAutoSize()) { |
|
696
|
62 |
|
$autoSizes[$colDimension->getColumnIndex()] = -1; |
|
697
|
|
|
} |
|
698
|
|
|
} |
|
699
|
|
|
|
|
700
|
|
|
// There is only something to do if there are some auto-size columns |
|
701
|
786 |
|
if (!empty($autoSizes)) { |
|
702
|
62 |
|
$holdActivePane = $this->activePane; |
|
703
|
|
|
// build list of cells references that participate in a merge |
|
704
|
62 |
|
$isMergeCell = []; |
|
705
|
62 |
|
foreach ($this->getMergeCells() as $cells) { |
|
706
|
16 |
|
foreach (Coordinate::extractAllCellReferencesInRange($cells) as $cellReference) { |
|
707
|
16 |
|
$isMergeCell[$cellReference] = true; |
|
708
|
|
|
} |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
62 |
|
$autoFilterIndentRanges = (new AutoFit($this))->getAutoFilterIndentRanges(); |
|
712
|
|
|
|
|
713
|
|
|
// loop through all cells in the worksheet |
|
714
|
62 |
|
foreach ($this->getCoordinates(false) as $coordinate) { |
|
715
|
62 |
|
$cell = $this->getCellOrNull($coordinate); |
|
716
|
|
|
|
|
717
|
62 |
|
if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) { |
|
718
|
|
|
//Determine if cell is in merge range |
|
719
|
62 |
|
$isMerged = isset($isMergeCell[$this->cellCollection->getCurrentCoordinate()]); |
|
720
|
|
|
|
|
721
|
|
|
//By default merged cells should be ignored |
|
722
|
62 |
|
$isMergedButProceed = false; |
|
723
|
|
|
|
|
724
|
|
|
//The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide) |
|
725
|
62 |
|
if ($isMerged && $cell->isMergeRangeValueCell()) { |
|
726
|
|
|
$range = (string) $cell->getMergeRange(); |
|
727
|
|
|
$rangeBoundaries = Coordinate::rangeDimension($range); |
|
728
|
|
|
if ($rangeBoundaries[0] === 1) { |
|
729
|
|
|
$isMergedButProceed = true; |
|
730
|
|
|
} |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
// Determine width if cell is not part of a merge or does and is a value cell of 1-column wide range |
|
734
|
62 |
|
if (!$isMerged || $isMergedButProceed) { |
|
735
|
|
|
// Determine if we need to make an adjustment for the first row in an AutoFilter range that |
|
736
|
|
|
// has a column filter dropdown |
|
737
|
62 |
|
$filterAdjustment = false; |
|
738
|
62 |
|
if (!empty($autoFilterIndentRanges)) { |
|
739
|
4 |
|
foreach ($autoFilterIndentRanges as $autoFilterFirstRowRange) { |
|
740
|
|
|
/** @var string $autoFilterFirstRowRange */ |
|
741
|
4 |
|
if ($cell->isInRange($autoFilterFirstRowRange)) { |
|
742
|
4 |
|
$filterAdjustment = true; |
|
743
|
|
|
|
|
744
|
4 |
|
break; |
|
745
|
|
|
} |
|
746
|
|
|
} |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
62 |
|
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent(); |
|
750
|
62 |
|
$indentAdjustment += (int) ($cell->getStyle()->getAlignment()->getHorizontal() === Alignment::HORIZONTAL_CENTER); |
|
751
|
|
|
|
|
752
|
|
|
// Calculated value |
|
753
|
|
|
// To formatted string |
|
754
|
62 |
|
$cellValue = NumberFormat::toFormattedString( |
|
755
|
62 |
|
$cell->getCalculatedValueString(), |
|
756
|
62 |
|
(string) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()) |
|
757
|
62 |
|
->getNumberFormat()->getFormatCode(true) |
|
758
|
62 |
|
); |
|
759
|
|
|
|
|
760
|
62 |
|
if ($cellValue !== '') { |
|
761
|
62 |
|
$autoSizes[$this->cellCollection->getCurrentColumn()] = max( |
|
762
|
62 |
|
$autoSizes[$this->cellCollection->getCurrentColumn()], |
|
763
|
62 |
|
round( |
|
764
|
62 |
|
Shared\Font::calculateColumnWidth( |
|
765
|
62 |
|
$this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont(), |
|
766
|
62 |
|
$cellValue, |
|
767
|
62 |
|
(int) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()) |
|
768
|
62 |
|
->getAlignment()->getTextRotation(), |
|
769
|
62 |
|
$this->getParentOrThrow()->getDefaultStyle()->getFont(), |
|
770
|
62 |
|
$filterAdjustment, |
|
771
|
62 |
|
$indentAdjustment |
|
772
|
62 |
|
), |
|
773
|
62 |
|
3 |
|
774
|
62 |
|
) |
|
775
|
62 |
|
); |
|
776
|
|
|
} |
|
777
|
|
|
} |
|
778
|
|
|
} |
|
779
|
|
|
} |
|
780
|
|
|
|
|
781
|
|
|
// adjust column widths |
|
782
|
62 |
|
foreach ($autoSizes as $columnIndex => $width) { |
|
783
|
62 |
|
if ($width == -1) { |
|
784
|
|
|
$width = $this->getDefaultColumnDimension()->getWidth(); |
|
785
|
|
|
} |
|
786
|
62 |
|
$this->getColumnDimension($columnIndex)->setWidth($width); |
|
787
|
|
|
} |
|
788
|
62 |
|
$this->activePane = $holdActivePane; |
|
789
|
|
|
} |
|
790
|
786 |
|
if ($activeSheet !== null && $activeSheet >= 0) { |
|
791
|
786 |
|
$this->getParent()?->setActiveSheetIndex($activeSheet); |
|
792
|
|
|
} |
|
793
|
786 |
|
$this->setSelectedCells($selectedCells); |
|
794
|
|
|
|
|
795
|
786 |
|
return $this; |
|
796
|
|
|
} |
|
797
|
|
|
|
|
798
|
|
|
/** |
|
799
|
|
|
* Get parent or null. |
|
800
|
|
|
*/ |
|
801
|
10318 |
|
public function getParent(): ?Spreadsheet |
|
802
|
|
|
{ |
|
803
|
10318 |
|
return $this->parent; |
|
804
|
|
|
} |
|
805
|
|
|
|
|
806
|
|
|
/** |
|
807
|
|
|
* Get parent, throw exception if null. |
|
808
|
|
|
*/ |
|
809
|
10350 |
|
public function getParentOrThrow(): Spreadsheet |
|
810
|
|
|
{ |
|
811
|
10350 |
|
if ($this->parent !== null) { |
|
812
|
10349 |
|
return $this->parent; |
|
813
|
|
|
} |
|
814
|
|
|
|
|
815
|
1 |
|
throw new Exception('Sheet does not have a parent.'); |
|
816
|
|
|
} |
|
817
|
|
|
|
|
818
|
|
|
/** |
|
819
|
|
|
* Re-bind parent. |
|
820
|
|
|
* |
|
821
|
|
|
* @return $this |
|
822
|
|
|
*/ |
|
823
|
54 |
|
public function rebindParent(Spreadsheet $parent): static |
|
824
|
|
|
{ |
|
825
|
54 |
|
if ($this->parent !== null) { |
|
826
|
4 |
|
$definedNames = $this->parent->getDefinedNames(); |
|
827
|
4 |
|
foreach ($definedNames as $definedName) { |
|
828
|
|
|
$parent->addDefinedName($definedName); |
|
829
|
|
|
} |
|
830
|
|
|
|
|
831
|
4 |
|
$this->parent->removeSheetByIndex( |
|
832
|
4 |
|
$this->parent->getIndex($this) |
|
833
|
4 |
|
); |
|
834
|
|
|
} |
|
835
|
54 |
|
$this->parent = $parent; |
|
836
|
|
|
|
|
837
|
54 |
|
return $this; |
|
838
|
|
|
} |
|
839
|
|
|
|
|
840
|
5 |
|
public function setParent(Spreadsheet $parent): self |
|
841
|
|
|
{ |
|
842
|
5 |
|
$this->parent = $parent; |
|
843
|
|
|
|
|
844
|
5 |
|
return $this; |
|
845
|
|
|
} |
|
846
|
|
|
|
|
847
|
|
|
/** |
|
848
|
|
|
* Get title. |
|
849
|
|
|
*/ |
|
850
|
10657 |
|
public function getTitle(): string |
|
851
|
|
|
{ |
|
852
|
10657 |
|
return $this->title; |
|
853
|
|
|
} |
|
854
|
|
|
|
|
855
|
|
|
/** |
|
856
|
|
|
* Set title. |
|
857
|
|
|
* |
|
858
|
|
|
* @param string $title String containing the dimension of this worksheet |
|
859
|
|
|
* @param bool $updateFormulaCellReferences Flag indicating whether cell references in formulae should |
|
860
|
|
|
* be updated to reflect the new sheet name. |
|
861
|
|
|
* This should be left as the default true, unless you are |
|
862
|
|
|
* certain that no formula cells on any worksheet contain |
|
863
|
|
|
* references to this worksheet |
|
864
|
|
|
* @param bool $validate False to skip validation of new title. WARNING: This should only be set |
|
865
|
|
|
* at parse time (by Readers), where titles can be assumed to be valid. |
|
866
|
|
|
* |
|
867
|
|
|
* @return $this |
|
868
|
|
|
*/ |
|
869
|
10656 |
|
public function setTitle(string $title, bool $updateFormulaCellReferences = true, bool $validate = true): static |
|
870
|
|
|
{ |
|
871
|
|
|
// Is this a 'rename' or not? |
|
872
|
10656 |
|
if ($this->getTitle() == $title) { |
|
873
|
293 |
|
return $this; |
|
874
|
|
|
} |
|
875
|
|
|
|
|
876
|
|
|
// Old title |
|
877
|
10656 |
|
$oldTitle = $this->getTitle(); |
|
878
|
|
|
|
|
879
|
10656 |
|
if ($validate) { |
|
880
|
|
|
// Syntax check |
|
881
|
10656 |
|
self::checkSheetTitle($title); |
|
882
|
|
|
|
|
883
|
10656 |
|
if ($this->parent && $this->parent->getIndex($this, true) >= 0) { |
|
884
|
|
|
// Is there already such sheet name? |
|
885
|
808 |
|
if ($this->parent->sheetNameExists($title)) { |
|
886
|
|
|
// Use name, but append with lowest possible integer |
|
887
|
|
|
|
|
888
|
2 |
|
if (StringHelper::countCharacters($title) > 29) { |
|
889
|
|
|
$title = StringHelper::substring($title, 0, 29); |
|
890
|
|
|
} |
|
891
|
2 |
|
$i = 1; |
|
892
|
2 |
|
while ($this->parent->sheetNameExists($title . ' ' . $i)) { |
|
893
|
1 |
|
++$i; |
|
894
|
1 |
|
if ($i == 10) { |
|
895
|
|
|
if (StringHelper::countCharacters($title) > 28) { |
|
896
|
|
|
$title = StringHelper::substring($title, 0, 28); |
|
897
|
|
|
} |
|
898
|
1 |
|
} elseif ($i == 100) { |
|
899
|
|
|
if (StringHelper::countCharacters($title) > 27) { |
|
900
|
|
|
$title = StringHelper::substring($title, 0, 27); |
|
901
|
|
|
} |
|
902
|
|
|
} |
|
903
|
|
|
} |
|
904
|
|
|
|
|
905
|
2 |
|
$title .= " $i"; |
|
906
|
|
|
} |
|
907
|
|
|
} |
|
908
|
|
|
} |
|
909
|
|
|
|
|
910
|
|
|
// Set title |
|
911
|
10656 |
|
$this->title = $title; |
|
912
|
|
|
|
|
913
|
10656 |
|
if ($this->parent && $this->parent->getIndex($this, true) >= 0) { |
|
914
|
|
|
// New title |
|
915
|
1423 |
|
$newTitle = $this->getTitle(); |
|
916
|
1423 |
|
$this->parent->getCalculationEngine() |
|
917
|
1423 |
|
->renameCalculationCacheForWorksheet($oldTitle, $newTitle); |
|
918
|
1423 |
|
if ($updateFormulaCellReferences) { |
|
919
|
808 |
|
ReferenceHelper::getInstance()->updateNamedFormulae($this->parent, $oldTitle, $newTitle); |
|
920
|
|
|
} |
|
921
|
|
|
} |
|
922
|
|
|
|
|
923
|
10656 |
|
return $this; |
|
924
|
|
|
} |
|
925
|
|
|
|
|
926
|
|
|
/** |
|
927
|
|
|
* Get sheet state. |
|
928
|
|
|
* |
|
929
|
|
|
* @return string Sheet state (visible, hidden, veryHidden) |
|
930
|
|
|
*/ |
|
931
|
508 |
|
public function getSheetState(): string |
|
932
|
|
|
{ |
|
933
|
508 |
|
return $this->sheetState; |
|
934
|
|
|
} |
|
935
|
|
|
|
|
936
|
|
|
/** |
|
937
|
|
|
* Set sheet state. |
|
938
|
|
|
* |
|
939
|
|
|
* @param string $value Sheet state (visible, hidden, veryHidden) |
|
940
|
|
|
* |
|
941
|
|
|
* @return $this |
|
942
|
|
|
*/ |
|
943
|
10656 |
|
public function setSheetState(string $value): static |
|
944
|
|
|
{ |
|
945
|
10656 |
|
$this->sheetState = $value; |
|
946
|
|
|
|
|
947
|
10656 |
|
return $this; |
|
948
|
|
|
} |
|
949
|
|
|
|
|
950
|
|
|
/** |
|
951
|
|
|
* Get page setup. |
|
952
|
|
|
*/ |
|
953
|
1571 |
|
public function getPageSetup(): PageSetup |
|
954
|
|
|
{ |
|
955
|
1571 |
|
return $this->pageSetup; |
|
956
|
|
|
} |
|
957
|
|
|
|
|
958
|
|
|
/** |
|
959
|
|
|
* Set page setup. |
|
960
|
|
|
* |
|
961
|
|
|
* @return $this |
|
962
|
|
|
*/ |
|
963
|
1 |
|
public function setPageSetup(PageSetup $pageSetup): static |
|
964
|
|
|
{ |
|
965
|
1 |
|
$this->pageSetup = $pageSetup; |
|
966
|
|
|
|
|
967
|
1 |
|
return $this; |
|
968
|
|
|
} |
|
969
|
|
|
|
|
970
|
|
|
/** |
|
971
|
|
|
* Get page margins. |
|
972
|
|
|
*/ |
|
973
|
1584 |
|
public function getPageMargins(): PageMargins |
|
974
|
|
|
{ |
|
975
|
1584 |
|
return $this->pageMargins; |
|
976
|
|
|
} |
|
977
|
|
|
|
|
978
|
|
|
/** |
|
979
|
|
|
* Set page margins. |
|
980
|
|
|
* |
|
981
|
|
|
* @return $this |
|
982
|
|
|
*/ |
|
983
|
1 |
|
public function setPageMargins(PageMargins $pageMargins): static |
|
984
|
|
|
{ |
|
985
|
1 |
|
$this->pageMargins = $pageMargins; |
|
986
|
|
|
|
|
987
|
1 |
|
return $this; |
|
988
|
|
|
} |
|
989
|
|
|
|
|
990
|
|
|
/** |
|
991
|
|
|
* Get page header/footer. |
|
992
|
|
|
*/ |
|
993
|
558 |
|
public function getHeaderFooter(): HeaderFooter |
|
994
|
|
|
{ |
|
995
|
558 |
|
return $this->headerFooter; |
|
996
|
|
|
} |
|
997
|
|
|
|
|
998
|
|
|
/** |
|
999
|
|
|
* Set page header/footer. |
|
1000
|
|
|
* |
|
1001
|
|
|
* @return $this |
|
1002
|
|
|
*/ |
|
1003
|
1 |
|
public function setHeaderFooter(HeaderFooter $headerFooter): static |
|
1004
|
|
|
{ |
|
1005
|
1 |
|
$this->headerFooter = $headerFooter; |
|
1006
|
|
|
|
|
1007
|
1 |
|
return $this; |
|
1008
|
|
|
} |
|
1009
|
|
|
|
|
1010
|
|
|
/** |
|
1011
|
|
|
* Get sheet view. |
|
1012
|
|
|
*/ |
|
1013
|
588 |
|
public function getSheetView(): SheetView |
|
1014
|
|
|
{ |
|
1015
|
588 |
|
return $this->sheetView; |
|
1016
|
|
|
} |
|
1017
|
|
|
|
|
1018
|
|
|
/** |
|
1019
|
|
|
* Set sheet view. |
|
1020
|
|
|
* |
|
1021
|
|
|
* @return $this |
|
1022
|
|
|
*/ |
|
1023
|
1 |
|
public function setSheetView(SheetView $sheetView): static |
|
1024
|
|
|
{ |
|
1025
|
1 |
|
$this->sheetView = $sheetView; |
|
1026
|
|
|
|
|
1027
|
1 |
|
return $this; |
|
1028
|
|
|
} |
|
1029
|
|
|
|
|
1030
|
|
|
/** |
|
1031
|
|
|
* Get Protection. |
|
1032
|
|
|
*/ |
|
1033
|
609 |
|
public function getProtection(): Protection |
|
1034
|
|
|
{ |
|
1035
|
609 |
|
return $this->protection; |
|
1036
|
|
|
} |
|
1037
|
|
|
|
|
1038
|
|
|
/** |
|
1039
|
|
|
* Set Protection. |
|
1040
|
|
|
* |
|
1041
|
|
|
* @return $this |
|
1042
|
|
|
*/ |
|
1043
|
1 |
|
public function setProtection(Protection $protection): static |
|
1044
|
|
|
{ |
|
1045
|
1 |
|
$this->protection = $protection; |
|
1046
|
|
|
|
|
1047
|
1 |
|
return $this; |
|
1048
|
|
|
} |
|
1049
|
|
|
|
|
1050
|
|
|
/** |
|
1051
|
|
|
* Get highest worksheet column. |
|
1052
|
|
|
* |
|
1053
|
|
|
* @param null|int|string $row Return the data highest column for the specified row, |
|
1054
|
|
|
* or the highest column of any row if no row number is passed |
|
1055
|
|
|
* |
|
1056
|
|
|
* @return string Highest column name |
|
1057
|
|
|
*/ |
|
1058
|
1473 |
|
public function getHighestColumn($row = null): string |
|
1059
|
|
|
{ |
|
1060
|
1473 |
|
if ($row === null) { |
|
1061
|
1472 |
|
return Coordinate::stringFromColumnIndex($this->cachedHighestColumn); |
|
1062
|
|
|
} |
|
1063
|
|
|
|
|
1064
|
1 |
|
return $this->getHighestDataColumn($row); |
|
1065
|
|
|
} |
|
1066
|
|
|
|
|
1067
|
|
|
/** |
|
1068
|
|
|
* Get highest worksheet column that contains data. |
|
1069
|
|
|
* |
|
1070
|
|
|
* @param null|int|string $row Return the highest data column for the specified row, |
|
1071
|
|
|
* or the highest data column of any row if no row number is passed |
|
1072
|
|
|
* |
|
1073
|
|
|
* @return string Highest column name that contains data |
|
1074
|
|
|
*/ |
|
1075
|
701 |
|
public function getHighestDataColumn($row = null): string |
|
1076
|
|
|
{ |
|
1077
|
701 |
|
return $this->cellCollection->getHighestColumn($row); |
|
1078
|
|
|
} |
|
1079
|
|
|
|
|
1080
|
|
|
/** |
|
1081
|
|
|
* Get highest worksheet row. |
|
1082
|
|
|
* |
|
1083
|
|
|
* @param null|string $column Return the highest data row for the specified column, |
|
1084
|
|
|
* or the highest row of any column if no column letter is passed |
|
1085
|
|
|
* |
|
1086
|
|
|
* @return int Highest row number |
|
1087
|
|
|
*/ |
|
1088
|
953 |
|
public function getHighestRow(?string $column = null): int |
|
1089
|
|
|
{ |
|
1090
|
953 |
|
if ($column === null) { |
|
1091
|
952 |
|
return $this->cachedHighestRow; |
|
1092
|
|
|
} |
|
1093
|
|
|
|
|
1094
|
1 |
|
return $this->getHighestDataRow($column); |
|
1095
|
|
|
} |
|
1096
|
|
|
|
|
1097
|
|
|
/** |
|
1098
|
|
|
* Get highest worksheet row that contains data. |
|
1099
|
|
|
* |
|
1100
|
|
|
* @param null|string $column Return the highest data row for the specified column, |
|
1101
|
|
|
* or the highest data row of any column if no column letter is passed |
|
1102
|
|
|
* |
|
1103
|
|
|
* @return int Highest row number that contains data |
|
1104
|
|
|
*/ |
|
1105
|
702 |
|
public function getHighestDataRow(?string $column = null): int |
|
1106
|
|
|
{ |
|
1107
|
702 |
|
return $this->cellCollection->getHighestRow($column); |
|
1108
|
|
|
} |
|
1109
|
|
|
|
|
1110
|
|
|
/** |
|
1111
|
|
|
* Get highest worksheet column and highest row that have cell records. |
|
1112
|
|
|
* |
|
1113
|
|
|
* @return array{row: int, column: string} Highest column name and highest row number |
|
1114
|
|
|
*/ |
|
1115
|
1 |
|
public function getHighestRowAndColumn(): array |
|
1116
|
|
|
{ |
|
1117
|
1 |
|
return $this->cellCollection->getHighestRowAndColumn(); |
|
1118
|
|
|
} |
|
1119
|
|
|
|
|
1120
|
|
|
/** |
|
1121
|
|
|
* Set a cell value. |
|
1122
|
|
|
* |
|
1123
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
1124
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
1125
|
|
|
* @param mixed $value Value for the cell |
|
1126
|
|
|
* @param null|IValueBinder $binder Value Binder to override the currently set Value Binder |
|
1127
|
|
|
* |
|
1128
|
|
|
* @return $this |
|
1129
|
|
|
*/ |
|
1130
|
5001 |
|
public function setCellValue(CellAddress|string|array $coordinate, mixed $value, ?IValueBinder $binder = null): static |
|
1131
|
|
|
{ |
|
1132
|
5001 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1133
|
5001 |
|
$this->getCell($cellAddress)->setValue($value, $binder); |
|
1134
|
|
|
|
|
1135
|
5001 |
|
return $this; |
|
1136
|
|
|
} |
|
1137
|
|
|
|
|
1138
|
|
|
/** |
|
1139
|
|
|
* Set a cell value. |
|
1140
|
|
|
* |
|
1141
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
1142
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
1143
|
|
|
* @param mixed $value Value of the cell |
|
1144
|
|
|
* @param string $dataType Explicit data type, see DataType::TYPE_* |
|
1145
|
|
|
* Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this |
|
1146
|
|
|
* method, then it is your responsibility as an end-user developer to validate that the value and |
|
1147
|
|
|
* the datatype match. |
|
1148
|
|
|
* If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype |
|
1149
|
|
|
* that you specify. |
|
1150
|
|
|
* |
|
1151
|
|
|
* @see DataType |
|
1152
|
|
|
* |
|
1153
|
|
|
* @return $this |
|
1154
|
|
|
*/ |
|
1155
|
104 |
|
public function setCellValueExplicit(CellAddress|string|array $coordinate, mixed $value, string $dataType): static |
|
1156
|
|
|
{ |
|
1157
|
104 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1158
|
104 |
|
$this->getCell($cellAddress)->setValueExplicit($value, $dataType); |
|
1159
|
|
|
|
|
1160
|
104 |
|
return $this; |
|
1161
|
|
|
} |
|
1162
|
|
|
|
|
1163
|
|
|
/** |
|
1164
|
|
|
* Get cell at a specific coordinate. |
|
1165
|
|
|
* |
|
1166
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
1167
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
1168
|
|
|
* |
|
1169
|
|
|
* @return Cell Cell that was found or created |
|
1170
|
|
|
* WARNING: Because the cell collection can be cached to reduce memory, it only allows one |
|
1171
|
|
|
* "active" cell at a time in memory. If you assign that cell to a variable, then select |
|
1172
|
|
|
* another cell using getCell() or any of its variants, the newly selected cell becomes |
|
1173
|
|
|
* the "active" cell, and any previous assignment becomes a disconnected reference because |
|
1174
|
|
|
* the active cell has changed. |
|
1175
|
|
|
*/ |
|
1176
|
10255 |
|
public function getCell(CellAddress|string|array $coordinate): Cell |
|
1177
|
|
|
{ |
|
1178
|
10255 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1179
|
|
|
|
|
1180
|
|
|
// Shortcut for increased performance for the vast majority of simple cases |
|
1181
|
10255 |
|
if ($this->cellCollection->has($cellAddress)) { |
|
1182
|
|
|
/** @var Cell $cell */ |
|
1183
|
10236 |
|
$cell = $this->cellCollection->get($cellAddress); |
|
1184
|
|
|
|
|
1185
|
10236 |
|
return $cell; |
|
1186
|
|
|
} |
|
1187
|
|
|
|
|
1188
|
|
|
/** @var Worksheet $sheet */ |
|
1189
|
10254 |
|
[$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress); |
|
1190
|
10254 |
|
$cell = $sheet->getCellCollection()->get($finalCoordinate); |
|
1191
|
|
|
|
|
1192
|
10254 |
|
return $cell ?? $sheet->createNewCell($finalCoordinate); |
|
1193
|
|
|
} |
|
1194
|
|
|
|
|
1195
|
|
|
/** |
|
1196
|
|
|
* Get the correct Worksheet and coordinate from a coordinate that may |
|
1197
|
|
|
* contains reference to another sheet or a named range. |
|
1198
|
|
|
* |
|
1199
|
|
|
* @return array{0: Worksheet, 1: string} |
|
1200
|
|
|
*/ |
|
1201
|
10264 |
|
private function getWorksheetAndCoordinate(string $coordinate): array |
|
1202
|
|
|
{ |
|
1203
|
10264 |
|
$sheet = null; |
|
1204
|
10264 |
|
$finalCoordinate = null; |
|
1205
|
|
|
|
|
1206
|
|
|
// Worksheet reference? |
|
1207
|
10264 |
|
if (str_contains($coordinate, '!')) { |
|
1208
|
|
|
$worksheetReference = self::extractSheetTitle($coordinate, true, true); |
|
1209
|
|
|
|
|
1210
|
|
|
$sheet = $this->getParentOrThrow()->getSheetByName($worksheetReference[0]); |
|
1211
|
|
|
$finalCoordinate = strtoupper($worksheetReference[1]); |
|
1212
|
|
|
|
|
1213
|
|
|
if ($sheet === null) { |
|
1214
|
|
|
throw new Exception('Sheet not found for name: ' . $worksheetReference[0]); |
|
1215
|
|
|
} |
|
1216
|
|
|
} elseif ( |
|
1217
|
10264 |
|
!Preg::isMatch('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $coordinate) |
|
1218
|
10264 |
|
&& Preg::isMatch('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/iu', $coordinate) |
|
1219
|
|
|
) { |
|
1220
|
|
|
// Named range? |
|
1221
|
17 |
|
$namedRange = $this->validateNamedRange($coordinate, true); |
|
1222
|
17 |
|
if ($namedRange !== null) { |
|
1223
|
12 |
|
$sheet = $namedRange->getWorksheet(); |
|
1224
|
12 |
|
if ($sheet === null) { |
|
1225
|
|
|
throw new Exception('Sheet not found for named range: ' . $namedRange->getName()); |
|
1226
|
|
|
} |
|
1227
|
|
|
|
|
1228
|
|
|
/** @phpstan-ignore-next-line */ |
|
1229
|
12 |
|
$cellCoordinate = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!'); |
|
1230
|
12 |
|
$finalCoordinate = str_replace('$', '', $cellCoordinate); |
|
1231
|
|
|
} |
|
1232
|
|
|
} |
|
1233
|
|
|
|
|
1234
|
10264 |
|
if ($sheet === null || $finalCoordinate === null) { |
|
1235
|
10264 |
|
$sheet = $this; |
|
1236
|
10264 |
|
$finalCoordinate = strtoupper($coordinate); |
|
1237
|
|
|
} |
|
1238
|
|
|
|
|
1239
|
10264 |
|
if (Coordinate::coordinateIsRange($finalCoordinate)) { |
|
1240
|
2 |
|
throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
1241
|
|
|
} |
|
1242
|
10264 |
|
$finalCoordinate = str_replace('$', '', $finalCoordinate); |
|
1243
|
|
|
|
|
1244
|
10264 |
|
return [$sheet, $finalCoordinate]; |
|
1245
|
|
|
} |
|
1246
|
|
|
|
|
1247
|
|
|
/** |
|
1248
|
|
|
* Get an existing cell at a specific coordinate, or null. |
|
1249
|
|
|
* |
|
1250
|
|
|
* @param string $coordinate Coordinate of the cell, eg: 'A1' |
|
1251
|
|
|
* |
|
1252
|
|
|
* @return null|Cell Cell that was found or null |
|
1253
|
|
|
*/ |
|
1254
|
62 |
|
private function getCellOrNull(string $coordinate): ?Cell |
|
1255
|
|
|
{ |
|
1256
|
|
|
// Check cell collection |
|
1257
|
62 |
|
if ($this->cellCollection->has($coordinate)) { |
|
1258
|
62 |
|
return $this->cellCollection->get($coordinate); |
|
1259
|
|
|
} |
|
1260
|
|
|
|
|
1261
|
|
|
return null; |
|
1262
|
|
|
} |
|
1263
|
|
|
|
|
1264
|
|
|
/** |
|
1265
|
|
|
* Create a new cell at the specified coordinate. |
|
1266
|
|
|
* |
|
1267
|
|
|
* @param string $coordinate Coordinate of the cell |
|
1268
|
|
|
* |
|
1269
|
|
|
* @return Cell Cell that was created |
|
1270
|
|
|
* WARNING: Because the cell collection can be cached to reduce memory, it only allows one |
|
1271
|
|
|
* "active" cell at a time in memory. If you assign that cell to a variable, then select |
|
1272
|
|
|
* another cell using getCell() or any of its variants, the newly selected cell becomes |
|
1273
|
|
|
* the "active" cell, and any previous assignment becomes a disconnected reference because |
|
1274
|
|
|
* the active cell has changed. |
|
1275
|
|
|
*/ |
|
1276
|
10254 |
|
public function createNewCell(string $coordinate): Cell |
|
1277
|
|
|
{ |
|
1278
|
10254 |
|
[$column, $row, $columnString] = Coordinate::indexesFromString($coordinate); |
|
1279
|
10254 |
|
$cell = new Cell(null, DataType::TYPE_NULL, $this); |
|
1280
|
10254 |
|
$this->cellCollection->add($coordinate, $cell); |
|
1281
|
|
|
|
|
1282
|
|
|
// Coordinates |
|
1283
|
10254 |
|
if ($column > $this->cachedHighestColumn) { |
|
1284
|
7065 |
|
$this->cachedHighestColumn = $column; |
|
1285
|
|
|
} |
|
1286
|
10254 |
|
if ($row > $this->cachedHighestRow) { |
|
1287
|
8618 |
|
$this->cachedHighestRow = $row; |
|
1288
|
|
|
} |
|
1289
|
|
|
|
|
1290
|
|
|
// Cell needs appropriate xfIndex from dimensions records |
|
1291
|
|
|
// but don't create dimension records if they don't already exist |
|
1292
|
10254 |
|
$rowDimension = $this->rowDimensions[$row] ?? null; |
|
1293
|
10254 |
|
$columnDimension = $this->columnDimensions[$columnString] ?? null; |
|
1294
|
|
|
|
|
1295
|
10254 |
|
$xfSet = false; |
|
1296
|
10254 |
|
if ($rowDimension !== null) { |
|
1297
|
393 |
|
$rowXf = (int) $rowDimension->getXfIndex(); |
|
1298
|
393 |
|
if ($rowXf > 0) { |
|
1299
|
|
|
// then there is a row dimension with explicit style, assign it to the cell |
|
1300
|
203 |
|
$cell->setXfIndex($rowXf); |
|
1301
|
203 |
|
$xfSet = true; |
|
1302
|
|
|
} |
|
1303
|
|
|
} |
|
1304
|
10254 |
|
if (!$xfSet && $columnDimension !== null) { |
|
1305
|
566 |
|
$colXf = (int) $columnDimension->getXfIndex(); |
|
1306
|
566 |
|
if ($colXf > 0) { |
|
1307
|
|
|
// then there is a column dimension, assign it to the cell |
|
1308
|
215 |
|
$cell->setXfIndex($colXf); |
|
1309
|
|
|
} |
|
1310
|
|
|
} |
|
1311
|
|
|
|
|
1312
|
10254 |
|
return $cell; |
|
1313
|
|
|
} |
|
1314
|
|
|
|
|
1315
|
|
|
/** |
|
1316
|
|
|
* Does the cell at a specific coordinate exist? |
|
1317
|
|
|
* |
|
1318
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
1319
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
1320
|
|
|
*/ |
|
1321
|
10182 |
|
public function cellExists(CellAddress|string|array $coordinate): bool |
|
1322
|
|
|
{ |
|
1323
|
10182 |
|
$cellAddress = Validations::validateCellAddress($coordinate); |
|
1324
|
10182 |
|
[$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress); |
|
1325
|
|
|
|
|
1326
|
10182 |
|
return $sheet->getCellCollection()->has($finalCoordinate); |
|
1327
|
|
|
} |
|
1328
|
|
|
|
|
1329
|
|
|
/** |
|
1330
|
|
|
* Get row dimension at a specific row. |
|
1331
|
|
|
* |
|
1332
|
|
|
* @param int $row Numeric index of the row |
|
1333
|
|
|
*/ |
|
1334
|
561 |
|
public function getRowDimension(int $row): RowDimension |
|
1335
|
|
|
{ |
|
1336
|
|
|
// Get row dimension |
|
1337
|
561 |
|
if (!isset($this->rowDimensions[$row])) { |
|
1338
|
561 |
|
$this->rowDimensions[$row] = new RowDimension($row); |
|
1339
|
|
|
|
|
1340
|
561 |
|
$this->cachedHighestRow = max($this->cachedHighestRow, $row); |
|
1341
|
|
|
} |
|
1342
|
|
|
|
|
1343
|
561 |
|
return $this->rowDimensions[$row]; |
|
1344
|
|
|
} |
|
1345
|
|
|
|
|
1346
|
1 |
|
public function getRowStyle(int $row): ?Style |
|
1347
|
|
|
{ |
|
1348
|
1 |
|
return $this->parent?->getCellXfByIndexOrNull( |
|
1349
|
1 |
|
($this->rowDimensions[$row] ?? null)?->getXfIndex() |
|
1350
|
1 |
|
); |
|
1351
|
|
|
} |
|
1352
|
|
|
|
|
1353
|
600 |
|
public function rowDimensionExists(int $row): bool |
|
1354
|
|
|
{ |
|
1355
|
600 |
|
return isset($this->rowDimensions[$row]); |
|
1356
|
|
|
} |
|
1357
|
|
|
|
|
1358
|
39 |
|
public function columnDimensionExists(string $column): bool |
|
1359
|
|
|
{ |
|
1360
|
39 |
|
return isset($this->columnDimensions[$column]); |
|
1361
|
|
|
} |
|
1362
|
|
|
|
|
1363
|
|
|
/** |
|
1364
|
|
|
* Get column dimension at a specific column. |
|
1365
|
|
|
* |
|
1366
|
|
|
* @param string $column String index of the column eg: 'A' |
|
1367
|
|
|
*/ |
|
1368
|
661 |
|
public function getColumnDimension(string $column): ColumnDimension |
|
1369
|
|
|
{ |
|
1370
|
|
|
// Uppercase coordinate |
|
1371
|
661 |
|
$column = strtoupper($column); |
|
1372
|
|
|
|
|
1373
|
|
|
// Fetch dimensions |
|
1374
|
661 |
|
if (!isset($this->columnDimensions[$column])) { |
|
1375
|
661 |
|
$this->columnDimensions[$column] = new ColumnDimension($column); |
|
1376
|
|
|
|
|
1377
|
661 |
|
$columnIndex = Coordinate::columnIndexFromString($column); |
|
1378
|
661 |
|
if ($this->cachedHighestColumn < $columnIndex) { |
|
1379
|
460 |
|
$this->cachedHighestColumn = $columnIndex; |
|
1380
|
|
|
} |
|
1381
|
|
|
} |
|
1382
|
|
|
|
|
1383
|
661 |
|
return $this->columnDimensions[$column]; |
|
1384
|
|
|
} |
|
1385
|
|
|
|
|
1386
|
|
|
/** |
|
1387
|
|
|
* Get column dimension at a specific column by using numeric cell coordinates. |
|
1388
|
|
|
* |
|
1389
|
|
|
* @param int $columnIndex Numeric column coordinate of the cell |
|
1390
|
|
|
*/ |
|
1391
|
108 |
|
public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension |
|
1392
|
|
|
{ |
|
1393
|
108 |
|
return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex)); |
|
1394
|
|
|
} |
|
1395
|
|
|
|
|
1396
|
1 |
|
public function getColumnStyle(string $column): ?Style |
|
1397
|
|
|
{ |
|
1398
|
1 |
|
return $this->parent?->getCellXfByIndexOrNull( |
|
1399
|
1 |
|
($this->columnDimensions[$column] ?? null)?->getXfIndex() |
|
1400
|
1 |
|
); |
|
1401
|
|
|
} |
|
1402
|
|
|
|
|
1403
|
|
|
/** |
|
1404
|
|
|
* Get style for cell. |
|
1405
|
|
|
* |
|
1406
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellCoordinate |
|
1407
|
|
|
* A simple string containing a cell address like 'A1' or a cell range like 'A1:E10' |
|
1408
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
1409
|
|
|
* or a CellAddress or AddressRange object. |
|
1410
|
|
|
*/ |
|
1411
|
10230 |
|
public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate): Style |
|
1412
|
|
|
{ |
|
1413
|
10230 |
|
if (is_string($cellCoordinate)) { |
|
1414
|
10228 |
|
$cellCoordinate = Validations::definedNameToCoordinate($cellCoordinate, $this); |
|
1415
|
|
|
} |
|
1416
|
10230 |
|
$cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate); |
|
1417
|
10230 |
|
$cellCoordinate = str_replace('$', '', $cellCoordinate); |
|
1418
|
|
|
|
|
1419
|
|
|
// set this sheet as active |
|
1420
|
10230 |
|
$this->getParentOrThrow()->setActiveSheetIndex($this->getParentOrThrow()->getIndex($this)); |
|
1421
|
|
|
|
|
1422
|
|
|
// set cell coordinate as active |
|
1423
|
10230 |
|
$this->setSelectedCells($cellCoordinate); |
|
1424
|
|
|
|
|
1425
|
10230 |
|
return $this->getParentOrThrow()->getCellXfSupervisor(); |
|
1426
|
|
|
} |
|
1427
|
|
|
|
|
1428
|
|
|
/** |
|
1429
|
|
|
* Get table styles set for the for given cell. |
|
1430
|
|
|
* |
|
1431
|
|
|
* @param Cell $cell |
|
1432
|
|
|
* The Cell for which the tables are retrieved |
|
1433
|
|
|
* |
|
1434
|
|
|
* @return mixed[] |
|
1435
|
|
|
*/ |
|
1436
|
549 |
|
public function getTablesWithStylesForCell(Cell $cell): array |
|
1437
|
|
|
{ |
|
1438
|
549 |
|
$retVal = []; |
|
1439
|
|
|
|
|
1440
|
549 |
|
foreach ($this->tableCollection as $table) { |
|
1441
|
|
|
/** @var Table $table */ |
|
1442
|
4 |
|
$dxfsTableStyle = $table->getStyle()->getTableDxfsStyle(); |
|
1443
|
4 |
|
if ($dxfsTableStyle !== null) { |
|
1444
|
4 |
|
if ($dxfsTableStyle->getHeaderRowStyle() !== null || $dxfsTableStyle->getFirstRowStripeStyle() !== null || $dxfsTableStyle->getSecondRowStripeStyle() !== null) { |
|
1445
|
4 |
|
$range = $table->getRange(); |
|
1446
|
4 |
|
if ($cell->isInRange($range)) { |
|
1447
|
4 |
|
$retVal[] = $table; |
|
1448
|
|
|
} |
|
1449
|
|
|
} |
|
1450
|
|
|
} |
|
1451
|
|
|
} |
|
1452
|
|
|
|
|
1453
|
549 |
|
return $retVal; |
|
1454
|
|
|
} |
|
1455
|
|
|
|
|
1456
|
|
|
/** |
|
1457
|
|
|
* Get conditional styles for a cell. |
|
1458
|
|
|
* |
|
1459
|
|
|
* @param string $coordinate eg: 'A1' or 'A1:A3'. |
|
1460
|
|
|
* If a single cell is referenced, then the array of conditional styles will be returned if the cell is |
|
1461
|
|
|
* included in a conditional style range. |
|
1462
|
|
|
* If a range of cells is specified, then the styles will only be returned if the range matches the entire |
|
1463
|
|
|
* range of the conditional. |
|
1464
|
|
|
* @param bool $firstOnly default true, return all matching |
|
1465
|
|
|
* conditionals ordered by priority if false, first only if true |
|
1466
|
|
|
* |
|
1467
|
|
|
* @return Conditional[] |
|
1468
|
|
|
*/ |
|
1469
|
789 |
|
public function getConditionalStyles(string $coordinate, bool $firstOnly = true): array |
|
1470
|
|
|
{ |
|
1471
|
789 |
|
$coordinate = strtoupper($coordinate); |
|
1472
|
789 |
|
if (Preg::isMatch('/[: ,]/', $coordinate)) { |
|
1473
|
48 |
|
return $this->conditionalStylesCollection[$coordinate] ?? []; |
|
1474
|
|
|
} |
|
1475
|
|
|
|
|
1476
|
766 |
|
$conditionalStyles = []; |
|
1477
|
766 |
|
foreach ($this->conditionalStylesCollection as $keyStylesOrig => $conditionalRange) { |
|
1478
|
221 |
|
$keyStyles = Coordinate::resolveUnionAndIntersection($keyStylesOrig); |
|
1479
|
221 |
|
$keyParts = explode(',', $keyStyles); |
|
1480
|
221 |
|
foreach ($keyParts as $keyPart) { |
|
1481
|
221 |
|
if ($keyPart === $coordinate) { |
|
1482
|
14 |
|
if ($firstOnly) { |
|
1483
|
14 |
|
return $conditionalRange; |
|
1484
|
|
|
} |
|
1485
|
|
|
$conditionalStyles[$keyStylesOrig] = $conditionalRange; |
|
1486
|
|
|
|
|
1487
|
|
|
break; |
|
1488
|
216 |
|
} elseif (str_contains($keyPart, ':')) { |
|
1489
|
211 |
|
if (Coordinate::coordinateIsInsideRange($keyPart, $coordinate)) { |
|
1490
|
197 |
|
if ($firstOnly) { |
|
1491
|
196 |
|
return $conditionalRange; |
|
1492
|
|
|
} |
|
1493
|
1 |
|
$conditionalStyles[$keyStylesOrig] = $conditionalRange; |
|
1494
|
|
|
|
|
1495
|
1 |
|
break; |
|
1496
|
|
|
} |
|
1497
|
|
|
} |
|
1498
|
|
|
} |
|
1499
|
|
|
} |
|
1500
|
593 |
|
$outArray = []; |
|
1501
|
593 |
|
foreach ($conditionalStyles as $conditionalArray) { |
|
1502
|
1 |
|
foreach ($conditionalArray as $conditional) { |
|
1503
|
1 |
|
$outArray[] = $conditional; |
|
1504
|
|
|
} |
|
1505
|
|
|
} |
|
1506
|
593 |
|
usort($outArray, [self::class, 'comparePriority']); |
|
1507
|
|
|
|
|
1508
|
593 |
|
return $outArray; |
|
1509
|
|
|
} |
|
1510
|
|
|
|
|
1511
|
1 |
|
private static function comparePriority(Conditional $condA, Conditional $condB): int |
|
1512
|
|
|
{ |
|
1513
|
1 |
|
$a = $condA->getPriority(); |
|
1514
|
1 |
|
$b = $condB->getPriority(); |
|
1515
|
1 |
|
if ($a === $b) { |
|
1516
|
|
|
return 0; |
|
1517
|
|
|
} |
|
1518
|
1 |
|
if ($a === 0) { |
|
1519
|
|
|
return 1; |
|
1520
|
|
|
} |
|
1521
|
1 |
|
if ($b === 0) { |
|
1522
|
|
|
return -1; |
|
1523
|
|
|
} |
|
1524
|
|
|
|
|
1525
|
1 |
|
return ($a < $b) ? -1 : 1; |
|
1526
|
|
|
} |
|
1527
|
|
|
|
|
1528
|
189 |
|
public function getConditionalRange(string $coordinate): ?string |
|
1529
|
|
|
{ |
|
1530
|
189 |
|
$coordinate = strtoupper($coordinate); |
|
1531
|
189 |
|
$cell = $this->getCell($coordinate); |
|
1532
|
189 |
|
foreach (array_keys($this->conditionalStylesCollection) as $conditionalRange) { |
|
1533
|
189 |
|
$cellBlocks = explode(',', Coordinate::resolveUnionAndIntersection($conditionalRange)); |
|
1534
|
189 |
|
foreach ($cellBlocks as $cellBlock) { |
|
1535
|
189 |
|
if ($cell->isInRange($cellBlock)) { |
|
1536
|
188 |
|
return $conditionalRange; |
|
1537
|
|
|
} |
|
1538
|
|
|
} |
|
1539
|
|
|
} |
|
1540
|
|
|
|
|
1541
|
3 |
|
return null; |
|
1542
|
|
|
} |
|
1543
|
|
|
|
|
1544
|
|
|
/** |
|
1545
|
|
|
* Do conditional styles exist for this cell? |
|
1546
|
|
|
* |
|
1547
|
|
|
* @param string $coordinate eg: 'A1' or 'A1:A3'. |
|
1548
|
|
|
* If a single cell is specified, then this method will return true if that cell is included in a |
|
1549
|
|
|
* conditional style range. |
|
1550
|
|
|
* If a range of cells is specified, then true will only be returned if the range matches the entire |
|
1551
|
|
|
* range of the conditional. |
|
1552
|
|
|
*/ |
|
1553
|
22 |
|
public function conditionalStylesExists(string $coordinate): bool |
|
1554
|
|
|
{ |
|
1555
|
22 |
|
return !empty($this->getConditionalStyles($coordinate)); |
|
1556
|
|
|
} |
|
1557
|
|
|
|
|
1558
|
|
|
/** |
|
1559
|
|
|
* Removes conditional styles for a cell. |
|
1560
|
|
|
* |
|
1561
|
|
|
* @param string $coordinate eg: 'A1' |
|
1562
|
|
|
* |
|
1563
|
|
|
* @return $this |
|
1564
|
|
|
*/ |
|
1565
|
47 |
|
public function removeConditionalStyles(string $coordinate): static |
|
1566
|
|
|
{ |
|
1567
|
47 |
|
unset($this->conditionalStylesCollection[strtoupper($coordinate)]); |
|
1568
|
|
|
|
|
1569
|
47 |
|
return $this; |
|
1570
|
|
|
} |
|
1571
|
|
|
|
|
1572
|
|
|
/** |
|
1573
|
|
|
* Get collection of conditional styles. |
|
1574
|
|
|
* |
|
1575
|
|
|
* @return Conditional[][] |
|
1576
|
|
|
*/ |
|
1577
|
1124 |
|
public function getConditionalStylesCollection(): array |
|
1578
|
|
|
{ |
|
1579
|
1124 |
|
return $this->conditionalStylesCollection; |
|
1580
|
|
|
} |
|
1581
|
|
|
|
|
1582
|
|
|
/** |
|
1583
|
|
|
* Set conditional styles. |
|
1584
|
|
|
* |
|
1585
|
|
|
* @param string $coordinate eg: 'A1' |
|
1586
|
|
|
* @param Conditional[] $styles |
|
1587
|
|
|
* |
|
1588
|
|
|
* @return $this |
|
1589
|
|
|
*/ |
|
1590
|
318 |
|
public function setConditionalStyles(string $coordinate, array $styles): static |
|
1591
|
|
|
{ |
|
1592
|
318 |
|
$this->conditionalStylesCollection[strtoupper($coordinate)] = $styles; |
|
1593
|
|
|
|
|
1594
|
318 |
|
return $this; |
|
1595
|
|
|
} |
|
1596
|
|
|
|
|
1597
|
|
|
/** |
|
1598
|
|
|
* Duplicate cell style to a range of cells. |
|
1599
|
|
|
* |
|
1600
|
|
|
* Please note that this will overwrite existing cell styles for cells in range! |
|
1601
|
|
|
* |
|
1602
|
|
|
* @param Style $style Cell style to duplicate |
|
1603
|
|
|
* @param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") |
|
1604
|
|
|
* |
|
1605
|
|
|
* @return $this |
|
1606
|
|
|
*/ |
|
1607
|
2 |
|
public function duplicateStyle(Style $style, string $range): static |
|
1608
|
|
|
{ |
|
1609
|
|
|
// Add the style to the workbook if necessary |
|
1610
|
2 |
|
$workbook = $this->getParentOrThrow(); |
|
1611
|
2 |
|
if ($existingStyle = $workbook->getCellXfByHashCode($style->getHashCode())) { |
|
1612
|
|
|
// there is already such cell Xf in our collection |
|
1613
|
1 |
|
$xfIndex = $existingStyle->getIndex(); |
|
1614
|
|
|
} else { |
|
1615
|
|
|
// we don't have such a cell Xf, need to add |
|
1616
|
2 |
|
$workbook->addCellXf($style); |
|
1617
|
2 |
|
$xfIndex = $style->getIndex(); |
|
1618
|
|
|
} |
|
1619
|
|
|
|
|
1620
|
|
|
// Calculate range outer borders |
|
1621
|
2 |
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range); |
|
1622
|
|
|
|
|
1623
|
|
|
// Make sure we can loop upwards on rows and columns |
|
1624
|
2 |
|
if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { |
|
1625
|
|
|
$tmp = $rangeStart; |
|
1626
|
|
|
$rangeStart = $rangeEnd; |
|
1627
|
|
|
$rangeEnd = $tmp; |
|
1628
|
|
|
} |
|
1629
|
|
|
|
|
1630
|
|
|
// Loop through cells and apply styles |
|
1631
|
2 |
|
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
|
1632
|
2 |
|
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
|
1633
|
2 |
|
$this->getCell(Coordinate::stringFromColumnIndex($col) . $row)->setXfIndex($xfIndex); |
|
1634
|
|
|
} |
|
1635
|
|
|
} |
|
1636
|
|
|
|
|
1637
|
2 |
|
return $this; |
|
1638
|
|
|
} |
|
1639
|
|
|
|
|
1640
|
|
|
/** |
|
1641
|
|
|
* Duplicate conditional style to a range of cells. |
|
1642
|
|
|
* |
|
1643
|
|
|
* Please note that this will overwrite existing cell styles for cells in range! |
|
1644
|
|
|
* |
|
1645
|
|
|
* @param Conditional[] $styles Cell style to duplicate |
|
1646
|
|
|
* @param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") |
|
1647
|
|
|
* |
|
1648
|
|
|
* @return $this |
|
1649
|
|
|
*/ |
|
1650
|
18 |
|
public function duplicateConditionalStyle(array $styles, string $range = ''): static |
|
1651
|
|
|
{ |
|
1652
|
18 |
|
foreach ($styles as $cellStyle) { |
|
1653
|
18 |
|
if (!($cellStyle instanceof Conditional)) { // @phpstan-ignore-line |
|
1654
|
|
|
throw new Exception('Style is not a conditional style'); |
|
1655
|
|
|
} |
|
1656
|
|
|
} |
|
1657
|
|
|
|
|
1658
|
|
|
// Calculate range outer borders |
|
1659
|
18 |
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range); |
|
1660
|
|
|
|
|
1661
|
|
|
// Make sure we can loop upwards on rows and columns |
|
1662
|
18 |
|
if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { |
|
1663
|
|
|
$tmp = $rangeStart; |
|
1664
|
|
|
$rangeStart = $rangeEnd; |
|
1665
|
|
|
$rangeEnd = $tmp; |
|
1666
|
|
|
} |
|
1667
|
|
|
|
|
1668
|
|
|
// Loop through cells and apply styles |
|
1669
|
18 |
|
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
|
1670
|
18 |
|
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
|
1671
|
18 |
|
$this->setConditionalStyles(Coordinate::stringFromColumnIndex($col) . $row, $styles); |
|
1672
|
|
|
} |
|
1673
|
|
|
} |
|
1674
|
|
|
|
|
1675
|
18 |
|
return $this; |
|
1676
|
|
|
} |
|
1677
|
|
|
|
|
1678
|
|
|
/** |
|
1679
|
|
|
* Set break on a cell. |
|
1680
|
|
|
* |
|
1681
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
1682
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
1683
|
|
|
* @param int $break Break type (type of Worksheet::BREAK_*) |
|
1684
|
|
|
* |
|
1685
|
|
|
* @return $this |
|
1686
|
|
|
*/ |
|
1687
|
33 |
|
public function setBreak(CellAddress|string|array $coordinate, int $break, int $max = -1): static |
|
1688
|
|
|
{ |
|
1689
|
33 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1690
|
|
|
|
|
1691
|
33 |
|
if ($break === self::BREAK_NONE) { |
|
1692
|
7 |
|
unset($this->rowBreaks[$cellAddress], $this->columnBreaks[$cellAddress]); |
|
1693
|
33 |
|
} elseif ($break === self::BREAK_ROW) { |
|
1694
|
23 |
|
$this->rowBreaks[$cellAddress] = new PageBreak($break, $cellAddress, $max); |
|
1695
|
19 |
|
} elseif ($break === self::BREAK_COLUMN) { |
|
1696
|
19 |
|
$this->columnBreaks[$cellAddress] = new PageBreak($break, $cellAddress, $max); |
|
1697
|
|
|
} |
|
1698
|
|
|
|
|
1699
|
33 |
|
return $this; |
|
1700
|
|
|
} |
|
1701
|
|
|
|
|
1702
|
|
|
/** |
|
1703
|
|
|
* Get breaks. |
|
1704
|
|
|
* |
|
1705
|
|
|
* @return int[] |
|
1706
|
|
|
*/ |
|
1707
|
649 |
|
public function getBreaks(): array |
|
1708
|
|
|
{ |
|
1709
|
649 |
|
$breaks = []; |
|
1710
|
|
|
/** @var callable $compareFunction */ |
|
1711
|
649 |
|
$compareFunction = [self::class, 'compareRowBreaks']; |
|
1712
|
649 |
|
uksort($this->rowBreaks, $compareFunction); |
|
1713
|
649 |
|
foreach ($this->rowBreaks as $break) { |
|
1714
|
10 |
|
$breaks[$break->getCoordinate()] = self::BREAK_ROW; |
|
1715
|
|
|
} |
|
1716
|
|
|
/** @var callable $compareFunction */ |
|
1717
|
649 |
|
$compareFunction = [self::class, 'compareColumnBreaks']; |
|
1718
|
649 |
|
uksort($this->columnBreaks, $compareFunction); |
|
1719
|
649 |
|
foreach ($this->columnBreaks as $break) { |
|
1720
|
8 |
|
$breaks[$break->getCoordinate()] = self::BREAK_COLUMN; |
|
1721
|
|
|
} |
|
1722
|
|
|
|
|
1723
|
649 |
|
return $breaks; |
|
1724
|
|
|
} |
|
1725
|
|
|
|
|
1726
|
|
|
/** |
|
1727
|
|
|
* Get row breaks. |
|
1728
|
|
|
* |
|
1729
|
|
|
* @return PageBreak[] |
|
1730
|
|
|
*/ |
|
1731
|
507 |
|
public function getRowBreaks(): array |
|
1732
|
|
|
{ |
|
1733
|
|
|
/** @var callable $compareFunction */ |
|
1734
|
507 |
|
$compareFunction = [self::class, 'compareRowBreaks']; |
|
1735
|
507 |
|
uksort($this->rowBreaks, $compareFunction); |
|
1736
|
|
|
|
|
1737
|
507 |
|
return $this->rowBreaks; |
|
1738
|
|
|
} |
|
1739
|
|
|
|
|
1740
|
9 |
|
protected static function compareRowBreaks(string $coordinate1, string $coordinate2): int |
|
1741
|
|
|
{ |
|
1742
|
9 |
|
$row1 = Coordinate::indexesFromString($coordinate1)[1]; |
|
1743
|
9 |
|
$row2 = Coordinate::indexesFromString($coordinate2)[1]; |
|
1744
|
|
|
|
|
1745
|
9 |
|
return $row1 - $row2; |
|
1746
|
|
|
} |
|
1747
|
|
|
|
|
1748
|
5 |
|
protected static function compareColumnBreaks(string $coordinate1, string $coordinate2): int |
|
1749
|
|
|
{ |
|
1750
|
5 |
|
$column1 = Coordinate::indexesFromString($coordinate1)[0]; |
|
1751
|
5 |
|
$column2 = Coordinate::indexesFromString($coordinate2)[0]; |
|
1752
|
|
|
|
|
1753
|
5 |
|
return $column1 - $column2; |
|
1754
|
|
|
} |
|
1755
|
|
|
|
|
1756
|
|
|
/** |
|
1757
|
|
|
* Get column breaks. |
|
1758
|
|
|
* |
|
1759
|
|
|
* @return PageBreak[] |
|
1760
|
|
|
*/ |
|
1761
|
506 |
|
public function getColumnBreaks(): array |
|
1762
|
|
|
{ |
|
1763
|
|
|
/** @var callable $compareFunction */ |
|
1764
|
506 |
|
$compareFunction = [self::class, 'compareColumnBreaks']; |
|
1765
|
506 |
|
uksort($this->columnBreaks, $compareFunction); |
|
1766
|
|
|
|
|
1767
|
506 |
|
return $this->columnBreaks; |
|
1768
|
|
|
} |
|
1769
|
|
|
|
|
1770
|
|
|
/** |
|
1771
|
|
|
* Set merge on a cell range. |
|
1772
|
|
|
* |
|
1773
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10' |
|
1774
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
1775
|
|
|
* or an AddressRange. |
|
1776
|
|
|
* @param string $behaviour How the merged cells should behave. |
|
1777
|
|
|
* Possible values are: |
|
1778
|
|
|
* MERGE_CELL_CONTENT_EMPTY - Empty the content of the hidden cells |
|
1779
|
|
|
* MERGE_CELL_CONTENT_HIDE - Keep the content of the hidden cells |
|
1780
|
|
|
* MERGE_CELL_CONTENT_MERGE - Move the content of the hidden cells into the first cell |
|
1781
|
|
|
* |
|
1782
|
|
|
* @return $this |
|
1783
|
|
|
*/ |
|
1784
|
174 |
|
public function mergeCells(AddressRange|string|array $range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static |
|
1785
|
|
|
{ |
|
1786
|
174 |
|
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range)); |
|
1787
|
|
|
|
|
1788
|
173 |
|
if (!str_contains($range, ':')) { |
|
1789
|
1 |
|
$range .= ":{$range}"; |
|
1790
|
|
|
} |
|
1791
|
|
|
|
|
1792
|
173 |
|
if (!Preg::isMatch('/^([A-Z]+)(\d+):([A-Z]+)(\d+)$/', $range, $matches)) { |
|
1793
|
1 |
|
throw new Exception('Merge must be on a valid range of cells.'); |
|
1794
|
|
|
} |
|
1795
|
|
|
|
|
1796
|
172 |
|
$this->mergeCells[$range] = $range; |
|
1797
|
172 |
|
$firstRow = (int) $matches[2]; |
|
1798
|
172 |
|
$lastRow = (int) $matches[4]; |
|
1799
|
172 |
|
$firstColumn = $matches[1]; |
|
1800
|
172 |
|
$lastColumn = $matches[3]; |
|
1801
|
172 |
|
$firstColumnIndex = Coordinate::columnIndexFromString($firstColumn); |
|
1802
|
172 |
|
$lastColumnIndex = Coordinate::columnIndexFromString($lastColumn); |
|
1803
|
172 |
|
$numberRows = $lastRow - $firstRow; |
|
1804
|
172 |
|
$numberColumns = $lastColumnIndex - $firstColumnIndex; |
|
1805
|
|
|
|
|
1806
|
172 |
|
if ($numberRows === 1 && $numberColumns === 1) { |
|
1807
|
35 |
|
return $this; |
|
1808
|
|
|
} |
|
1809
|
|
|
|
|
1810
|
|
|
// create upper left cell if it does not already exist |
|
1811
|
165 |
|
$upperLeft = "{$firstColumn}{$firstRow}"; |
|
1812
|
165 |
|
if (!$this->cellExists($upperLeft)) { |
|
1813
|
36 |
|
$this->getCell($upperLeft)->setValueExplicit(null, DataType::TYPE_NULL); |
|
1814
|
|
|
} |
|
1815
|
|
|
|
|
1816
|
165 |
|
if ($behaviour !== self::MERGE_CELL_CONTENT_HIDE) { |
|
1817
|
|
|
// Blank out the rest of the cells in the range (if they exist) |
|
1818
|
58 |
|
if ($numberRows > $numberColumns) { |
|
1819
|
18 |
|
$this->clearMergeCellsByColumn($firstColumn, $lastColumn, $firstRow, $lastRow, $upperLeft, $behaviour); |
|
1820
|
|
|
} else { |
|
1821
|
40 |
|
$this->clearMergeCellsByRow($firstColumn, $lastColumnIndex, $firstRow, $lastRow, $upperLeft, $behaviour); |
|
1822
|
|
|
} |
|
1823
|
|
|
} |
|
1824
|
|
|
|
|
1825
|
165 |
|
return $this; |
|
1826
|
|
|
} |
|
1827
|
|
|
|
|
1828
|
18 |
|
private function clearMergeCellsByColumn(string $firstColumn, string $lastColumn, int $firstRow, int $lastRow, string $upperLeft, string $behaviour): void |
|
1829
|
|
|
{ |
|
1830
|
18 |
|
$leftCellValue = ($behaviour === self::MERGE_CELL_CONTENT_MERGE) |
|
1831
|
|
|
? [$this->getCell($upperLeft)->getFormattedValue()] |
|
1832
|
18 |
|
: []; |
|
1833
|
|
|
|
|
1834
|
18 |
|
foreach ($this->getColumnIterator($firstColumn, $lastColumn) as $column) { |
|
1835
|
18 |
|
$iterator = $column->getCellIterator($firstRow); |
|
1836
|
18 |
|
$iterator->setIterateOnlyExistingCells(true); |
|
1837
|
18 |
|
foreach ($iterator as $cell) { |
|
1838
|
18 |
|
$row = $cell->getRow(); |
|
1839
|
18 |
|
if ($row > $lastRow) { |
|
1840
|
8 |
|
break; |
|
1841
|
|
|
} |
|
1842
|
18 |
|
$leftCellValue = $this->mergeCellBehaviour($cell, $upperLeft, $behaviour, $leftCellValue); |
|
1843
|
|
|
} |
|
1844
|
|
|
} |
|
1845
|
|
|
|
|
1846
|
18 |
|
if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
|
1847
|
|
|
$this->getCell($upperLeft)->setValueExplicit(implode(' ', $leftCellValue), DataType::TYPE_STRING); |
|
1848
|
|
|
} |
|
1849
|
|
|
} |
|
1850
|
|
|
|
|
1851
|
40 |
|
private function clearMergeCellsByRow(string $firstColumn, int $lastColumnIndex, int $firstRow, int $lastRow, string $upperLeft, string $behaviour): void |
|
1852
|
|
|
{ |
|
1853
|
40 |
|
$leftCellValue = ($behaviour === self::MERGE_CELL_CONTENT_MERGE) |
|
1854
|
4 |
|
? [$this->getCell($upperLeft)->getFormattedValue()] |
|
1855
|
36 |
|
: []; |
|
1856
|
|
|
|
|
1857
|
40 |
|
foreach ($this->getRowIterator($firstRow, $lastRow) as $row) { |
|
1858
|
40 |
|
$iterator = $row->getCellIterator($firstColumn); |
|
1859
|
40 |
|
$iterator->setIterateOnlyExistingCells(true); |
|
1860
|
40 |
|
foreach ($iterator as $cell) { |
|
1861
|
40 |
|
$column = $cell->getColumn(); |
|
1862
|
40 |
|
$columnIndex = Coordinate::columnIndexFromString($column); |
|
1863
|
40 |
|
if ($columnIndex > $lastColumnIndex) { |
|
1864
|
9 |
|
break; |
|
1865
|
|
|
} |
|
1866
|
40 |
|
$leftCellValue = $this->mergeCellBehaviour($cell, $upperLeft, $behaviour, $leftCellValue); |
|
1867
|
|
|
} |
|
1868
|
|
|
} |
|
1869
|
|
|
|
|
1870
|
40 |
|
if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
|
1871
|
4 |
|
$this->getCell($upperLeft)->setValueExplicit(implode(' ', $leftCellValue), DataType::TYPE_STRING); |
|
1872
|
|
|
} |
|
1873
|
|
|
} |
|
1874
|
|
|
|
|
1875
|
|
|
/** |
|
1876
|
|
|
* @param mixed[] $leftCellValue |
|
1877
|
|
|
* |
|
1878
|
|
|
* @return mixed[] |
|
1879
|
|
|
*/ |
|
1880
|
58 |
|
public function mergeCellBehaviour(Cell $cell, string $upperLeft, string $behaviour, array $leftCellValue): array |
|
1881
|
|
|
{ |
|
1882
|
58 |
|
if ($cell->getCoordinate() !== $upperLeft) { |
|
1883
|
24 |
|
Calculation::getInstance($cell->getWorksheet()->getParentOrThrow())->flushInstance(); |
|
1884
|
24 |
|
if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
|
1885
|
4 |
|
$cellValue = $cell->getFormattedValue(); |
|
1886
|
4 |
|
if ($cellValue !== '') { |
|
1887
|
4 |
|
$leftCellValue[] = $cellValue; |
|
1888
|
|
|
} |
|
1889
|
|
|
} |
|
1890
|
24 |
|
$cell->setValueExplicit(null, DataType::TYPE_NULL); |
|
1891
|
|
|
} |
|
1892
|
|
|
|
|
1893
|
58 |
|
return $leftCellValue; |
|
1894
|
|
|
} |
|
1895
|
|
|
|
|
1896
|
|
|
/** |
|
1897
|
|
|
* Remove merge on a cell range. |
|
1898
|
|
|
* |
|
1899
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10' |
|
1900
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
1901
|
|
|
* or an AddressRange. |
|
1902
|
|
|
* |
|
1903
|
|
|
* @return $this |
|
1904
|
|
|
*/ |
|
1905
|
23 |
|
public function unmergeCells(AddressRange|string|array $range): static |
|
1906
|
|
|
{ |
|
1907
|
23 |
|
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range)); |
|
1908
|
|
|
|
|
1909
|
23 |
|
if (str_contains($range, ':')) { |
|
1910
|
22 |
|
if (isset($this->mergeCells[$range])) { |
|
1911
|
22 |
|
unset($this->mergeCells[$range]); |
|
1912
|
|
|
} else { |
|
1913
|
|
|
throw new Exception('Cell range ' . $range . ' not known as merged.'); |
|
1914
|
|
|
} |
|
1915
|
|
|
} else { |
|
1916
|
1 |
|
throw new Exception('Merge can only be removed from a range of cells.'); |
|
1917
|
|
|
} |
|
1918
|
|
|
|
|
1919
|
22 |
|
return $this; |
|
1920
|
|
|
} |
|
1921
|
|
|
|
|
1922
|
|
|
/** |
|
1923
|
|
|
* Get merge cells array. |
|
1924
|
|
|
* |
|
1925
|
|
|
* @return string[] |
|
1926
|
|
|
*/ |
|
1927
|
1184 |
|
public function getMergeCells(): array |
|
1928
|
|
|
{ |
|
1929
|
1184 |
|
return $this->mergeCells; |
|
1930
|
|
|
} |
|
1931
|
|
|
|
|
1932
|
|
|
/** |
|
1933
|
|
|
* Set merge cells array for the entire sheet. Use instead mergeCells() to merge |
|
1934
|
|
|
* a single cell range. |
|
1935
|
|
|
* |
|
1936
|
|
|
* @param string[] $mergeCells |
|
1937
|
|
|
* |
|
1938
|
|
|
* @return $this |
|
1939
|
|
|
*/ |
|
1940
|
101 |
|
public function setMergeCells(array $mergeCells): static |
|
1941
|
|
|
{ |
|
1942
|
101 |
|
$this->mergeCells = $mergeCells; |
|
1943
|
|
|
|
|
1944
|
101 |
|
return $this; |
|
1945
|
|
|
} |
|
1946
|
|
|
|
|
1947
|
|
|
/** |
|
1948
|
|
|
* Set protection on a cell or cell range. |
|
1949
|
|
|
* |
|
1950
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10' |
|
1951
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
1952
|
|
|
* or a CellAddress or AddressRange object. |
|
1953
|
|
|
* @param string $password Password to unlock the protection |
|
1954
|
|
|
* @param bool $alreadyHashed If the password has already been hashed, set this to true |
|
1955
|
|
|
* |
|
1956
|
|
|
* @return $this |
|
1957
|
|
|
*/ |
|
1958
|
25 |
|
public function protectCells(AddressRange|CellAddress|int|string|array $range, string $password = '', bool $alreadyHashed = false, string $name = '', string $securityDescriptor = ''): static |
|
1959
|
|
|
{ |
|
1960
|
25 |
|
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range)); |
|
1961
|
|
|
|
|
1962
|
25 |
|
if (!$alreadyHashed && $password !== '') { |
|
1963
|
24 |
|
$password = Shared\PasswordHasher::hashPassword($password); |
|
1964
|
|
|
} |
|
1965
|
25 |
|
$this->protectedCells[$range] = new ProtectedRange($range, $password, $name, $securityDescriptor); |
|
1966
|
|
|
|
|
1967
|
25 |
|
return $this; |
|
1968
|
|
|
} |
|
1969
|
|
|
|
|
1970
|
|
|
/** |
|
1971
|
|
|
* Remove protection on a cell or cell range. |
|
1972
|
|
|
* |
|
1973
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10' |
|
1974
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
1975
|
|
|
* or a CellAddress or AddressRange object. |
|
1976
|
|
|
* |
|
1977
|
|
|
* @return $this |
|
1978
|
|
|
*/ |
|
1979
|
20 |
|
public function unprotectCells(AddressRange|CellAddress|int|string|array $range): static |
|
1980
|
|
|
{ |
|
1981
|
20 |
|
$range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range)); |
|
1982
|
|
|
|
|
1983
|
20 |
|
if (isset($this->protectedCells[$range])) { |
|
1984
|
19 |
|
unset($this->protectedCells[$range]); |
|
1985
|
|
|
} else { |
|
1986
|
1 |
|
throw new Exception('Cell range ' . $range . ' not known as protected.'); |
|
1987
|
|
|
} |
|
1988
|
|
|
|
|
1989
|
19 |
|
return $this; |
|
1990
|
|
|
} |
|
1991
|
|
|
|
|
1992
|
|
|
/** |
|
1993
|
|
|
* Get protected cells. |
|
1994
|
|
|
* |
|
1995
|
|
|
* @return ProtectedRange[] |
|
1996
|
|
|
*/ |
|
1997
|
592 |
|
public function getProtectedCellRanges(): array |
|
1998
|
|
|
{ |
|
1999
|
592 |
|
return $this->protectedCells; |
|
2000
|
|
|
} |
|
2001
|
|
|
|
|
2002
|
|
|
/** |
|
2003
|
|
|
* Get Autofilter. |
|
2004
|
|
|
*/ |
|
2005
|
790 |
|
public function getAutoFilter(): AutoFilter |
|
2006
|
|
|
{ |
|
2007
|
790 |
|
return $this->autoFilter; |
|
2008
|
|
|
} |
|
2009
|
|
|
|
|
2010
|
|
|
/** |
|
2011
|
|
|
* Set AutoFilter. |
|
2012
|
|
|
* |
|
2013
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|AutoFilter|string $autoFilterOrRange |
|
2014
|
|
|
* A simple string containing a Cell range like 'A1:E10' is permitted for backward compatibility |
|
2015
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
2016
|
|
|
* or an AddressRange. |
|
2017
|
|
|
* |
|
2018
|
|
|
* @return $this |
|
2019
|
|
|
*/ |
|
2020
|
20 |
|
public function setAutoFilter(AddressRange|string|array|AutoFilter $autoFilterOrRange): static |
|
2021
|
|
|
{ |
|
2022
|
20 |
|
if (is_object($autoFilterOrRange) && ($autoFilterOrRange instanceof AutoFilter)) { |
|
2023
|
|
|
$this->autoFilter = $autoFilterOrRange; |
|
2024
|
|
|
} else { |
|
2025
|
20 |
|
$cellRange = Functions::trimSheetFromCellReference(Validations::validateCellRange($autoFilterOrRange)); |
|
2026
|
|
|
|
|
2027
|
20 |
|
$this->autoFilter->setRange($cellRange); |
|
2028
|
|
|
} |
|
2029
|
|
|
|
|
2030
|
20 |
|
return $this; |
|
2031
|
|
|
} |
|
2032
|
|
|
|
|
2033
|
|
|
/** |
|
2034
|
|
|
* Remove autofilter. |
|
2035
|
|
|
*/ |
|
2036
|
1 |
|
public function removeAutoFilter(): self |
|
2037
|
|
|
{ |
|
2038
|
1 |
|
$this->autoFilter->setRange(''); |
|
2039
|
|
|
|
|
2040
|
1 |
|
return $this; |
|
2041
|
|
|
} |
|
2042
|
|
|
|
|
2043
|
|
|
/** |
|
2044
|
|
|
* Get collection of Tables. |
|
2045
|
|
|
* |
|
2046
|
|
|
* @return ArrayObject<int, Table> |
|
2047
|
|
|
*/ |
|
2048
|
10225 |
|
public function getTableCollection(): ArrayObject |
|
2049
|
|
|
{ |
|
2050
|
10225 |
|
return $this->tableCollection; |
|
2051
|
|
|
} |
|
2052
|
|
|
|
|
2053
|
|
|
/** |
|
2054
|
|
|
* Add Table. |
|
2055
|
|
|
* |
|
2056
|
|
|
* @return $this |
|
2057
|
|
|
*/ |
|
2058
|
103 |
|
public function addTable(Table $table): self |
|
2059
|
|
|
{ |
|
2060
|
103 |
|
$table->setWorksheet($this); |
|
2061
|
103 |
|
$this->tableCollection[] = $table; |
|
2062
|
|
|
|
|
2063
|
103 |
|
return $this; |
|
2064
|
|
|
} |
|
2065
|
|
|
|
|
2066
|
|
|
/** |
|
2067
|
|
|
* @return string[] array of Table names |
|
2068
|
|
|
*/ |
|
2069
|
1 |
|
public function getTableNames(): array |
|
2070
|
|
|
{ |
|
2071
|
1 |
|
$tableNames = []; |
|
2072
|
|
|
|
|
2073
|
1 |
|
foreach ($this->tableCollection as $table) { |
|
2074
|
|
|
/** @var Table $table */ |
|
2075
|
1 |
|
$tableNames[] = $table->getName(); |
|
2076
|
|
|
} |
|
2077
|
|
|
|
|
2078
|
1 |
|
return $tableNames; |
|
2079
|
|
|
} |
|
2080
|
|
|
|
|
2081
|
|
|
/** |
|
2082
|
|
|
* @param string $name the table name to search |
|
2083
|
|
|
* |
|
2084
|
|
|
* @return null|Table The table from the tables collection, or null if not found |
|
2085
|
|
|
*/ |
|
2086
|
95 |
|
public function getTableByName(string $name): ?Table |
|
2087
|
|
|
{ |
|
2088
|
95 |
|
$tableIndex = $this->getTableIndexByName($name); |
|
2089
|
|
|
|
|
2090
|
95 |
|
return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex]; |
|
2091
|
|
|
} |
|
2092
|
|
|
|
|
2093
|
|
|
/** |
|
2094
|
|
|
* @param string $name the table name to search |
|
2095
|
|
|
* |
|
2096
|
|
|
* @return null|int The index of the located table in the tables collection, or null if not found |
|
2097
|
|
|
*/ |
|
2098
|
96 |
|
protected function getTableIndexByName(string $name): ?int |
|
2099
|
|
|
{ |
|
2100
|
96 |
|
$name = StringHelper::strToUpper($name); |
|
2101
|
96 |
|
foreach ($this->tableCollection as $index => $table) { |
|
2102
|
|
|
/** @var Table $table */ |
|
2103
|
63 |
|
if (StringHelper::strToUpper($table->getName()) === $name) { |
|
2104
|
62 |
|
return $index; |
|
2105
|
|
|
} |
|
2106
|
|
|
} |
|
2107
|
|
|
|
|
2108
|
39 |
|
return null; |
|
2109
|
|
|
} |
|
2110
|
|
|
|
|
2111
|
|
|
/** |
|
2112
|
|
|
* Remove Table by name. |
|
2113
|
|
|
* |
|
2114
|
|
|
* @param string $name Table name |
|
2115
|
|
|
* |
|
2116
|
|
|
* @return $this |
|
2117
|
|
|
*/ |
|
2118
|
1 |
|
public function removeTableByName(string $name): self |
|
2119
|
|
|
{ |
|
2120
|
1 |
|
$tableIndex = $this->getTableIndexByName($name); |
|
2121
|
|
|
|
|
2122
|
1 |
|
if ($tableIndex !== null) { |
|
2123
|
1 |
|
unset($this->tableCollection[$tableIndex]); |
|
2124
|
|
|
} |
|
2125
|
|
|
|
|
2126
|
1 |
|
return $this; |
|
2127
|
|
|
} |
|
2128
|
|
|
|
|
2129
|
|
|
/** |
|
2130
|
|
|
* Remove collection of Tables. |
|
2131
|
|
|
*/ |
|
2132
|
1 |
|
public function removeTableCollection(): self |
|
2133
|
|
|
{ |
|
2134
|
1 |
|
$this->tableCollection = new ArrayObject(); |
|
2135
|
|
|
|
|
2136
|
1 |
|
return $this; |
|
2137
|
|
|
} |
|
2138
|
|
|
|
|
2139
|
|
|
/** |
|
2140
|
|
|
* Get Freeze Pane. |
|
2141
|
|
|
*/ |
|
2142
|
264 |
|
public function getFreezePane(): ?string |
|
2143
|
|
|
{ |
|
2144
|
264 |
|
return $this->freezePane; |
|
2145
|
|
|
} |
|
2146
|
|
|
|
|
2147
|
|
|
/** |
|
2148
|
|
|
* Freeze Pane. |
|
2149
|
|
|
* |
|
2150
|
|
|
* Examples: |
|
2151
|
|
|
* |
|
2152
|
|
|
* - A2 will freeze the rows above cell A2 (i.e row 1) |
|
2153
|
|
|
* - B1 will freeze the columns to the left of cell B1 (i.e column A) |
|
2154
|
|
|
* - B2 will freeze the rows above and to the left of cell B2 (i.e row 1 and column A) |
|
2155
|
|
|
* |
|
2156
|
|
|
* @param null|array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
|
2157
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
2158
|
|
|
* Passing a null value for this argument will clear any existing freeze pane for this worksheet. |
|
2159
|
|
|
* @param null|array{0: int, 1: int}|CellAddress|string $topLeftCell default position of the right bottom pane |
|
2160
|
|
|
* Coordinate of the cell as a string, eg: 'C5'; or as an array of [$columnIndex, $row] (e.g. [3, 5]), |
|
2161
|
|
|
* or a CellAddress object. |
|
2162
|
|
|
* |
|
2163
|
|
|
* @return $this |
|
2164
|
|
|
*/ |
|
2165
|
49 |
|
public function freezePane(null|CellAddress|string|array $coordinate, null|CellAddress|string|array $topLeftCell = null, bool $frozenSplit = false): static |
|
2166
|
|
|
{ |
|
2167
|
49 |
|
$this->panes = [ |
|
2168
|
49 |
|
'bottomRight' => null, |
|
2169
|
49 |
|
'bottomLeft' => null, |
|
2170
|
49 |
|
'topRight' => null, |
|
2171
|
49 |
|
'topLeft' => null, |
|
2172
|
49 |
|
]; |
|
2173
|
49 |
|
$cellAddress = ($coordinate !== null) |
|
2174
|
49 |
|
? Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)) |
|
2175
|
1 |
|
: null; |
|
2176
|
49 |
|
if ($cellAddress !== null && Coordinate::coordinateIsRange($cellAddress)) { |
|
2177
|
1 |
|
throw new Exception('Freeze pane can not be set on a range of cells.'); |
|
2178
|
|
|
} |
|
2179
|
48 |
|
$topLeftCell = ($topLeftCell !== null) |
|
2180
|
37 |
|
? Functions::trimSheetFromCellReference(Validations::validateCellAddress($topLeftCell)) |
|
2181
|
36 |
|
: null; |
|
2182
|
|
|
|
|
2183
|
48 |
|
if ($cellAddress !== null && $topLeftCell === null) { |
|
2184
|
36 |
|
$coordinate = Coordinate::coordinateFromString($cellAddress); |
|
2185
|
36 |
|
$topLeftCell = $coordinate[0] . $coordinate[1]; |
|
2186
|
|
|
} |
|
2187
|
|
|
|
|
2188
|
48 |
|
$topLeftCell = "$topLeftCell"; |
|
2189
|
48 |
|
$this->paneTopLeftCell = $topLeftCell; |
|
2190
|
|
|
|
|
2191
|
48 |
|
$this->freezePane = $cellAddress; |
|
2192
|
48 |
|
$this->topLeftCell = $topLeftCell; |
|
2193
|
48 |
|
if ($cellAddress === null) { |
|
2194
|
1 |
|
$this->paneState = ''; |
|
2195
|
1 |
|
$this->xSplit = $this->ySplit = 0; |
|
2196
|
1 |
|
$this->activePane = ''; |
|
2197
|
|
|
} else { |
|
2198
|
48 |
|
$coordinates = Coordinate::indexesFromString($cellAddress); |
|
2199
|
48 |
|
$this->xSplit = $coordinates[0] - 1; |
|
2200
|
48 |
|
$this->ySplit = $coordinates[1] - 1; |
|
2201
|
48 |
|
if ($this->xSplit > 0 || $this->ySplit > 0) { |
|
2202
|
47 |
|
$this->paneState = $frozenSplit ? self::PANE_FROZENSPLIT : self::PANE_FROZEN; |
|
2203
|
47 |
|
$this->setSelectedCellsActivePane(); |
|
2204
|
|
|
} else { |
|
2205
|
1 |
|
$this->paneState = ''; |
|
2206
|
1 |
|
$this->freezePane = null; |
|
2207
|
1 |
|
$this->activePane = ''; |
|
2208
|
|
|
} |
|
2209
|
|
|
} |
|
2210
|
|
|
|
|
2211
|
48 |
|
return $this; |
|
2212
|
|
|
} |
|
2213
|
|
|
|
|
2214
|
47 |
|
public function setTopLeftCell(string $topLeftCell): self |
|
2215
|
|
|
{ |
|
2216
|
47 |
|
$this->topLeftCell = $topLeftCell; |
|
2217
|
|
|
|
|
2218
|
47 |
|
return $this; |
|
2219
|
|
|
} |
|
2220
|
|
|
|
|
2221
|
|
|
/** |
|
2222
|
|
|
* Unfreeze Pane. |
|
2223
|
|
|
* |
|
2224
|
|
|
* @return $this |
|
2225
|
|
|
*/ |
|
2226
|
1 |
|
public function unfreezePane(): static |
|
2227
|
|
|
{ |
|
2228
|
1 |
|
return $this->freezePane(null); |
|
2229
|
|
|
} |
|
2230
|
|
|
|
|
2231
|
|
|
/** |
|
2232
|
|
|
* Get the default position of the right bottom pane. |
|
2233
|
|
|
*/ |
|
2234
|
448 |
|
public function getTopLeftCell(): ?string |
|
2235
|
|
|
{ |
|
2236
|
448 |
|
return $this->topLeftCell; |
|
2237
|
|
|
} |
|
2238
|
|
|
|
|
2239
|
11 |
|
public function getPaneTopLeftCell(): string |
|
2240
|
|
|
{ |
|
2241
|
11 |
|
return $this->paneTopLeftCell; |
|
2242
|
|
|
} |
|
2243
|
|
|
|
|
2244
|
26 |
|
public function setPaneTopLeftCell(string $paneTopLeftCell): self |
|
2245
|
|
|
{ |
|
2246
|
26 |
|
$this->paneTopLeftCell = $paneTopLeftCell; |
|
2247
|
|
|
|
|
2248
|
26 |
|
return $this; |
|
2249
|
|
|
} |
|
2250
|
|
|
|
|
2251
|
436 |
|
public function usesPanes(): bool |
|
2252
|
|
|
{ |
|
2253
|
436 |
|
return $this->xSplit > 0 || $this->ySplit > 0; |
|
2254
|
|
|
} |
|
2255
|
|
|
|
|
2256
|
2 |
|
public function getPane(string $position): ?Pane |
|
2257
|
|
|
{ |
|
2258
|
2 |
|
return $this->panes[$position] ?? null; |
|
2259
|
|
|
} |
|
2260
|
|
|
|
|
2261
|
35 |
|
public function setPane(string $position, ?Pane $pane): self |
|
2262
|
|
|
{ |
|
2263
|
35 |
|
if (array_key_exists($position, $this->panes)) { |
|
2264
|
35 |
|
$this->panes[$position] = $pane; |
|
2265
|
|
|
} |
|
2266
|
|
|
|
|
2267
|
35 |
|
return $this; |
|
2268
|
|
|
} |
|
2269
|
|
|
|
|
2270
|
|
|
/** @return (null|Pane)[] */ |
|
2271
|
3 |
|
public function getPanes(): array |
|
2272
|
|
|
{ |
|
2273
|
3 |
|
return $this->panes; |
|
2274
|
|
|
} |
|
2275
|
|
|
|
|
2276
|
14 |
|
public function getActivePane(): string |
|
2277
|
|
|
{ |
|
2278
|
14 |
|
return $this->activePane; |
|
2279
|
|
|
} |
|
2280
|
|
|
|
|
2281
|
48 |
|
public function setActivePane(string $activePane): self |
|
2282
|
|
|
{ |
|
2283
|
48 |
|
$this->activePane = array_key_exists($activePane, $this->panes) ? $activePane : ''; |
|
2284
|
|
|
|
|
2285
|
48 |
|
return $this; |
|
2286
|
|
|
} |
|
2287
|
|
|
|
|
2288
|
11 |
|
public function getXSplit(): int |
|
2289
|
|
|
{ |
|
2290
|
11 |
|
return $this->xSplit; |
|
2291
|
|
|
} |
|
2292
|
|
|
|
|
2293
|
11 |
|
public function setXSplit(int $xSplit): self |
|
2294
|
|
|
{ |
|
2295
|
11 |
|
$this->xSplit = $xSplit; |
|
2296
|
11 |
|
if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
|
2297
|
1 |
|
$this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
|
2298
|
|
|
} |
|
2299
|
|
|
|
|
2300
|
11 |
|
return $this; |
|
2301
|
|
|
} |
|
2302
|
|
|
|
|
2303
|
11 |
|
public function getYSplit(): int |
|
2304
|
|
|
{ |
|
2305
|
11 |
|
return $this->ySplit; |
|
2306
|
|
|
} |
|
2307
|
|
|
|
|
2308
|
26 |
|
public function setYSplit(int $ySplit): self |
|
2309
|
|
|
{ |
|
2310
|
26 |
|
$this->ySplit = $ySplit; |
|
2311
|
26 |
|
if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
|
2312
|
1 |
|
$this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
|
2313
|
|
|
} |
|
2314
|
|
|
|
|
2315
|
26 |
|
return $this; |
|
2316
|
|
|
} |
|
2317
|
|
|
|
|
2318
|
21 |
|
public function getPaneState(): string |
|
2319
|
|
|
{ |
|
2320
|
21 |
|
return $this->paneState; |
|
2321
|
|
|
} |
|
2322
|
|
|
|
|
2323
|
|
|
public const PANE_FROZEN = 'frozen'; |
|
2324
|
|
|
public const PANE_FROZENSPLIT = 'frozenSplit'; |
|
2325
|
|
|
public const PANE_SPLIT = 'split'; |
|
2326
|
|
|
private const VALIDPANESTATE = [self::PANE_FROZEN, self::PANE_SPLIT, self::PANE_FROZENSPLIT]; |
|
2327
|
|
|
private const VALIDFROZENSTATE = [self::PANE_FROZEN, self::PANE_FROZENSPLIT]; |
|
2328
|
|
|
|
|
2329
|
26 |
|
public function setPaneState(string $paneState): self |
|
2330
|
|
|
{ |
|
2331
|
26 |
|
$this->paneState = in_array($paneState, self::VALIDPANESTATE, true) ? $paneState : ''; |
|
2332
|
26 |
|
if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
|
2333
|
25 |
|
$this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
|
2334
|
|
|
} else { |
|
2335
|
3 |
|
$this->freezePane = null; |
|
2336
|
|
|
} |
|
2337
|
|
|
|
|
2338
|
26 |
|
return $this; |
|
2339
|
|
|
} |
|
2340
|
|
|
|
|
2341
|
|
|
/** |
|
2342
|
|
|
* Insert a new row, updating all possible related data. |
|
2343
|
|
|
* |
|
2344
|
|
|
* @param int $before Insert before this row number |
|
2345
|
|
|
* @param int $numberOfRows Number of new rows to insert |
|
2346
|
|
|
* |
|
2347
|
|
|
* @return $this |
|
2348
|
|
|
*/ |
|
2349
|
38 |
|
public function insertNewRowBefore(int $before, int $numberOfRows = 1): static |
|
2350
|
|
|
{ |
|
2351
|
38 |
|
if ($before >= 1) { |
|
2352
|
37 |
|
$objReferenceHelper = ReferenceHelper::getInstance(); |
|
2353
|
37 |
|
$objReferenceHelper->insertNewBefore('A' . $before, 0, $numberOfRows, $this); |
|
2354
|
|
|
} else { |
|
2355
|
1 |
|
throw new Exception('Rows can only be inserted before at least row 1.'); |
|
2356
|
|
|
} |
|
2357
|
|
|
|
|
2358
|
37 |
|
return $this; |
|
2359
|
|
|
} |
|
2360
|
|
|
|
|
2361
|
|
|
/** |
|
2362
|
|
|
* Insert a new column, updating all possible related data. |
|
2363
|
|
|
* |
|
2364
|
|
|
* @param string $before Insert before this column Name, eg: 'A' |
|
2365
|
|
|
* @param int $numberOfColumns Number of new columns to insert |
|
2366
|
|
|
* |
|
2367
|
|
|
* @return $this |
|
2368
|
|
|
*/ |
|
2369
|
46 |
|
public function insertNewColumnBefore(string $before, int $numberOfColumns = 1): static |
|
2370
|
|
|
{ |
|
2371
|
46 |
|
if (!is_numeric($before)) { |
|
2372
|
45 |
|
$objReferenceHelper = ReferenceHelper::getInstance(); |
|
2373
|
45 |
|
$objReferenceHelper->insertNewBefore($before . '1', $numberOfColumns, 0, $this); |
|
2374
|
|
|
} else { |
|
2375
|
1 |
|
throw new Exception('Column references should not be numeric.'); |
|
2376
|
|
|
} |
|
2377
|
|
|
|
|
2378
|
45 |
|
return $this; |
|
2379
|
|
|
} |
|
2380
|
|
|
|
|
2381
|
|
|
/** |
|
2382
|
|
|
* Insert a new column, updating all possible related data. |
|
2383
|
|
|
* |
|
2384
|
|
|
* @param int $beforeColumnIndex Insert before this column ID (numeric column coordinate of the cell) |
|
2385
|
|
|
* @param int $numberOfColumns Number of new columns to insert |
|
2386
|
|
|
* |
|
2387
|
|
|
* @return $this |
|
2388
|
|
|
*/ |
|
2389
|
2 |
|
public function insertNewColumnBeforeByIndex(int $beforeColumnIndex, int $numberOfColumns = 1): static |
|
2390
|
|
|
{ |
|
2391
|
2 |
|
if ($beforeColumnIndex >= 1) { |
|
2392
|
1 |
|
return $this->insertNewColumnBefore(Coordinate::stringFromColumnIndex($beforeColumnIndex), $numberOfColumns); |
|
2393
|
|
|
} |
|
2394
|
|
|
|
|
2395
|
1 |
|
throw new Exception('Columns can only be inserted before at least column A (1).'); |
|
2396
|
|
|
} |
|
2397
|
|
|
|
|
2398
|
|
|
/** |
|
2399
|
|
|
* Delete a row, updating all possible related data. |
|
2400
|
|
|
* |
|
2401
|
|
|
* @param int $row Remove rows, starting with this row number |
|
2402
|
|
|
* @param int $numberOfRows Number of rows to remove |
|
2403
|
|
|
* |
|
2404
|
|
|
* @return $this |
|
2405
|
|
|
*/ |
|
2406
|
44 |
|
public function removeRow(int $row, int $numberOfRows = 1): static |
|
2407
|
|
|
{ |
|
2408
|
44 |
|
if ($row < 1) { |
|
2409
|
1 |
|
throw new Exception('Rows to be deleted should at least start from row 1.'); |
|
2410
|
|
|
} |
|
2411
|
43 |
|
$startRow = $row; |
|
2412
|
43 |
|
$endRow = $startRow + $numberOfRows - 1; |
|
2413
|
43 |
|
$removeKeys = []; |
|
2414
|
43 |
|
$addKeys = []; |
|
2415
|
43 |
|
foreach ($this->mergeCells as $key => $value) { |
|
2416
|
|
|
if ( |
|
2417
|
21 |
|
Preg::isMatch( |
|
2418
|
21 |
|
'/^([a-z]{1,3})(\d+):([a-z]{1,3})(\d+)/i', |
|
2419
|
21 |
|
$key, |
|
2420
|
21 |
|
$matches |
|
2421
|
21 |
|
) |
|
2422
|
|
|
) { |
|
2423
|
21 |
|
$startMergeInt = (int) $matches[2]; |
|
2424
|
21 |
|
$endMergeInt = (int) $matches[4]; |
|
2425
|
21 |
|
if ($startMergeInt >= $startRow) { |
|
2426
|
21 |
|
if ($startMergeInt <= $endRow) { |
|
2427
|
3 |
|
$removeKeys[] = $key; |
|
2428
|
|
|
} |
|
2429
|
1 |
|
} elseif ($endMergeInt >= $startRow) { |
|
2430
|
1 |
|
if ($endMergeInt <= $endRow) { |
|
2431
|
1 |
|
$temp = $endMergeInt - 1; |
|
2432
|
1 |
|
$removeKeys[] = $key; |
|
2433
|
1 |
|
if ($temp !== $startMergeInt) { |
|
2434
|
1 |
|
$temp3 = $matches[1] . $matches[2] . ':' . $matches[3] . $temp; |
|
2435
|
1 |
|
$addKeys[] = $temp3; |
|
2436
|
|
|
} |
|
2437
|
|
|
} |
|
2438
|
|
|
} |
|
2439
|
|
|
} |
|
2440
|
|
|
} |
|
2441
|
43 |
|
foreach ($removeKeys as $key) { |
|
2442
|
3 |
|
unset($this->mergeCells[$key]); |
|
2443
|
|
|
} |
|
2444
|
43 |
|
foreach ($addKeys as $key) { |
|
2445
|
1 |
|
$this->mergeCells[$key] = $key; |
|
2446
|
|
|
} |
|
2447
|
|
|
|
|
2448
|
43 |
|
$holdRowDimensions = $this->removeRowDimensions($row, $numberOfRows); |
|
2449
|
43 |
|
$highestRow = $this->getHighestDataRow(); |
|
2450
|
43 |
|
$removedRowsCounter = 0; |
|
2451
|
|
|
|
|
2452
|
43 |
|
for ($r = 0; $r < $numberOfRows; ++$r) { |
|
2453
|
43 |
|
if ($row + $r <= $highestRow) { |
|
2454
|
39 |
|
$this->cellCollection->removeRow($row + $r); |
|
2455
|
39 |
|
++$removedRowsCounter; |
|
2456
|
|
|
} |
|
2457
|
|
|
} |
|
2458
|
|
|
|
|
2459
|
43 |
|
$objReferenceHelper = ReferenceHelper::getInstance(); |
|
2460
|
43 |
|
$objReferenceHelper->insertNewBefore('A' . ($row + $numberOfRows), 0, -$numberOfRows, $this); |
|
2461
|
43 |
|
for ($r = 0; $r < $removedRowsCounter; ++$r) { |
|
2462
|
39 |
|
$this->cellCollection->removeRow($highestRow); |
|
2463
|
39 |
|
--$highestRow; |
|
2464
|
|
|
} |
|
2465
|
|
|
|
|
2466
|
43 |
|
$this->rowDimensions = $holdRowDimensions; |
|
2467
|
|
|
|
|
2468
|
43 |
|
return $this; |
|
2469
|
|
|
} |
|
2470
|
|
|
|
|
2471
|
|
|
/** @return RowDimension[] */ |
|
2472
|
43 |
|
private function removeRowDimensions(int $row, int $numberOfRows): array |
|
2473
|
|
|
{ |
|
2474
|
43 |
|
$highRow = $row + $numberOfRows - 1; |
|
2475
|
43 |
|
$holdRowDimensions = []; |
|
2476
|
43 |
|
foreach ($this->rowDimensions as $rowDimension) { |
|
2477
|
4 |
|
$num = $rowDimension->getRowIndex(); |
|
2478
|
4 |
|
if ($num < $row) { |
|
2479
|
3 |
|
$holdRowDimensions[$num] = $rowDimension; |
|
2480
|
4 |
|
} elseif ($num > $highRow) { |
|
2481
|
4 |
|
$num -= $numberOfRows; |
|
2482
|
4 |
|
$cloneDimension = clone $rowDimension; |
|
2483
|
4 |
|
$cloneDimension->setRowIndex($num); |
|
2484
|
4 |
|
$holdRowDimensions[$num] = $cloneDimension; |
|
2485
|
|
|
} |
|
2486
|
|
|
} |
|
2487
|
|
|
|
|
2488
|
43 |
|
return $holdRowDimensions; |
|
2489
|
|
|
} |
|
2490
|
|
|
|
|
2491
|
|
|
/** |
|
2492
|
|
|
* Remove a column, updating all possible related data. |
|
2493
|
|
|
* |
|
2494
|
|
|
* @param string $column Remove columns starting with this column name, eg: 'A' |
|
2495
|
|
|
* @param int $numberOfColumns Number of columns to remove |
|
2496
|
|
|
* |
|
2497
|
|
|
* @return $this |
|
2498
|
|
|
*/ |
|
2499
|
36 |
|
public function removeColumn(string $column, int $numberOfColumns = 1): static |
|
2500
|
|
|
{ |
|
2501
|
36 |
|
if (is_numeric($column)) { |
|
2502
|
1 |
|
throw new Exception('Column references should not be numeric.'); |
|
2503
|
|
|
} |
|
2504
|
35 |
|
$startColumnInt = Coordinate::columnIndexFromString($column); |
|
2505
|
35 |
|
$endColumnInt = $startColumnInt + $numberOfColumns - 1; |
|
2506
|
35 |
|
$removeKeys = []; |
|
2507
|
35 |
|
$addKeys = []; |
|
2508
|
35 |
|
foreach ($this->mergeCells as $key => $value) { |
|
2509
|
|
|
if ( |
|
2510
|
19 |
|
Preg::isMatch( |
|
2511
|
19 |
|
'/^([a-z]{1,3})(\d+):([a-z]{1,3})(\d+)/i', |
|
2512
|
19 |
|
$key, |
|
2513
|
19 |
|
$matches |
|
2514
|
19 |
|
) |
|
2515
|
|
|
) { |
|
2516
|
19 |
|
$startMergeInt = Coordinate::columnIndexFromString($matches[1]); |
|
2517
|
19 |
|
$endMergeInt = Coordinate::columnIndexFromString($matches[3]); |
|
2518
|
19 |
|
if ($startMergeInt >= $startColumnInt) { |
|
2519
|
2 |
|
if ($startMergeInt <= $endColumnInt) { |
|
2520
|
2 |
|
$removeKeys[] = $key; |
|
2521
|
|
|
} |
|
2522
|
18 |
|
} elseif ($endMergeInt >= $startColumnInt) { |
|
2523
|
18 |
|
if ($endMergeInt <= $endColumnInt) { |
|
2524
|
1 |
|
$temp = Coordinate::columnIndexFromString($matches[3]) - 1; |
|
2525
|
1 |
|
$temp2 = Coordinate::stringFromColumnIndex($temp); |
|
2526
|
1 |
|
$removeKeys[] = $key; |
|
2527
|
1 |
|
if ($temp2 !== $matches[1]) { |
|
2528
|
1 |
|
$temp3 = $matches[1] . $matches[2] . ':' . $temp2 . $matches[4]; |
|
2529
|
1 |
|
$addKeys[] = $temp3; |
|
2530
|
|
|
} |
|
2531
|
|
|
} |
|
2532
|
|
|
} |
|
2533
|
|
|
} |
|
2534
|
|
|
} |
|
2535
|
35 |
|
foreach ($removeKeys as $key) { |
|
2536
|
2 |
|
unset($this->mergeCells[$key]); |
|
2537
|
|
|
} |
|
2538
|
35 |
|
foreach ($addKeys as $key) { |
|
2539
|
1 |
|
$this->mergeCells[$key] = $key; |
|
2540
|
|
|
} |
|
2541
|
|
|
|
|
2542
|
35 |
|
$highestColumn = $this->getHighestDataColumn(); |
|
2543
|
35 |
|
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); |
|
2544
|
35 |
|
$pColumnIndex = Coordinate::columnIndexFromString($column); |
|
2545
|
|
|
|
|
2546
|
35 |
|
$holdColumnDimensions = $this->removeColumnDimensions($pColumnIndex, $numberOfColumns); |
|
2547
|
|
|
|
|
2548
|
35 |
|
$column = Coordinate::stringFromColumnIndex($pColumnIndex + $numberOfColumns); |
|
2549
|
35 |
|
$objReferenceHelper = ReferenceHelper::getInstance(); |
|
2550
|
35 |
|
$objReferenceHelper->insertNewBefore($column . '1', -$numberOfColumns, 0, $this); |
|
2551
|
|
|
|
|
2552
|
35 |
|
$this->columnDimensions = $holdColumnDimensions; |
|
2553
|
|
|
|
|
2554
|
35 |
|
if ($pColumnIndex > $highestColumnIndex) { |
|
2555
|
2 |
|
return $this; |
|
2556
|
|
|
} |
|
2557
|
|
|
|
|
2558
|
33 |
|
$maxPossibleColumnsToBeRemoved = $highestColumnIndex - $pColumnIndex + 1; |
|
2559
|
|
|
|
|
2560
|
33 |
|
for ($c = 0, $n = min($maxPossibleColumnsToBeRemoved, $numberOfColumns); $c < $n; ++$c) { |
|
2561
|
33 |
|
$this->cellCollection->removeColumn($highestColumn); |
|
2562
|
33 |
|
$highestColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($highestColumn) - 1); |
|
2563
|
|
|
} |
|
2564
|
|
|
|
|
2565
|
33 |
|
$this->garbageCollect(); |
|
2566
|
|
|
|
|
2567
|
33 |
|
return $this; |
|
2568
|
|
|
} |
|
2569
|
|
|
|
|
2570
|
|
|
/** @return ColumnDimension[] */ |
|
2571
|
35 |
|
private function removeColumnDimensions(int $pColumnIndex, int $numberOfColumns): array |
|
2572
|
|
|
{ |
|
2573
|
35 |
|
$highCol = $pColumnIndex + $numberOfColumns - 1; |
|
2574
|
35 |
|
$holdColumnDimensions = []; |
|
2575
|
35 |
|
foreach ($this->columnDimensions as $columnDimension) { |
|
2576
|
18 |
|
$num = $columnDimension->getColumnNumeric(); |
|
2577
|
18 |
|
if ($num < $pColumnIndex) { |
|
2578
|
18 |
|
$str = $columnDimension->getColumnIndex(); |
|
2579
|
18 |
|
$holdColumnDimensions[$str] = $columnDimension; |
|
2580
|
18 |
|
} elseif ($num > $highCol) { |
|
2581
|
18 |
|
$cloneDimension = clone $columnDimension; |
|
2582
|
18 |
|
$cloneDimension->setColumnNumeric($num - $numberOfColumns); |
|
2583
|
18 |
|
$str = $cloneDimension->getColumnIndex(); |
|
2584
|
18 |
|
$holdColumnDimensions[$str] = $cloneDimension; |
|
2585
|
|
|
} |
|
2586
|
|
|
} |
|
2587
|
|
|
|
|
2588
|
35 |
|
return $holdColumnDimensions; |
|
2589
|
|
|
} |
|
2590
|
|
|
|
|
2591
|
|
|
/** |
|
2592
|
|
|
* Remove a column, updating all possible related data. |
|
2593
|
|
|
* |
|
2594
|
|
|
* @param int $columnIndex Remove starting with this column Index (numeric column coordinate) |
|
2595
|
|
|
* @param int $numColumns Number of columns to remove |
|
2596
|
|
|
* |
|
2597
|
|
|
* @return $this |
|
2598
|
|
|
*/ |
|
2599
|
3 |
|
public function removeColumnByIndex(int $columnIndex, int $numColumns = 1): static |
|
2600
|
|
|
{ |
|
2601
|
3 |
|
if ($columnIndex >= 1) { |
|
2602
|
2 |
|
return $this->removeColumn(Coordinate::stringFromColumnIndex($columnIndex), $numColumns); |
|
2603
|
|
|
} |
|
2604
|
|
|
|
|
2605
|
1 |
|
throw new Exception('Columns to be deleted should at least start from column A (1)'); |
|
2606
|
|
|
} |
|
2607
|
|
|
|
|
2608
|
|
|
/** |
|
2609
|
|
|
* Show gridlines? |
|
2610
|
|
|
*/ |
|
2611
|
1036 |
|
public function getShowGridlines(): bool |
|
2612
|
|
|
{ |
|
2613
|
1036 |
|
return $this->showGridlines; |
|
2614
|
|
|
} |
|
2615
|
|
|
|
|
2616
|
|
|
/** |
|
2617
|
|
|
* Set show gridlines. |
|
2618
|
|
|
* |
|
2619
|
|
|
* @param bool $showGridLines Show gridlines (true/false) |
|
2620
|
|
|
* |
|
2621
|
|
|
* @return $this |
|
2622
|
|
|
*/ |
|
2623
|
856 |
|
public function setShowGridlines(bool $showGridLines): self |
|
2624
|
|
|
{ |
|
2625
|
856 |
|
$this->showGridlines = $showGridLines; |
|
2626
|
|
|
|
|
2627
|
856 |
|
return $this; |
|
2628
|
|
|
} |
|
2629
|
|
|
|
|
2630
|
|
|
/** |
|
2631
|
|
|
* Print gridlines? |
|
2632
|
|
|
*/ |
|
2633
|
1042 |
|
public function getPrintGridlines(): bool |
|
2634
|
|
|
{ |
|
2635
|
1042 |
|
return $this->printGridlines; |
|
2636
|
|
|
} |
|
2637
|
|
|
|
|
2638
|
|
|
/** |
|
2639
|
|
|
* Set print gridlines. |
|
2640
|
|
|
* |
|
2641
|
|
|
* @param bool $printGridLines Print gridlines (true/false) |
|
2642
|
|
|
* |
|
2643
|
|
|
* @return $this |
|
2644
|
|
|
*/ |
|
2645
|
579 |
|
public function setPrintGridlines(bool $printGridLines): self |
|
2646
|
|
|
{ |
|
2647
|
579 |
|
$this->printGridlines = $printGridLines; |
|
2648
|
|
|
|
|
2649
|
579 |
|
return $this; |
|
2650
|
|
|
} |
|
2651
|
|
|
|
|
2652
|
|
|
/** |
|
2653
|
|
|
* Show row and column headers? |
|
2654
|
|
|
*/ |
|
2655
|
503 |
|
public function getShowRowColHeaders(): bool |
|
2656
|
|
|
{ |
|
2657
|
503 |
|
return $this->showRowColHeaders; |
|
2658
|
|
|
} |
|
2659
|
|
|
|
|
2660
|
|
|
/** |
|
2661
|
|
|
* Set show row and column headers. |
|
2662
|
|
|
* |
|
2663
|
|
|
* @param bool $showRowColHeaders Show row and column headers (true/false) |
|
2664
|
|
|
* |
|
2665
|
|
|
* @return $this |
|
2666
|
|
|
*/ |
|
2667
|
391 |
|
public function setShowRowColHeaders(bool $showRowColHeaders): self |
|
2668
|
|
|
{ |
|
2669
|
391 |
|
$this->showRowColHeaders = $showRowColHeaders; |
|
2670
|
|
|
|
|
2671
|
391 |
|
return $this; |
|
2672
|
|
|
} |
|
2673
|
|
|
|
|
2674
|
|
|
/** |
|
2675
|
|
|
* Show summary below? (Row/Column outlining). |
|
2676
|
|
|
*/ |
|
2677
|
504 |
|
public function getShowSummaryBelow(): bool |
|
2678
|
|
|
{ |
|
2679
|
504 |
|
return $this->showSummaryBelow; |
|
2680
|
|
|
} |
|
2681
|
|
|
|
|
2682
|
|
|
/** |
|
2683
|
|
|
* Set show summary below. |
|
2684
|
|
|
* |
|
2685
|
|
|
* @param bool $showSummaryBelow Show summary below (true/false) |
|
2686
|
|
|
* |
|
2687
|
|
|
* @return $this |
|
2688
|
|
|
*/ |
|
2689
|
399 |
|
public function setShowSummaryBelow(bool $showSummaryBelow): self |
|
2690
|
|
|
{ |
|
2691
|
399 |
|
$this->showSummaryBelow = $showSummaryBelow; |
|
2692
|
|
|
|
|
2693
|
399 |
|
return $this; |
|
2694
|
|
|
} |
|
2695
|
|
|
|
|
2696
|
|
|
/** |
|
2697
|
|
|
* Show summary right? (Row/Column outlining). |
|
2698
|
|
|
*/ |
|
2699
|
504 |
|
public function getShowSummaryRight(): bool |
|
2700
|
|
|
{ |
|
2701
|
504 |
|
return $this->showSummaryRight; |
|
2702
|
|
|
} |
|
2703
|
|
|
|
|
2704
|
|
|
/** |
|
2705
|
|
|
* Set show summary right. |
|
2706
|
|
|
* |
|
2707
|
|
|
* @param bool $showSummaryRight Show summary right (true/false) |
|
2708
|
|
|
* |
|
2709
|
|
|
* @return $this |
|
2710
|
|
|
*/ |
|
2711
|
399 |
|
public function setShowSummaryRight(bool $showSummaryRight): self |
|
2712
|
|
|
{ |
|
2713
|
399 |
|
$this->showSummaryRight = $showSummaryRight; |
|
2714
|
|
|
|
|
2715
|
399 |
|
return $this; |
|
2716
|
|
|
} |
|
2717
|
|
|
|
|
2718
|
|
|
/** |
|
2719
|
|
|
* Get comments. |
|
2720
|
|
|
* |
|
2721
|
|
|
* @return Comment[] |
|
2722
|
|
|
*/ |
|
2723
|
1074 |
|
public function getComments(): array |
|
2724
|
|
|
{ |
|
2725
|
1074 |
|
return $this->comments; |
|
2726
|
|
|
} |
|
2727
|
|
|
|
|
2728
|
|
|
/** |
|
2729
|
|
|
* Set comments array for the entire sheet. |
|
2730
|
|
|
* |
|
2731
|
|
|
* @param Comment[] $comments |
|
2732
|
|
|
* |
|
2733
|
|
|
* @return $this |
|
2734
|
|
|
*/ |
|
2735
|
101 |
|
public function setComments(array $comments): self |
|
2736
|
|
|
{ |
|
2737
|
101 |
|
$this->comments = $comments; |
|
2738
|
|
|
|
|
2739
|
101 |
|
return $this; |
|
2740
|
|
|
} |
|
2741
|
|
|
|
|
2742
|
|
|
/** |
|
2743
|
|
|
* Remove comment from cell. |
|
2744
|
|
|
* |
|
2745
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; |
|
2746
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
2747
|
|
|
* |
|
2748
|
|
|
* @return $this |
|
2749
|
|
|
*/ |
|
2750
|
49 |
|
public function removeComment(CellAddress|string|array $cellCoordinate): self |
|
2751
|
|
|
{ |
|
2752
|
49 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); |
|
2753
|
|
|
|
|
2754
|
49 |
|
if (Coordinate::coordinateIsRange($cellAddress)) { |
|
2755
|
1 |
|
throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
2756
|
48 |
|
} elseif (str_contains($cellAddress, '$')) { |
|
2757
|
1 |
|
throw new Exception('Cell coordinate string must not be absolute.'); |
|
2758
|
47 |
|
} elseif ($cellAddress == '') { |
|
2759
|
1 |
|
throw new Exception('Cell coordinate can not be zero-length string.'); |
|
2760
|
|
|
} |
|
2761
|
|
|
// Check if we have a comment for this cell and delete it |
|
2762
|
46 |
|
if (isset($this->comments[$cellAddress])) { |
|
2763
|
2 |
|
unset($this->comments[$cellAddress]); |
|
2764
|
|
|
} |
|
2765
|
|
|
|
|
2766
|
46 |
|
return $this; |
|
2767
|
|
|
} |
|
2768
|
|
|
|
|
2769
|
|
|
/** |
|
2770
|
|
|
* Get comment for cell. |
|
2771
|
|
|
* |
|
2772
|
|
|
* @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; |
|
2773
|
|
|
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
|
2774
|
|
|
*/ |
|
2775
|
110 |
|
public function getComment(CellAddress|string|array $cellCoordinate, bool $attachNew = true): Comment |
|
2776
|
|
|
{ |
|
2777
|
110 |
|
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); |
|
2778
|
|
|
|
|
2779
|
110 |
|
if (Coordinate::coordinateIsRange($cellAddress)) { |
|
2780
|
1 |
|
throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
2781
|
109 |
|
} elseif (str_contains($cellAddress, '$')) { |
|
2782
|
1 |
|
throw new Exception('Cell coordinate string must not be absolute.'); |
|
2783
|
108 |
|
} elseif ($cellAddress == '') { |
|
2784
|
1 |
|
throw new Exception('Cell coordinate can not be zero-length string.'); |
|
2785
|
|
|
} |
|
2786
|
|
|
|
|
2787
|
|
|
// Check if we already have a comment for this cell. |
|
2788
|
107 |
|
if (isset($this->comments[$cellAddress])) { |
|
2789
|
74 |
|
return $this->comments[$cellAddress]; |
|
2790
|
|
|
} |
|
2791
|
|
|
|
|
2792
|
|
|
// If not, create a new comment. |
|
2793
|
107 |
|
$newComment = new Comment(); |
|
2794
|
107 |
|
if ($attachNew) { |
|
2795
|
107 |
|
$this->comments[$cellAddress] = $newComment; |
|
2796
|
|
|
} |
|
2797
|
|
|
|
|
2798
|
107 |
|
return $newComment; |
|
2799
|
|
|
} |
|
2800
|
|
|
|
|
2801
|
|
|
/** |
|
2802
|
|
|
* Get active cell. |
|
2803
|
|
|
* |
|
2804
|
|
|
* @return string Example: 'A1' |
|
2805
|
|
|
*/ |
|
2806
|
10272 |
|
public function getActiveCell(): string |
|
2807
|
|
|
{ |
|
2808
|
10272 |
|
return $this->activeCell; |
|
2809
|
|
|
} |
|
2810
|
|
|
|
|
2811
|
|
|
/** |
|
2812
|
|
|
* Get selected cells. |
|
2813
|
|
|
*/ |
|
2814
|
10319 |
|
public function getSelectedCells(): string |
|
2815
|
|
|
{ |
|
2816
|
10319 |
|
return $this->selectedCells; |
|
2817
|
|
|
} |
|
2818
|
|
|
|
|
2819
|
|
|
/** |
|
2820
|
|
|
* Selected cell. |
|
2821
|
|
|
* |
|
2822
|
|
|
* @param string $coordinate Cell (i.e. A1) |
|
2823
|
|
|
* |
|
2824
|
|
|
* @return $this |
|
2825
|
|
|
*/ |
|
2826
|
38 |
|
public function setSelectedCell(string $coordinate): static |
|
2827
|
|
|
{ |
|
2828
|
38 |
|
return $this->setSelectedCells($coordinate); |
|
2829
|
|
|
} |
|
2830
|
|
|
|
|
2831
|
|
|
/** |
|
2832
|
|
|
* Select a range of cells. |
|
2833
|
|
|
* |
|
2834
|
|
|
* @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10' |
|
2835
|
|
|
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
|
2836
|
|
|
* or a CellAddress or AddressRange object. |
|
2837
|
|
|
* |
|
2838
|
|
|
* @return $this |
|
2839
|
|
|
*/ |
|
2840
|
10291 |
|
public function setSelectedCells(AddressRange|CellAddress|int|string|array $coordinate): static |
|
2841
|
|
|
{ |
|
2842
|
10291 |
|
if (is_string($coordinate)) { |
|
2843
|
10291 |
|
$coordinate = Validations::definedNameToCoordinate($coordinate, $this); |
|
2844
|
|
|
} |
|
2845
|
10291 |
|
$coordinate = Validations::validateCellOrCellRange($coordinate); |
|
2846
|
|
|
|
|
2847
|
10291 |
|
if (Coordinate::coordinateIsRange($coordinate)) { |
|
2848
|
499 |
|
[$first] = Coordinate::splitRange($coordinate); |
|
2849
|
499 |
|
$this->activeCell = $first[0]; |
|
2850
|
|
|
} else { |
|
2851
|
10260 |
|
$this->activeCell = $coordinate; |
|
2852
|
|
|
} |
|
2853
|
10291 |
|
$this->selectedCells = $coordinate; |
|
2854
|
10291 |
|
$this->setSelectedCellsActivePane(); |
|
2855
|
|
|
|
|
2856
|
10291 |
|
return $this; |
|
2857
|
|
|
} |
|
2858
|
|
|
|
|
2859
|
10292 |
|
private function setSelectedCellsActivePane(): void |
|
2860
|
|
|
{ |
|
2861
|
10292 |
|
if (!empty($this->freezePane)) { |
|
2862
|
47 |
|
$coordinateC = Coordinate::indexesFromString($this->freezePane); |
|
2863
|
47 |
|
$coordinateT = Coordinate::indexesFromString($this->activeCell); |
|
2864
|
47 |
|
if ($coordinateC[0] === 1) { |
|
2865
|
26 |
|
$activePane = ($coordinateT[1] <= $coordinateC[1]) ? 'topLeft' : 'bottomLeft'; |
|
2866
|
23 |
|
} elseif ($coordinateC[1] === 1) { |
|
2867
|
3 |
|
$activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'topLeft' : 'topRight'; |
|
2868
|
21 |
|
} elseif ($coordinateT[1] <= $coordinateC[1]) { |
|
2869
|
21 |
|
$activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'topLeft' : 'topRight'; |
|
2870
|
|
|
} else { |
|
2871
|
10 |
|
$activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'bottomLeft' : 'bottomRight'; |
|
2872
|
|
|
} |
|
2873
|
47 |
|
$this->setActivePane($activePane); |
|
2874
|
47 |
|
$this->panes[$activePane] = new Pane($activePane, $this->selectedCells, $this->activeCell); |
|
2875
|
|
|
} |
|
2876
|
|
|
} |
|
2877
|
|
|
|
|
2878
|
|
|
/** |
|
2879
|
|
|
* Get right-to-left. |
|
2880
|
|
|
*/ |
|
2881
|
1045 |
|
public function getRightToLeft(): bool |
|
2882
|
|
|
{ |
|
2883
|
1045 |
|
return $this->rightToLeft; |
|
2884
|
|
|
} |
|
2885
|
|
|
|
|
2886
|
|
|
/** |
|
2887
|
|
|
* Set right-to-left. |
|
2888
|
|
|
* |
|
2889
|
|
|
* @param bool $value Right-to-left true/false |
|
2890
|
|
|
* |
|
2891
|
|
|
* @return $this |
|
2892
|
|
|
*/ |
|
2893
|
144 |
|
public function setRightToLeft(bool $value): static |
|
2894
|
|
|
{ |
|
2895
|
144 |
|
$this->rightToLeft = $value; |
|
2896
|
|
|
|
|
2897
|
144 |
|
return $this; |
|
2898
|
|
|
} |
|
2899
|
|
|
|
|
2900
|
|
|
/** |
|
2901
|
|
|
* Fill worksheet from values in array. |
|
2902
|
|
|
* |
|
2903
|
|
|
* @param mixed[]|mixed[][] $source Source array |
|
2904
|
|
|
* @param mixed $nullValue Value in source array that stands for blank cell |
|
2905
|
|
|
* @param string $startCell Insert array starting from this cell address as the top left coordinate |
|
2906
|
|
|
* @param bool $strictNullComparison Apply strict comparison when testing for null values in the array |
|
2907
|
|
|
* |
|
2908
|
|
|
* @return $this |
|
2909
|
|
|
*/ |
|
2910
|
798 |
|
public function fromArray(array $source, mixed $nullValue = null, string $startCell = 'A1', bool $strictNullComparison = false): static |
|
2911
|
|
|
{ |
|
2912
|
|
|
// Convert a 1-D array to 2-D (for ease of looping) |
|
2913
|
798 |
|
if (!is_array(end($source))) { |
|
2914
|
47 |
|
$source = [$source]; |
|
2915
|
|
|
} |
|
2916
|
|
|
/** @var mixed[][] $source */ |
|
2917
|
|
|
|
|
2918
|
|
|
// start coordinate |
|
2919
|
798 |
|
[$startColumn, $startRow] = Coordinate::coordinateFromString($startCell); |
|
2920
|
798 |
|
$startRow = (int) $startRow; |
|
2921
|
|
|
|
|
2922
|
|
|
// Loop through $source |
|
2923
|
798 |
|
if ($strictNullComparison) { |
|
2924
|
393 |
|
foreach ($source as $rowData) { |
|
2925
|
|
|
/** @var string */ |
|
2926
|
393 |
|
$currentColumn = $startColumn; |
|
2927
|
393 |
|
foreach ($rowData as $cellValue) { |
|
2928
|
393 |
|
if ($cellValue !== $nullValue) { |
|
2929
|
|
|
/** @var string $currentColumn */ |
|
2930
|
393 |
|
$this->getCell($currentColumn . $startRow)->setValue($cellValue); |
|
2931
|
|
|
} |
|
2932
|
|
|
/** @var string $currentColumn */ |
|
2933
|
393 |
|
++$currentColumn; |
|
2934
|
|
|
} |
|
2935
|
393 |
|
++$startRow; |
|
2936
|
|
|
} |
|
2937
|
|
|
} else { |
|
2938
|
406 |
|
foreach ($source as $rowData) { |
|
2939
|
406 |
|
$currentColumn = $startColumn; |
|
2940
|
406 |
|
foreach ($rowData as $cellValue) { |
|
2941
|
405 |
|
if ($cellValue != $nullValue) { |
|
2942
|
|
|
/** @var string $currentColumn */ |
|
2943
|
399 |
|
$this->getCell($currentColumn . $startRow)->setValue($cellValue); |
|
2944
|
|
|
} |
|
2945
|
|
|
/** @var string $currentColumn */ |
|
2946
|
405 |
|
++$currentColumn; |
|
2947
|
|
|
} |
|
2948
|
406 |
|
++$startRow; |
|
2949
|
|
|
} |
|
2950
|
|
|
} |
|
2951
|
|
|
|
|
2952
|
798 |
|
return $this; |
|
2953
|
|
|
} |
|
2954
|
|
|
|
|
2955
|
|
|
/** |
|
2956
|
|
|
* @param null|bool|float|int|RichText|string $nullValue value to use when null |
|
2957
|
|
|
* |
|
2958
|
|
|
* @throws Exception |
|
2959
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Calculation\Exception |
|
2960
|
|
|
*/ |
|
2961
|
179 |
|
protected function cellToArray(Cell $cell, bool $calculateFormulas, bool $formatData, mixed $nullValue): mixed |
|
2962
|
|
|
{ |
|
2963
|
179 |
|
$returnValue = $nullValue; |
|
2964
|
|
|
|
|
2965
|
179 |
|
if ($cell->getValue() !== null) { |
|
2966
|
179 |
|
if ($cell->getValue() instanceof RichText) { |
|
2967
|
4 |
|
$returnValue = $cell->getValue()->getPlainText(); |
|
2968
|
|
|
} else { |
|
2969
|
179 |
|
$returnValue = ($calculateFormulas) ? $cell->getCalculatedValue() : $cell->getValue(); |
|
2970
|
|
|
} |
|
2971
|
|
|
|
|
2972
|
179 |
|
if ($formatData) { |
|
2973
|
117 |
|
$style = $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()); |
|
2974
|
|
|
/** @var null|bool|float|int|RichText|string */ |
|
2975
|
117 |
|
$returnValuex = $returnValue; |
|
2976
|
117 |
|
$returnValue = NumberFormat::toFormattedString( |
|
2977
|
117 |
|
$returnValuex, |
|
2978
|
117 |
|
$style->getNumberFormat()->getFormatCode() ?? NumberFormat::FORMAT_GENERAL |
|
2979
|
117 |
|
); |
|
2980
|
|
|
} |
|
2981
|
|
|
} |
|
2982
|
|
|
|
|
2983
|
179 |
|
return $returnValue; |
|
2984
|
|
|
} |
|
2985
|
|
|
|
|
2986
|
|
|
/** |
|
2987
|
|
|
* Create array from a range of cells. |
|
2988
|
|
|
* |
|
2989
|
|
|
* @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
|
2990
|
|
|
* @param bool $calculateFormulas Should formulas be calculated? |
|
2991
|
|
|
* @param bool $formatData Should formatting be applied to cell values? |
|
2992
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
2993
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
2994
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
2995
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
2996
|
|
|
* |
|
2997
|
|
|
* @return mixed[][] |
|
2998
|
|
|
*/ |
|
2999
|
149 |
|
public function rangeToArray( |
|
3000
|
|
|
string $range, |
|
3001
|
|
|
mixed $nullValue = null, |
|
3002
|
|
|
bool $calculateFormulas = true, |
|
3003
|
|
|
bool $formatData = true, |
|
3004
|
|
|
bool $returnCellRef = false, |
|
3005
|
|
|
bool $ignoreHidden = false, |
|
3006
|
|
|
bool $reduceArrays = false |
|
3007
|
|
|
): array { |
|
3008
|
149 |
|
$returnValue = []; |
|
3009
|
|
|
|
|
3010
|
|
|
// Loop through rows |
|
3011
|
149 |
|
foreach ($this->rangeToArrayYieldRows($range, $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays) as $rowRef => $rowArray) { |
|
3012
|
149 |
|
$returnValue[$rowRef] = $rowArray; |
|
3013
|
|
|
} |
|
3014
|
|
|
|
|
3015
|
|
|
// Return |
|
3016
|
149 |
|
return $returnValue; |
|
3017
|
|
|
} |
|
3018
|
|
|
|
|
3019
|
|
|
/** |
|
3020
|
|
|
* Create array from a multiple ranges of cells. (such as A1:A3,A15,B17:C17). |
|
3021
|
|
|
* |
|
3022
|
|
|
* @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
|
3023
|
|
|
* @param bool $calculateFormulas Should formulas be calculated? |
|
3024
|
|
|
* @param bool $formatData Should formatting be applied to cell values? |
|
3025
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
3026
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
3027
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3028
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
3029
|
|
|
* |
|
3030
|
|
|
* @return mixed[][] |
|
3031
|
|
|
*/ |
|
3032
|
3 |
|
public function rangesToArray( |
|
3033
|
|
|
string $ranges, |
|
3034
|
|
|
mixed $nullValue = null, |
|
3035
|
|
|
bool $calculateFormulas = true, |
|
3036
|
|
|
bool $formatData = true, |
|
3037
|
|
|
bool $returnCellRef = false, |
|
3038
|
|
|
bool $ignoreHidden = false, |
|
3039
|
|
|
bool $reduceArrays = false |
|
3040
|
|
|
): array { |
|
3041
|
3 |
|
$returnValue = []; |
|
3042
|
|
|
|
|
3043
|
3 |
|
$parts = explode(',', $ranges); |
|
3044
|
3 |
|
foreach ($parts as $part) { |
|
3045
|
|
|
// Loop through rows |
|
3046
|
3 |
|
foreach ($this->rangeToArrayYieldRows($part, $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays) as $rowRef => $rowArray) { |
|
3047
|
3 |
|
$returnValue[$rowRef] = $rowArray; |
|
3048
|
|
|
} |
|
3049
|
|
|
} |
|
3050
|
|
|
|
|
3051
|
|
|
// Return |
|
3052
|
3 |
|
return $returnValue; |
|
3053
|
|
|
} |
|
3054
|
|
|
|
|
3055
|
|
|
/** |
|
3056
|
|
|
* Create array from a range of cells, yielding each row in turn. |
|
3057
|
|
|
* |
|
3058
|
|
|
* @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
|
3059
|
|
|
* @param bool $calculateFormulas Should formulas be calculated? |
|
3060
|
|
|
* @param bool $formatData Should formatting be applied to cell values? |
|
3061
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
3062
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
3063
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3064
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
3065
|
|
|
* |
|
3066
|
|
|
* @return Generator<array<mixed>> |
|
3067
|
|
|
*/ |
|
3068
|
179 |
|
public function rangeToArrayYieldRows( |
|
3069
|
|
|
string $range, |
|
3070
|
|
|
mixed $nullValue = null, |
|
3071
|
|
|
bool $calculateFormulas = true, |
|
3072
|
|
|
bool $formatData = true, |
|
3073
|
|
|
bool $returnCellRef = false, |
|
3074
|
|
|
bool $ignoreHidden = false, |
|
3075
|
|
|
bool $reduceArrays = false |
|
3076
|
|
|
) { |
|
3077
|
179 |
|
$range = Validations::validateCellOrCellRange($range); |
|
3078
|
|
|
|
|
3079
|
|
|
// Identify the range that we need to extract from the worksheet |
|
3080
|
179 |
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range); |
|
3081
|
179 |
|
$minCol = Coordinate::stringFromColumnIndex($rangeStart[0]); |
|
3082
|
179 |
|
$minRow = $rangeStart[1]; |
|
3083
|
179 |
|
$maxCol = Coordinate::stringFromColumnIndex($rangeEnd[0]); |
|
3084
|
179 |
|
$maxRow = $rangeEnd[1]; |
|
3085
|
179 |
|
$minColInt = $rangeStart[0]; |
|
3086
|
179 |
|
$maxColInt = $rangeEnd[0]; |
|
3087
|
|
|
|
|
3088
|
179 |
|
++$maxCol; |
|
3089
|
|
|
/** @var array<string, bool> */ |
|
3090
|
179 |
|
$hiddenColumns = []; |
|
3091
|
179 |
|
$nullRow = $this->buildNullRow($nullValue, $minCol, $maxCol, $returnCellRef, $ignoreHidden, $hiddenColumns); |
|
3092
|
179 |
|
$hideColumns = !empty($hiddenColumns); |
|
3093
|
|
|
|
|
3094
|
179 |
|
$keys = $this->cellCollection->getSortedCoordinatesInt(); |
|
3095
|
179 |
|
$keyIndex = 0; |
|
3096
|
179 |
|
$keysCount = count($keys); |
|
3097
|
|
|
// Loop through rows |
|
3098
|
179 |
|
for ($row = $minRow; $row <= $maxRow; ++$row) { |
|
3099
|
179 |
|
if (($ignoreHidden === true) && ($this->isRowVisible($row) === false)) { |
|
3100
|
4 |
|
continue; |
|
3101
|
|
|
} |
|
3102
|
179 |
|
$rowRef = $returnCellRef ? $row : ($row - $minRow); |
|
3103
|
179 |
|
$returnValue = $nullRow; |
|
3104
|
|
|
|
|
3105
|
179 |
|
$index = ($row - 1) * AddressRange::MAX_COLUMN_INT + 1; |
|
3106
|
179 |
|
$indexPlus = $index + AddressRange::MAX_COLUMN_INT - 1; |
|
3107
|
179 |
|
while ($keyIndex < $keysCount && $keys[$keyIndex] < $index) { |
|
3108
|
54 |
|
++$keyIndex; |
|
3109
|
|
|
} |
|
3110
|
179 |
|
while ($keyIndex < $keysCount && $keys[$keyIndex] <= $indexPlus) { |
|
3111
|
179 |
|
$key = $keys[$keyIndex]; |
|
3112
|
179 |
|
$thisRow = intdiv($key - 1, AddressRange::MAX_COLUMN_INT) + 1; |
|
3113
|
179 |
|
$thisCol = ($key % AddressRange::MAX_COLUMN_INT) ?: AddressRange::MAX_COLUMN_INT; |
|
3114
|
179 |
|
if ($thisCol >= $minColInt && $thisCol <= $maxColInt) { |
|
3115
|
179 |
|
$col = Coordinate::stringFromColumnIndex($thisCol); |
|
3116
|
179 |
|
if ($hideColumns === false || !isset($hiddenColumns[$col])) { |
|
3117
|
179 |
|
$columnRef = $returnCellRef ? $col : ($thisCol - $minColInt); |
|
3118
|
179 |
|
$cell = $this->cellCollection->get("{$col}{$thisRow}"); |
|
3119
|
179 |
|
if ($cell !== null) { |
|
3120
|
179 |
|
$value = $this->cellToArray($cell, $calculateFormulas, $formatData, $nullValue); |
|
3121
|
179 |
|
if ($reduceArrays) { |
|
3122
|
21 |
|
while (is_array($value)) { |
|
3123
|
19 |
|
$value = array_shift($value); |
|
3124
|
|
|
} |
|
3125
|
|
|
} |
|
3126
|
179 |
|
if ($value !== $nullValue) { |
|
3127
|
179 |
|
$returnValue[$columnRef] = $value; |
|
3128
|
|
|
} |
|
3129
|
|
|
} |
|
3130
|
|
|
} |
|
3131
|
|
|
} |
|
3132
|
179 |
|
++$keyIndex; |
|
3133
|
|
|
} |
|
3134
|
|
|
|
|
3135
|
179 |
|
yield $rowRef => $returnValue; |
|
3136
|
|
|
} |
|
3137
|
|
|
} |
|
3138
|
|
|
|
|
3139
|
|
|
/** |
|
3140
|
|
|
* Prepare a row data filled with null values to deduplicate the memory areas for empty rows. |
|
3141
|
|
|
* |
|
3142
|
|
|
* @param mixed $nullValue Value returned in the array entry if a cell doesn't exist |
|
3143
|
|
|
* @param string $minCol Start column of the range |
|
3144
|
|
|
* @param string $maxCol End column of the range |
|
3145
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
3146
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
3147
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3148
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
3149
|
|
|
* @param array<string, bool> $hiddenColumns |
|
3150
|
|
|
* |
|
3151
|
|
|
* @return mixed[] |
|
3152
|
|
|
*/ |
|
3153
|
179 |
|
private function buildNullRow( |
|
3154
|
|
|
mixed $nullValue, |
|
3155
|
|
|
string $minCol, |
|
3156
|
|
|
string $maxCol, |
|
3157
|
|
|
bool $returnCellRef, |
|
3158
|
|
|
bool $ignoreHidden, |
|
3159
|
|
|
array &$hiddenColumns |
|
3160
|
|
|
): array { |
|
3161
|
179 |
|
$nullRow = []; |
|
3162
|
179 |
|
$c = -1; |
|
3163
|
179 |
|
for ($col = $minCol; $col !== $maxCol; ++$col) { |
|
3164
|
|
|
/** @var string $col */ |
|
3165
|
179 |
|
if ($ignoreHidden === true && $this->columnDimensionExists($col) && $this->getColumnDimension($col)->getVisible() === false) { |
|
3166
|
2 |
|
$hiddenColumns[$col] = true; |
|
3167
|
|
|
} else { |
|
3168
|
179 |
|
$columnRef = $returnCellRef ? $col : ++$c; |
|
3169
|
179 |
|
$nullRow[$columnRef] = $nullValue; |
|
3170
|
|
|
} |
|
3171
|
|
|
} |
|
3172
|
|
|
|
|
3173
|
179 |
|
return $nullRow; |
|
3174
|
|
|
} |
|
3175
|
|
|
|
|
3176
|
19 |
|
private function validateNamedRange(string $definedName, bool $returnNullIfInvalid = false): ?DefinedName |
|
3177
|
|
|
{ |
|
3178
|
19 |
|
$namedRange = DefinedName::resolveName($definedName, $this); |
|
3179
|
19 |
|
if ($namedRange === null) { |
|
3180
|
6 |
|
if ($returnNullIfInvalid) { |
|
3181
|
5 |
|
return null; |
|
3182
|
|
|
} |
|
3183
|
|
|
|
|
3184
|
1 |
|
throw new Exception('Named Range ' . $definedName . ' does not exist.'); |
|
3185
|
|
|
} |
|
3186
|
|
|
|
|
3187
|
13 |
|
if ($namedRange->isFormula()) { |
|
3188
|
|
|
if ($returnNullIfInvalid) { |
|
3189
|
|
|
return null; |
|
3190
|
|
|
} |
|
3191
|
|
|
|
|
3192
|
|
|
throw new Exception('Defined Named ' . $definedName . ' is a formula, not a range or cell.'); |
|
3193
|
|
|
} |
|
3194
|
|
|
|
|
3195
|
13 |
|
if ($namedRange->getLocalOnly()) { |
|
3196
|
2 |
|
$worksheet = $namedRange->getWorksheet(); |
|
3197
|
2 |
|
if ($worksheet === null || $this->hash !== $worksheet->getHashInt()) { |
|
3198
|
|
|
if ($returnNullIfInvalid) { |
|
3199
|
|
|
return null; |
|
3200
|
|
|
} |
|
3201
|
|
|
|
|
3202
|
|
|
throw new Exception( |
|
3203
|
|
|
'Named range ' . $definedName . ' is not accessible from within sheet ' . $this->getTitle() |
|
3204
|
|
|
); |
|
3205
|
|
|
} |
|
3206
|
|
|
} |
|
3207
|
|
|
|
|
3208
|
13 |
|
return $namedRange; |
|
3209
|
|
|
} |
|
3210
|
|
|
|
|
3211
|
|
|
/** |
|
3212
|
|
|
* Create array from a range of cells. |
|
3213
|
|
|
* |
|
3214
|
|
|
* @param string $definedName The Named Range that should be returned |
|
3215
|
|
|
* @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
|
3216
|
|
|
* @param bool $calculateFormulas Should formulas be calculated? |
|
3217
|
|
|
* @param bool $formatData Should formatting be applied to cell values? |
|
3218
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
3219
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
3220
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3221
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
3222
|
|
|
* |
|
3223
|
|
|
* @return mixed[][] |
|
3224
|
|
|
*/ |
|
3225
|
2 |
|
public function namedRangeToArray( |
|
3226
|
|
|
string $definedName, |
|
3227
|
|
|
mixed $nullValue = null, |
|
3228
|
|
|
bool $calculateFormulas = true, |
|
3229
|
|
|
bool $formatData = true, |
|
3230
|
|
|
bool $returnCellRef = false, |
|
3231
|
|
|
bool $ignoreHidden = false, |
|
3232
|
|
|
bool $reduceArrays = false |
|
3233
|
|
|
): array { |
|
3234
|
2 |
|
$retVal = []; |
|
3235
|
2 |
|
$namedRange = $this->validateNamedRange($definedName); |
|
3236
|
1 |
|
if ($namedRange !== null) { |
|
3237
|
1 |
|
$cellRange = ltrim(substr($namedRange->getValue(), (int) strrpos($namedRange->getValue(), '!')), '!'); |
|
3238
|
1 |
|
$cellRange = str_replace('$', '', $cellRange); |
|
3239
|
1 |
|
$workSheet = $namedRange->getWorksheet(); |
|
3240
|
1 |
|
if ($workSheet !== null) { |
|
3241
|
1 |
|
$retVal = $workSheet->rangeToArray($cellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays); |
|
3242
|
|
|
} |
|
3243
|
|
|
} |
|
3244
|
|
|
|
|
3245
|
1 |
|
return $retVal; |
|
3246
|
|
|
} |
|
3247
|
|
|
|
|
3248
|
|
|
/** |
|
3249
|
|
|
* Create array from worksheet. |
|
3250
|
|
|
* |
|
3251
|
|
|
* @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
|
3252
|
|
|
* @param bool $calculateFormulas Should formulas be calculated? |
|
3253
|
|
|
* @param bool $formatData Should formatting be applied to cell values? |
|
3254
|
|
|
* @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
|
3255
|
|
|
* True - Return rows and columns indexed by their actual row and column IDs |
|
3256
|
|
|
* @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3257
|
|
|
* True - Don't return values for rows/columns that are defined as hidden. |
|
3258
|
|
|
* |
|
3259
|
|
|
* @return mixed[][] |
|
3260
|
|
|
*/ |
|
3261
|
80 |
|
public function toArray( |
|
3262
|
|
|
mixed $nullValue = null, |
|
3263
|
|
|
bool $calculateFormulas = true, |
|
3264
|
|
|
bool $formatData = true, |
|
3265
|
|
|
bool $returnCellRef = false, |
|
3266
|
|
|
bool $ignoreHidden = false, |
|
3267
|
|
|
bool $reduceArrays = false |
|
3268
|
|
|
): array { |
|
3269
|
|
|
// Garbage collect... |
|
3270
|
80 |
|
$this->garbageCollect(); |
|
3271
|
80 |
|
$this->calculateArrays($calculateFormulas); |
|
3272
|
|
|
|
|
3273
|
|
|
// Identify the range that we need to extract from the worksheet |
|
3274
|
80 |
|
$maxCol = $this->getHighestColumn(); |
|
3275
|
80 |
|
$maxRow = $this->getHighestRow(); |
|
3276
|
|
|
|
|
3277
|
|
|
// Return |
|
3278
|
80 |
|
return $this->rangeToArray("A1:{$maxCol}{$maxRow}", $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays); |
|
3279
|
|
|
} |
|
3280
|
|
|
|
|
3281
|
|
|
/** |
|
3282
|
|
|
* Get row iterator. |
|
3283
|
|
|
* |
|
3284
|
|
|
* @param int $startRow The row number at which to start iterating |
|
3285
|
|
|
* @param ?int $endRow The row number at which to stop iterating |
|
3286
|
|
|
*/ |
|
3287
|
94 |
|
public function getRowIterator(int $startRow = 1, ?int $endRow = null): RowIterator |
|
3288
|
|
|
{ |
|
3289
|
94 |
|
return new RowIterator($this, $startRow, $endRow); |
|
3290
|
|
|
} |
|
3291
|
|
|
|
|
3292
|
|
|
/** |
|
3293
|
|
|
* Get column iterator. |
|
3294
|
|
|
* |
|
3295
|
|
|
* @param string $startColumn The column address at which to start iterating |
|
3296
|
|
|
* @param ?string $endColumn The column address at which to stop iterating |
|
3297
|
|
|
*/ |
|
3298
|
26 |
|
public function getColumnIterator(string $startColumn = 'A', ?string $endColumn = null): ColumnIterator |
|
3299
|
|
|
{ |
|
3300
|
26 |
|
return new ColumnIterator($this, $startColumn, $endColumn); |
|
3301
|
|
|
} |
|
3302
|
|
|
|
|
3303
|
|
|
/** |
|
3304
|
|
|
* Run PhpSpreadsheet garbage collector. |
|
3305
|
|
|
* |
|
3306
|
|
|
* @return $this |
|
3307
|
|
|
*/ |
|
3308
|
1166 |
|
public function garbageCollect(): static |
|
3309
|
|
|
{ |
|
3310
|
|
|
// Flush cache |
|
3311
|
1166 |
|
$this->cellCollection->get('A1'); |
|
3312
|
|
|
|
|
3313
|
|
|
// Lookup highest column and highest row if cells are cleaned |
|
3314
|
1166 |
|
$colRow = $this->cellCollection->getHighestRowAndColumn(); |
|
3315
|
1166 |
|
$highestRow = $colRow['row']; |
|
3316
|
1166 |
|
$highestColumn = Coordinate::columnIndexFromString($colRow['column']); |
|
3317
|
|
|
|
|
3318
|
|
|
// Loop through column dimensions |
|
3319
|
1166 |
|
foreach ($this->columnDimensions as $dimension) { |
|
3320
|
166 |
|
$highestColumn = max($highestColumn, Coordinate::columnIndexFromString($dimension->getColumnIndex())); |
|
3321
|
|
|
} |
|
3322
|
|
|
|
|
3323
|
|
|
// Loop through row dimensions |
|
3324
|
1166 |
|
foreach ($this->rowDimensions as $dimension) { |
|
3325
|
103 |
|
$highestRow = max($highestRow, $dimension->getRowIndex()); |
|
3326
|
|
|
} |
|
3327
|
|
|
|
|
3328
|
|
|
// Cache values |
|
3329
|
1166 |
|
if ($highestColumn < 1) { |
|
3330
|
|
|
$this->cachedHighestColumn = 1; |
|
3331
|
|
|
} else { |
|
3332
|
1166 |
|
$this->cachedHighestColumn = $highestColumn; |
|
3333
|
|
|
} |
|
3334
|
|
|
/** @var int $highestRow */ |
|
3335
|
1166 |
|
$this->cachedHighestRow = $highestRow; |
|
3336
|
|
|
|
|
3337
|
|
|
// Return |
|
3338
|
1166 |
|
return $this; |
|
3339
|
|
|
} |
|
3340
|
|
|
|
|
3341
|
10619 |
|
public function getHashInt(): int |
|
3342
|
|
|
{ |
|
3343
|
10619 |
|
return $this->hash; |
|
3344
|
|
|
} |
|
3345
|
|
|
|
|
3346
|
|
|
/** |
|
3347
|
|
|
* Extract worksheet title from range. |
|
3348
|
|
|
* |
|
3349
|
|
|
* Example: extractSheetTitle("testSheet!A1") ==> 'A1' |
|
3350
|
|
|
* Example: extractSheetTitle("testSheet!A1:C3") ==> 'A1:C3' |
|
3351
|
|
|
* Example: extractSheetTitle("'testSheet 1'!A1", true) ==> ['testSheet 1', 'A1']; |
|
3352
|
|
|
* Example: extractSheetTitle("'testSheet 1'!A1:C3", true) ==> ['testSheet 1', 'A1:C3']; |
|
3353
|
|
|
* Example: extractSheetTitle("A1", true) ==> ['', 'A1']; |
|
3354
|
|
|
* Example: extractSheetTitle("A1:C3", true) ==> ['', 'A1:C3'] |
|
3355
|
|
|
* |
|
3356
|
|
|
* @param ?string $range Range to extract title from |
|
3357
|
|
|
* @param bool $returnRange Return range? (see example) |
|
3358
|
|
|
* |
|
3359
|
|
|
* @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null)) |
|
3360
|
|
|
*/ |
|
3361
|
10496 |
|
public static function extractSheetTitle(?string $range, bool $returnRange = false, bool $unapostrophize = false): array|null|string |
|
3362
|
|
|
{ |
|
3363
|
10496 |
|
if (empty($range)) { |
|
3364
|
13 |
|
return $returnRange ? [null, null] : null; |
|
3365
|
|
|
} |
|
3366
|
|
|
|
|
3367
|
|
|
// Sheet title included? |
|
3368
|
10494 |
|
if (($sep = strrpos($range, '!')) === false) { |
|
3369
|
10468 |
|
return $returnRange ? ['', $range] : ''; |
|
3370
|
|
|
} |
|
3371
|
|
|
|
|
3372
|
1397 |
|
if ($returnRange) { |
|
3373
|
1397 |
|
$title = substr($range, 0, $sep); |
|
3374
|
1397 |
|
if ($unapostrophize) { |
|
3375
|
1336 |
|
$title = self::unApostrophizeTitle($title); |
|
3376
|
|
|
} |
|
3377
|
|
|
|
|
3378
|
1397 |
|
return [$title, substr($range, $sep + 1)]; |
|
3379
|
|
|
} |
|
3380
|
|
|
|
|
3381
|
7 |
|
return substr($range, $sep + 1); |
|
3382
|
|
|
} |
|
3383
|
|
|
|
|
3384
|
1350 |
|
public static function unApostrophizeTitle(?string $title): string |
|
3385
|
|
|
{ |
|
3386
|
1350 |
|
$title ??= ''; |
|
3387
|
1350 |
|
if ($title[0] === "'" && substr($title, -1) === "'") { |
|
3388
|
1278 |
|
$title = str_replace("''", "'", substr($title, 1, -1)); |
|
3389
|
|
|
} |
|
3390
|
|
|
|
|
3391
|
1350 |
|
return $title; |
|
3392
|
|
|
} |
|
3393
|
|
|
|
|
3394
|
|
|
/** |
|
3395
|
|
|
* Get hyperlink. |
|
3396
|
|
|
* |
|
3397
|
|
|
* @param string $cellCoordinate Cell coordinate to get hyperlink for, eg: 'A1' |
|
3398
|
|
|
*/ |
|
3399
|
97 |
|
public function getHyperlink(string $cellCoordinate): Hyperlink |
|
3400
|
|
|
{ |
|
3401
|
|
|
// return hyperlink if we already have one |
|
3402
|
97 |
|
if (isset($this->hyperlinkCollection[$cellCoordinate])) { |
|
3403
|
45 |
|
return $this->hyperlinkCollection[$cellCoordinate]; |
|
3404
|
|
|
} |
|
3405
|
|
|
|
|
3406
|
|
|
// else create hyperlink |
|
3407
|
97 |
|
$this->hyperlinkCollection[$cellCoordinate] = new Hyperlink(); |
|
3408
|
|
|
|
|
3409
|
97 |
|
return $this->hyperlinkCollection[$cellCoordinate]; |
|
3410
|
|
|
} |
|
3411
|
|
|
|
|
3412
|
|
|
/** |
|
3413
|
|
|
* Set hyperlink. |
|
3414
|
|
|
* |
|
3415
|
|
|
* @param string $cellCoordinate Cell coordinate to insert hyperlink, eg: 'A1' |
|
3416
|
|
|
* |
|
3417
|
|
|
* @return $this |
|
3418
|
|
|
*/ |
|
3419
|
47 |
|
public function setHyperlink(string $cellCoordinate, ?Hyperlink $hyperlink = null): static |
|
3420
|
|
|
{ |
|
3421
|
47 |
|
if ($hyperlink === null) { |
|
3422
|
46 |
|
unset($this->hyperlinkCollection[$cellCoordinate]); |
|
3423
|
|
|
} else { |
|
3424
|
21 |
|
$this->hyperlinkCollection[$cellCoordinate] = $hyperlink; |
|
3425
|
|
|
} |
|
3426
|
|
|
|
|
3427
|
47 |
|
return $this; |
|
3428
|
|
|
} |
|
3429
|
|
|
|
|
3430
|
|
|
/** |
|
3431
|
|
|
* Hyperlink at a specific coordinate exists? |
|
3432
|
|
|
* |
|
3433
|
|
|
* @param string $coordinate eg: 'A1' |
|
3434
|
|
|
*/ |
|
3435
|
549 |
|
public function hyperlinkExists(string $coordinate): bool |
|
3436
|
|
|
{ |
|
3437
|
549 |
|
return isset($this->hyperlinkCollection[$coordinate]); |
|
3438
|
|
|
} |
|
3439
|
|
|
|
|
3440
|
|
|
/** |
|
3441
|
|
|
* Get collection of hyperlinks. |
|
3442
|
|
|
* |
|
3443
|
|
|
* @return Hyperlink[] |
|
3444
|
|
|
*/ |
|
3445
|
588 |
|
public function getHyperlinkCollection(): array |
|
3446
|
|
|
{ |
|
3447
|
588 |
|
return $this->hyperlinkCollection; |
|
3448
|
|
|
} |
|
3449
|
|
|
|
|
3450
|
|
|
/** |
|
3451
|
|
|
* Get data validation. |
|
3452
|
|
|
* |
|
3453
|
|
|
* @param string $cellCoordinate Cell coordinate to get data validation for, eg: 'A1' |
|
3454
|
|
|
*/ |
|
3455
|
35 |
|
public function getDataValidation(string $cellCoordinate): DataValidation |
|
3456
|
|
|
{ |
|
3457
|
|
|
// return data validation if we already have one |
|
3458
|
35 |
|
if (isset($this->dataValidationCollection[$cellCoordinate])) { |
|
3459
|
26 |
|
return $this->dataValidationCollection[$cellCoordinate]; |
|
3460
|
|
|
} |
|
3461
|
|
|
|
|
3462
|
|
|
// or if cell is part of a data validation range |
|
3463
|
28 |
|
foreach ($this->dataValidationCollection as $key => $dataValidation) { |
|
3464
|
12 |
|
$keyParts = explode(' ', $key); |
|
3465
|
12 |
|
foreach ($keyParts as $keyPart) { |
|
3466
|
12 |
|
if ($keyPart === $cellCoordinate) { |
|
3467
|
1 |
|
return $dataValidation; |
|
3468
|
|
|
} |
|
3469
|
12 |
|
if (str_contains($keyPart, ':')) { |
|
3470
|
9 |
|
if (Coordinate::coordinateIsInsideRange($keyPart, $cellCoordinate)) { |
|
3471
|
9 |
|
return $dataValidation; |
|
3472
|
|
|
} |
|
3473
|
|
|
} |
|
3474
|
|
|
} |
|
3475
|
|
|
} |
|
3476
|
|
|
|
|
3477
|
|
|
// else create data validation |
|
3478
|
20 |
|
$dataValidation = new DataValidation(); |
|
3479
|
20 |
|
$dataValidation->setSqref($cellCoordinate); |
|
3480
|
20 |
|
$this->dataValidationCollection[$cellCoordinate] = $dataValidation; |
|
3481
|
|
|
|
|
3482
|
20 |
|
return $dataValidation; |
|
3483
|
|
|
} |
|
3484
|
|
|
|
|
3485
|
|
|
/** |
|
3486
|
|
|
* Set data validation. |
|
3487
|
|
|
* |
|
3488
|
|
|
* @param string $cellCoordinate Cell coordinate to insert data validation, eg: 'A1' |
|
3489
|
|
|
* |
|
3490
|
|
|
* @return $this |
|
3491
|
|
|
*/ |
|
3492
|
82 |
|
public function setDataValidation(string $cellCoordinate, ?DataValidation $dataValidation = null): static |
|
3493
|
|
|
{ |
|
3494
|
82 |
|
if ($dataValidation === null) { |
|
3495
|
51 |
|
unset($this->dataValidationCollection[$cellCoordinate]); |
|
3496
|
|
|
} else { |
|
3497
|
38 |
|
$dataValidation->setSqref($cellCoordinate); |
|
3498
|
38 |
|
$this->dataValidationCollection[$cellCoordinate] = $dataValidation; |
|
3499
|
|
|
} |
|
3500
|
|
|
|
|
3501
|
82 |
|
return $this; |
|
3502
|
|
|
} |
|
3503
|
|
|
|
|
3504
|
|
|
/** |
|
3505
|
|
|
* Data validation at a specific coordinate exists? |
|
3506
|
|
|
* |
|
3507
|
|
|
* @param string $coordinate eg: 'A1' |
|
3508
|
|
|
*/ |
|
3509
|
25 |
|
public function dataValidationExists(string $coordinate): bool |
|
3510
|
|
|
{ |
|
3511
|
25 |
|
if (isset($this->dataValidationCollection[$coordinate])) { |
|
3512
|
23 |
|
return true; |
|
3513
|
|
|
} |
|
3514
|
8 |
|
foreach ($this->dataValidationCollection as $key => $dataValidation) { |
|
3515
|
7 |
|
$keyParts = explode(' ', $key); |
|
3516
|
7 |
|
foreach ($keyParts as $keyPart) { |
|
3517
|
7 |
|
if ($keyPart === $coordinate) { |
|
3518
|
1 |
|
return true; |
|
3519
|
|
|
} |
|
3520
|
7 |
|
if (str_contains($keyPart, ':')) { |
|
3521
|
2 |
|
if (Coordinate::coordinateIsInsideRange($keyPart, $coordinate)) { |
|
3522
|
2 |
|
return true; |
|
3523
|
|
|
} |
|
3524
|
|
|
} |
|
3525
|
|
|
} |
|
3526
|
|
|
} |
|
3527
|
|
|
|
|
3528
|
6 |
|
return false; |
|
3529
|
|
|
} |
|
3530
|
|
|
|
|
3531
|
|
|
/** |
|
3532
|
|
|
* Get collection of data validations. |
|
3533
|
|
|
* |
|
3534
|
|
|
* @return DataValidation[] |
|
3535
|
|
|
*/ |
|
3536
|
589 |
|
public function getDataValidationCollection(): array |
|
3537
|
|
|
{ |
|
3538
|
589 |
|
$collectionCells = []; |
|
3539
|
589 |
|
$collectionRanges = []; |
|
3540
|
589 |
|
foreach ($this->dataValidationCollection as $key => $dataValidation) { |
|
3541
|
27 |
|
if (Preg::isMatch('/[: ]/', $key)) { |
|
3542
|
15 |
|
$collectionRanges[$key] = $dataValidation; |
|
3543
|
|
|
} else { |
|
3544
|
22 |
|
$collectionCells[$key] = $dataValidation; |
|
3545
|
|
|
} |
|
3546
|
|
|
} |
|
3547
|
|
|
|
|
3548
|
589 |
|
return array_merge($collectionCells, $collectionRanges); |
|
3549
|
|
|
} |
|
3550
|
|
|
|
|
3551
|
|
|
/** |
|
3552
|
|
|
* Accepts a range, returning it as a range that falls within the current highest row and column of the worksheet. |
|
3553
|
|
|
* |
|
3554
|
|
|
* @return string Adjusted range value |
|
3555
|
|
|
*/ |
|
3556
|
|
|
public function shrinkRangeToFit(string $range): string |
|
3557
|
|
|
{ |
|
3558
|
|
|
$maxCol = $this->getHighestColumn(); |
|
3559
|
|
|
$maxRow = $this->getHighestRow(); |
|
3560
|
|
|
$maxCol = Coordinate::columnIndexFromString($maxCol); |
|
3561
|
|
|
|
|
3562
|
|
|
$rangeBlocks = explode(' ', $range); |
|
3563
|
|
|
foreach ($rangeBlocks as &$rangeSet) { |
|
3564
|
|
|
$rangeBoundaries = Coordinate::getRangeBoundaries($rangeSet); |
|
3565
|
|
|
|
|
3566
|
|
|
if (Coordinate::columnIndexFromString($rangeBoundaries[0][0]) > $maxCol) { |
|
3567
|
|
|
$rangeBoundaries[0][0] = Coordinate::stringFromColumnIndex($maxCol); |
|
3568
|
|
|
} |
|
3569
|
|
|
if ($rangeBoundaries[0][1] > $maxRow) { |
|
3570
|
|
|
$rangeBoundaries[0][1] = $maxRow; |
|
3571
|
|
|
} |
|
3572
|
|
|
if (Coordinate::columnIndexFromString($rangeBoundaries[1][0]) > $maxCol) { |
|
3573
|
|
|
$rangeBoundaries[1][0] = Coordinate::stringFromColumnIndex($maxCol); |
|
3574
|
|
|
} |
|
3575
|
|
|
if ($rangeBoundaries[1][1] > $maxRow) { |
|
3576
|
|
|
$rangeBoundaries[1][1] = $maxRow; |
|
3577
|
|
|
} |
|
3578
|
|
|
$rangeSet = $rangeBoundaries[0][0] . $rangeBoundaries[0][1] . ':' . $rangeBoundaries[1][0] . $rangeBoundaries[1][1]; |
|
3579
|
|
|
} |
|
3580
|
|
|
unset($rangeSet); |
|
3581
|
|
|
|
|
3582
|
|
|
return implode(' ', $rangeBlocks); |
|
3583
|
|
|
} |
|
3584
|
|
|
|
|
3585
|
|
|
/** |
|
3586
|
|
|
* Get tab color. |
|
3587
|
|
|
*/ |
|
3588
|
23 |
|
public function getTabColor(): Color |
|
3589
|
|
|
{ |
|
3590
|
23 |
|
if ($this->tabColor === null) { |
|
3591
|
23 |
|
$this->tabColor = new Color(); |
|
3592
|
|
|
} |
|
3593
|
|
|
|
|
3594
|
23 |
|
return $this->tabColor; |
|
3595
|
|
|
} |
|
3596
|
|
|
|
|
3597
|
|
|
/** |
|
3598
|
|
|
* Reset tab color. |
|
3599
|
|
|
* |
|
3600
|
|
|
* @return $this |
|
3601
|
|
|
*/ |
|
3602
|
1 |
|
public function resetTabColor(): static |
|
3603
|
|
|
{ |
|
3604
|
1 |
|
$this->tabColor = null; |
|
3605
|
|
|
|
|
3606
|
1 |
|
return $this; |
|
3607
|
|
|
} |
|
3608
|
|
|
|
|
3609
|
|
|
/** |
|
3610
|
|
|
* Tab color set? |
|
3611
|
|
|
*/ |
|
3612
|
505 |
|
public function isTabColorSet(): bool |
|
3613
|
|
|
{ |
|
3614
|
505 |
|
return $this->tabColor !== null; |
|
3615
|
|
|
} |
|
3616
|
|
|
|
|
3617
|
|
|
/** |
|
3618
|
|
|
* Copy worksheet (!= clone!). |
|
3619
|
|
|
*/ |
|
3620
|
|
|
public function copy(): static |
|
3621
|
|
|
{ |
|
3622
|
|
|
return clone $this; |
|
3623
|
|
|
} |
|
3624
|
|
|
|
|
3625
|
|
|
/** |
|
3626
|
|
|
* Returns a boolean true if the specified row contains no cells. By default, this means that no cell records |
|
3627
|
|
|
* exist in the collection for this row. false will be returned otherwise. |
|
3628
|
|
|
* This rule can be modified by passing a $definitionOfEmptyFlags value: |
|
3629
|
|
|
* 1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value |
|
3630
|
|
|
* cells, then the row will be considered empty. |
|
3631
|
|
|
* 2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty |
|
3632
|
|
|
* string value cells, then the row will be considered empty. |
|
3633
|
|
|
* 3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
|
3634
|
|
|
* If the only cells in the collection are null value or empty string value cells, then the row |
|
3635
|
|
|
* will be considered empty. |
|
3636
|
|
|
* |
|
3637
|
|
|
* @param int $definitionOfEmptyFlags |
|
3638
|
|
|
* Possible Flag Values are: |
|
3639
|
|
|
* CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL |
|
3640
|
|
|
* CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
|
3641
|
|
|
*/ |
|
3642
|
9 |
|
public function isEmptyRow(int $rowId, int $definitionOfEmptyFlags = 0): bool |
|
3643
|
|
|
{ |
|
3644
|
|
|
try { |
|
3645
|
9 |
|
$iterator = new RowIterator($this, $rowId, $rowId); |
|
3646
|
8 |
|
$iterator->seek($rowId); |
|
3647
|
8 |
|
$row = $iterator->current(); |
|
3648
|
1 |
|
} catch (Exception) { |
|
3649
|
1 |
|
return true; |
|
3650
|
|
|
} |
|
3651
|
|
|
|
|
3652
|
8 |
|
return $row->isEmpty($definitionOfEmptyFlags); |
|
3653
|
|
|
} |
|
3654
|
|
|
|
|
3655
|
|
|
/** |
|
3656
|
|
|
* Returns a boolean true if the specified column contains no cells. By default, this means that no cell records |
|
3657
|
|
|
* exist in the collection for this column. false will be returned otherwise. |
|
3658
|
|
|
* This rule can be modified by passing a $definitionOfEmptyFlags value: |
|
3659
|
|
|
* 1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value |
|
3660
|
|
|
* cells, then the column will be considered empty. |
|
3661
|
|
|
* 2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty |
|
3662
|
|
|
* string value cells, then the column will be considered empty. |
|
3663
|
|
|
* 3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
|
3664
|
|
|
* If the only cells in the collection are null value or empty string value cells, then the column |
|
3665
|
|
|
* will be considered empty. |
|
3666
|
|
|
* |
|
3667
|
|
|
* @param int $definitionOfEmptyFlags |
|
3668
|
|
|
* Possible Flag Values are: |
|
3669
|
|
|
* CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL |
|
3670
|
|
|
* CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
|
3671
|
|
|
*/ |
|
3672
|
9 |
|
public function isEmptyColumn(string $columnId, int $definitionOfEmptyFlags = 0): bool |
|
3673
|
|
|
{ |
|
3674
|
|
|
try { |
|
3675
|
9 |
|
$iterator = new ColumnIterator($this, $columnId, $columnId); |
|
3676
|
8 |
|
$iterator->seek($columnId); |
|
3677
|
8 |
|
$column = $iterator->current(); |
|
3678
|
1 |
|
} catch (Exception) { |
|
3679
|
1 |
|
return true; |
|
3680
|
|
|
} |
|
3681
|
|
|
|
|
3682
|
8 |
|
return $column->isEmpty($definitionOfEmptyFlags); |
|
3683
|
|
|
} |
|
3684
|
|
|
|
|
3685
|
|
|
/** |
|
3686
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy. |
|
3687
|
|
|
*/ |
|
3688
|
20 |
|
public function __clone() |
|
3689
|
|
|
{ |
|
3690
|
20 |
|
foreach (get_object_vars($this) as $key => $val) { |
|
3691
|
20 |
|
if ($key == 'parent') { |
|
3692
|
20 |
|
continue; |
|
3693
|
|
|
} |
|
3694
|
|
|
|
|
3695
|
20 |
|
if (is_object($val) || (is_array($val))) { |
|
3696
|
20 |
|
if ($key === 'cellCollection') { |
|
3697
|
20 |
|
$newCollection = $this->cellCollection->cloneCellCollection($this); |
|
3698
|
20 |
|
$this->cellCollection = $newCollection; |
|
3699
|
20 |
|
} elseif ($key === 'drawingCollection') { |
|
3700
|
20 |
|
$currentCollection = $this->drawingCollection; |
|
3701
|
20 |
|
$this->drawingCollection = new ArrayObject(); |
|
3702
|
20 |
|
foreach ($currentCollection as $item) { |
|
3703
|
4 |
|
$newDrawing = clone $item; |
|
3704
|
4 |
|
$newDrawing->setWorksheet($this); |
|
3705
|
|
|
} |
|
3706
|
20 |
|
} elseif ($key === 'tableCollection') { |
|
3707
|
20 |
|
$currentCollection = $this->tableCollection; |
|
3708
|
20 |
|
$this->tableCollection = new ArrayObject(); |
|
3709
|
20 |
|
foreach ($currentCollection as $item) { |
|
3710
|
1 |
|
$newTable = clone $item; |
|
3711
|
1 |
|
$newTable->setName($item->getName() . 'clone'); |
|
3712
|
1 |
|
$this->addTable($newTable); |
|
3713
|
|
|
} |
|
3714
|
20 |
|
} elseif ($key === 'chartCollection') { |
|
3715
|
20 |
|
$currentCollection = $this->chartCollection; |
|
3716
|
20 |
|
$this->chartCollection = new ArrayObject(); |
|
3717
|
20 |
|
foreach ($currentCollection as $item) { |
|
3718
|
5 |
|
$newChart = clone $item; |
|
3719
|
5 |
|
$this->addChart($newChart); |
|
3720
|
|
|
} |
|
3721
|
20 |
|
} elseif ($key === 'autoFilter') { |
|
3722
|
20 |
|
$newAutoFilter = clone $this->autoFilter; |
|
3723
|
20 |
|
$this->autoFilter = $newAutoFilter; |
|
3724
|
20 |
|
$this->autoFilter->setParent($this); |
|
3725
|
|
|
} else { |
|
3726
|
20 |
|
$this->{$key} = unserialize(serialize($val)); |
|
3727
|
|
|
} |
|
3728
|
|
|
} |
|
3729
|
|
|
} |
|
3730
|
20 |
|
$this->hash = spl_object_id($this); |
|
3731
|
|
|
} |
|
3732
|
|
|
|
|
3733
|
|
|
/** |
|
3734
|
|
|
* Define the code name of the sheet. |
|
3735
|
|
|
* |
|
3736
|
|
|
* @param string $codeName Same rule as Title minus space not allowed (but, like Excel, change |
|
3737
|
|
|
* silently space to underscore) |
|
3738
|
|
|
* @param bool $validate False to skip validation of new title. WARNING: This should only be set |
|
3739
|
|
|
* at parse time (by Readers), where titles can be assumed to be valid. |
|
3740
|
|
|
* |
|
3741
|
|
|
* @return $this |
|
3742
|
|
|
*/ |
|
3743
|
10656 |
|
public function setCodeName(string $codeName, bool $validate = true): static |
|
3744
|
|
|
{ |
|
3745
|
|
|
// Is this a 'rename' or not? |
|
3746
|
10656 |
|
if ($this->getCodeName() == $codeName) { |
|
3747
|
|
|
return $this; |
|
3748
|
|
|
} |
|
3749
|
|
|
|
|
3750
|
10656 |
|
if ($validate) { |
|
3751
|
10656 |
|
$codeName = str_replace(' ', '_', $codeName); //Excel does this automatically without flinching, we are doing the same |
|
3752
|
|
|
|
|
3753
|
|
|
// Syntax check |
|
3754
|
|
|
// throw an exception if not valid |
|
3755
|
10656 |
|
self::checkSheetCodeName($codeName); |
|
3756
|
|
|
|
|
3757
|
|
|
// We use the same code that setTitle to find a valid codeName else not using a space (Excel don't like) but a '_' |
|
3758
|
|
|
|
|
3759
|
10656 |
|
if ($this->parent !== null) { |
|
3760
|
|
|
// Is there already such sheet name? |
|
3761
|
10617 |
|
if ($this->parent->sheetCodeNameExists($codeName)) { |
|
3762
|
|
|
// Use name, but append with lowest possible integer |
|
3763
|
|
|
|
|
3764
|
690 |
|
if (StringHelper::countCharacters($codeName) > 29) { |
|
3765
|
|
|
$codeName = StringHelper::substring($codeName, 0, 29); |
|
3766
|
|
|
} |
|
3767
|
690 |
|
$i = 1; |
|
3768
|
690 |
|
while ($this->getParentOrThrow()->sheetCodeNameExists($codeName . '_' . $i)) { |
|
3769
|
283 |
|
++$i; |
|
3770
|
283 |
|
if ($i == 10) { |
|
3771
|
2 |
|
if (StringHelper::countCharacters($codeName) > 28) { |
|
3772
|
|
|
$codeName = StringHelper::substring($codeName, 0, 28); |
|
3773
|
|
|
} |
|
3774
|
283 |
|
} elseif ($i == 100) { |
|
3775
|
|
|
if (StringHelper::countCharacters($codeName) > 27) { |
|
3776
|
|
|
$codeName = StringHelper::substring($codeName, 0, 27); |
|
3777
|
|
|
} |
|
3778
|
|
|
} |
|
3779
|
|
|
} |
|
3780
|
|
|
|
|
3781
|
690 |
|
$codeName .= '_' . $i; // ok, we have a valid name |
|
3782
|
|
|
} |
|
3783
|
|
|
} |
|
3784
|
|
|
} |
|
3785
|
|
|
|
|
3786
|
10656 |
|
$this->codeName = $codeName; |
|
3787
|
|
|
|
|
3788
|
10656 |
|
return $this; |
|
3789
|
|
|
} |
|
3790
|
|
|
|
|
3791
|
|
|
/** |
|
3792
|
|
|
* Return the code name of the sheet. |
|
3793
|
|
|
*/ |
|
3794
|
10656 |
|
public function getCodeName(): ?string |
|
3795
|
|
|
{ |
|
3796
|
10656 |
|
return $this->codeName; |
|
3797
|
|
|
} |
|
3798
|
|
|
|
|
3799
|
|
|
/** |
|
3800
|
|
|
* Sheet has a code name ? |
|
3801
|
|
|
*/ |
|
3802
|
2 |
|
public function hasCodeName(): bool |
|
3803
|
|
|
{ |
|
3804
|
2 |
|
return $this->codeName !== null; |
|
3805
|
|
|
} |
|
3806
|
|
|
|
|
3807
|
4 |
|
public static function nameRequiresQuotes(string $sheetName): bool |
|
3808
|
|
|
{ |
|
3809
|
4 |
|
return !Preg::isMatch(self::SHEET_NAME_REQUIRES_NO_QUOTES, $sheetName); |
|
3810
|
|
|
} |
|
3811
|
|
|
|
|
3812
|
121 |
|
public function isRowVisible(int $row): bool |
|
3813
|
|
|
{ |
|
3814
|
121 |
|
return !$this->rowDimensionExists($row) || $this->getRowDimension($row)->getVisible(); |
|
3815
|
|
|
} |
|
3816
|
|
|
|
|
3817
|
|
|
/** |
|
3818
|
|
|
* Same as Cell->isLocked, but without creating cell if it doesn't exist. |
|
3819
|
|
|
*/ |
|
3820
|
1 |
|
public function isCellLocked(string $coordinate): bool |
|
3821
|
|
|
{ |
|
3822
|
1 |
|
if ($this->getProtection()->getsheet() !== true) { |
|
3823
|
1 |
|
return false; |
|
3824
|
|
|
} |
|
3825
|
1 |
|
if ($this->cellExists($coordinate)) { |
|
3826
|
1 |
|
return $this->getCell($coordinate)->isLocked(); |
|
3827
|
|
|
} |
|
3828
|
1 |
|
$spreadsheet = $this->parent; |
|
3829
|
1 |
|
$xfIndex = $this->getXfIndex($coordinate); |
|
3830
|
1 |
|
if ($spreadsheet === null || $xfIndex === null) { |
|
3831
|
1 |
|
return true; |
|
3832
|
|
|
} |
|
3833
|
|
|
|
|
3834
|
|
|
return $spreadsheet->getCellXfByIndex($xfIndex)->getProtection()->getLocked() !== StyleProtection::PROTECTION_UNPROTECTED; |
|
3835
|
|
|
} |
|
3836
|
|
|
|
|
3837
|
|
|
/** |
|
3838
|
|
|
* Same as Cell->isHiddenOnFormulaBar, but without creating cell if it doesn't exist. |
|
3839
|
|
|
*/ |
|
3840
|
1 |
|
public function isCellHiddenOnFormulaBar(string $coordinate): bool |
|
3841
|
|
|
{ |
|
3842
|
1 |
|
if ($this->cellExists($coordinate)) { |
|
3843
|
1 |
|
return $this->getCell($coordinate)->isHiddenOnFormulaBar(); |
|
3844
|
|
|
} |
|
3845
|
|
|
|
|
3846
|
|
|
// cell doesn't exist, therefore isn't a formula, |
|
3847
|
|
|
// therefore isn't hidden on formula bar. |
|
3848
|
1 |
|
return false; |
|
3849
|
|
|
} |
|
3850
|
|
|
|
|
3851
|
1 |
|
private function getXfIndex(string $coordinate): ?int |
|
3852
|
|
|
{ |
|
3853
|
1 |
|
[$column, $row] = Coordinate::coordinateFromString($coordinate); |
|
3854
|
1 |
|
$row = (int) $row; |
|
3855
|
1 |
|
$xfIndex = null; |
|
3856
|
1 |
|
if ($this->rowDimensionExists($row)) { |
|
3857
|
|
|
$xfIndex = $this->getRowDimension($row)->getXfIndex(); |
|
3858
|
|
|
} |
|
3859
|
1 |
|
if ($xfIndex === null && $this->ColumnDimensionExists($column)) { |
|
3860
|
|
|
$xfIndex = $this->getColumnDimension($column)->getXfIndex(); |
|
3861
|
|
|
} |
|
3862
|
|
|
|
|
3863
|
1 |
|
return $xfIndex; |
|
3864
|
|
|
} |
|
3865
|
|
|
|
|
3866
|
|
|
private string $backgroundImage = ''; |
|
3867
|
|
|
|
|
3868
|
|
|
private string $backgroundMime = ''; |
|
3869
|
|
|
|
|
3870
|
|
|
private string $backgroundExtension = ''; |
|
3871
|
|
|
|
|
3872
|
978 |
|
public function getBackgroundImage(): string |
|
3873
|
|
|
{ |
|
3874
|
978 |
|
return $this->backgroundImage; |
|
3875
|
|
|
} |
|
3876
|
|
|
|
|
3877
|
394 |
|
public function getBackgroundMime(): string |
|
3878
|
|
|
{ |
|
3879
|
394 |
|
return $this->backgroundMime; |
|
3880
|
|
|
} |
|
3881
|
|
|
|
|
3882
|
394 |
|
public function getBackgroundExtension(): string |
|
3883
|
|
|
{ |
|
3884
|
394 |
|
return $this->backgroundExtension; |
|
3885
|
|
|
} |
|
3886
|
|
|
|
|
3887
|
|
|
/** |
|
3888
|
|
|
* Set background image. |
|
3889
|
|
|
* Used on read/write for Xlsx. |
|
3890
|
|
|
* Used on write for Html. |
|
3891
|
|
|
* |
|
3892
|
|
|
* @param string $backgroundImage Image represented as a string, e.g. results of file_get_contents |
|
3893
|
|
|
*/ |
|
3894
|
4 |
|
public function setBackgroundImage(string $backgroundImage): self |
|
3895
|
|
|
{ |
|
3896
|
4 |
|
$imageArray = getimagesizefromstring($backgroundImage) ?: ['mime' => '']; |
|
3897
|
4 |
|
$mime = $imageArray['mime']; |
|
3898
|
4 |
|
if ($mime !== '') { |
|
3899
|
3 |
|
$extension = explode('/', $mime); |
|
3900
|
3 |
|
$extension = $extension[1]; |
|
3901
|
3 |
|
$this->backgroundImage = $backgroundImage; |
|
3902
|
3 |
|
$this->backgroundMime = $mime; |
|
3903
|
3 |
|
$this->backgroundExtension = $extension; |
|
3904
|
|
|
} |
|
3905
|
|
|
|
|
3906
|
4 |
|
return $this; |
|
3907
|
|
|
} |
|
3908
|
|
|
|
|
3909
|
|
|
/** |
|
3910
|
|
|
* Copy cells, adjusting relative cell references in formulas. |
|
3911
|
|
|
* Acts similarly to Excel "fill handle" feature. |
|
3912
|
|
|
* |
|
3913
|
|
|
* @param string $fromCell Single source cell, e.g. C3 |
|
3914
|
|
|
* @param string $toCells Single cell or cell range, e.g. C4 or C4:C10 |
|
3915
|
|
|
* @param bool $copyStyle Copy styles as well as values, defaults to true |
|
3916
|
|
|
*/ |
|
3917
|
1 |
|
public function copyCells(string $fromCell, string $toCells, bool $copyStyle = true): void |
|
3918
|
|
|
{ |
|
3919
|
1 |
|
$toArray = Coordinate::extractAllCellReferencesInRange($toCells); |
|
3920
|
1 |
|
$valueString = $this->getCell($fromCell)->getValueString(); |
|
3921
|
|
|
/** @var mixed[][] */ |
|
3922
|
1 |
|
$style = $this->getStyle($fromCell)->exportArray(); |
|
3923
|
1 |
|
$fromIndexes = Coordinate::indexesFromString($fromCell); |
|
3924
|
1 |
|
$referenceHelper = ReferenceHelper::getInstance(); |
|
3925
|
1 |
|
foreach ($toArray as $destination) { |
|
3926
|
1 |
|
if ($destination !== $fromCell) { |
|
3927
|
1 |
|
$toIndexes = Coordinate::indexesFromString($destination); |
|
3928
|
1 |
|
$this->getCell($destination)->setValue($referenceHelper->updateFormulaReferences($valueString, 'A1', $toIndexes[0] - $fromIndexes[0], $toIndexes[1] - $fromIndexes[1])); |
|
3929
|
1 |
|
if ($copyStyle) { |
|
3930
|
1 |
|
$this->getCell($destination)->getStyle()->applyFromArray($style); |
|
3931
|
|
|
} |
|
3932
|
|
|
} |
|
3933
|
|
|
} |
|
3934
|
|
|
} |
|
3935
|
|
|
|
|
3936
|
1138 |
|
public function calculateArrays(bool $preCalculateFormulas = true): void |
|
3937
|
|
|
{ |
|
3938
|
1138 |
|
if ($preCalculateFormulas && Calculation::getInstance($this->parent)->getInstanceArrayReturnType() === Calculation::RETURN_ARRAY_AS_ARRAY) { |
|
3939
|
46 |
|
$keys = $this->cellCollection->getCoordinates(); |
|
3940
|
46 |
|
foreach ($keys as $key) { |
|
3941
|
46 |
|
if ($this->getCell($key)->getDataType() === DataType::TYPE_FORMULA) { |
|
3942
|
46 |
|
if (!Preg::isMatch(self::FUNCTION_LIKE_GROUPBY, $this->getCell($key)->getValueString())) { |
|
3943
|
45 |
|
$this->getCell($key)->getCalculatedValue(); |
|
3944
|
|
|
} |
|
3945
|
|
|
} |
|
3946
|
|
|
} |
|
3947
|
|
|
} |
|
3948
|
|
|
} |
|
3949
|
|
|
|
|
3950
|
2 |
|
public function isCellInSpillRange(string $coordinate): bool |
|
3951
|
|
|
{ |
|
3952
|
2 |
|
if (Calculation::getInstance($this->parent)->getInstanceArrayReturnType() !== Calculation::RETURN_ARRAY_AS_ARRAY) { |
|
3953
|
1 |
|
return false; |
|
3954
|
|
|
} |
|
3955
|
1 |
|
$this->calculateArrays(); |
|
3956
|
1 |
|
$keys = $this->cellCollection->getCoordinates(); |
|
3957
|
1 |
|
foreach ($keys as $key) { |
|
3958
|
1 |
|
$attributes = $this->getCell($key)->getFormulaAttributes(); |
|
3959
|
1 |
|
if (isset($attributes['ref'])) { |
|
3960
|
1 |
|
if (Coordinate::coordinateIsInsideRange($attributes['ref'], $coordinate)) { |
|
3961
|
|
|
// false for first cell in range, true otherwise |
|
3962
|
1 |
|
return $coordinate !== $key; |
|
3963
|
|
|
} |
|
3964
|
|
|
} |
|
3965
|
|
|
} |
|
3966
|
|
|
|
|
3967
|
1 |
|
return false; |
|
3968
|
|
|
} |
|
3969
|
|
|
|
|
3970
|
|
|
/** @param mixed[][] $styleArray */ |
|
3971
|
2 |
|
public function applyStylesFromArray(string $coordinate, array $styleArray): bool |
|
3972
|
|
|
{ |
|
3973
|
2 |
|
$spreadsheet = $this->parent; |
|
3974
|
2 |
|
if ($spreadsheet === null) { |
|
3975
|
1 |
|
return false; |
|
3976
|
|
|
} |
|
3977
|
1 |
|
$activeSheetIndex = $spreadsheet->getActiveSheetIndex(); |
|
3978
|
1 |
|
$originalSelected = $this->selectedCells; |
|
3979
|
1 |
|
$this->getStyle($coordinate)->applyFromArray($styleArray); |
|
3980
|
1 |
|
$this->setSelectedCells($originalSelected); |
|
3981
|
1 |
|
if ($activeSheetIndex >= 0) { |
|
3982
|
1 |
|
$spreadsheet->setActiveSheetIndex($activeSheetIndex); |
|
3983
|
|
|
} |
|
3984
|
|
|
|
|
3985
|
1 |
|
return true; |
|
3986
|
|
|
} |
|
3987
|
|
|
} |
|
3988
|
|
|
|