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