← All changes
|
libraries/vendor/PhpSpreadsheet/Worksheet/Worksheet.php
+211
-11
3.3.4
→
3.4
View file →
| @@ -31,8 +31,12 @@ | ||
| 31 | 31 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Conditional; |
| 32 | 32 | use TablePress\PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
| 33 | 33 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Protection as StyleProtection; |
| 34 | 34 | use TablePress\PhpOffice\PhpSpreadsheet\Style\Style; |
| 35 | +use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\PivotTable\PivotTable; | |
| 36 | +use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\Sparkline; | |
| 37 | +use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\SparklineGroup; | |
| 38 | +use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\SparklineType; | |
| 35 | 39 | |
| 36 | 40 | class Worksheet |
| 37 | 41 | { |
| 38 | 42 | // Break types |
| @@ -129,8 +133,22 @@ | ||
| 129 | 133 | */ |
| 130 | 134 | private ArrayObject $tableCollection; |
| 131 | 135 | |
| 132 | 136 | /** |
| 137 | + * Collection of SparklineGroup objects. | |
| 138 | + * | |
| 139 | + * @var ArrayObject<int, SparklineGroup> | |
| 140 | + */ | |
| 141 | + private ArrayObject $sparklineGroupCollection; | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Collection of PivotTable objects. | |
| 145 | + * | |
| 146 | + * @var ArrayObject<int, PivotTable> | |
| 147 | + */ | |
| 148 | + private ArrayObject $pivotTableCollection; | |
| 149 | + | |
| 150 | + /** | |
| 133 | 151 | * Worksheet title. |
| 134 | 152 | */ |
| 135 | 153 | private string $title = ''; |
| 136 | 154 | |
| @@ -324,9 +342,11 @@ | ||
| 324 | 342 | public function __construct(?Spreadsheet $parent = null, string $title = 'Worksheet') |
| 325 | 343 | { |
| 326 | 344 | // Set parent and title |
| 327 | 345 | $this->parent = $parent; |
| 328 | - $this->setTitle($title, false); | |
| 346 | + // Chart collection must be set before title | |
| 347 | + $this->chartCollection = new ArrayObject(); | |
| 348 | + $this->setTitle($title, false, true, false); | |
| 329 | 349 | // setTitle can change $pTitle |
| 330 | 350 | $this->setCodeName($this->getTitle()); |
| 331 | 351 | $this->setSheetState(self::SHEETSTATE_VISIBLE); |
| 332 | 352 | |
| @@ -342,10 +362,8 @@ | ||
| 342 | 362 | // Drawing collection |
| 343 | 363 | $this->drawingCollection = new ArrayObject(); |
| 344 | 364 | // In Cell Drawing collection |
| 345 | 365 | $this->inCellDrawingCollection = new ArrayObject(); |
| 346 | - // Chart collection | |
| 347 | - $this->chartCollection = new ArrayObject(); | |
| 348 | 366 | // Protection |
| 349 | 367 | $this->protection = new Protection(); |
| 350 | 368 | // Default row dimension |
| 351 | 369 | $this->defaultRowDimension = new RowDimension(null); |
| @@ -354,8 +372,13 @@ | ||
| 354 | 372 | // AutoFilter |
| 355 | 373 | $this->autoFilter = new AutoFilter('', $this); |
| 356 | 374 | // Table collection |
| 357 | 375 | $this->tableCollection = new ArrayObject(); |
| 376 | + // Sparkline group collection | |
| 377 | + $this->sparklineGroupCollection = new ArrayObject(); | |
| 378 | + | |
| 379 | + // Pivot table collection | |
| 380 | + $this->pivotTableCollection = new ArrayObject(); | |
| 358 | 381 | } |
| 359 | 382 | |
| 360 | 383 | /** |
| 361 | 384 | * Disconnect all cells from this Worksheet object, |
| @@ -364,10 +387,9 @@ | ||
| 364 | 387 | * this method has completed. |
| 365 | 388 | */ |
| 366 | 389 | public function disconnectCells(): void |
| 367 | 390 | { |
| 368 | - // isset needed to avoid problems at destruct time | |
| 369 | - if (isset($this->cellCollection)) { //* @phpstan-ignore-line | |
| 391 | + if (isset($this->cellCollection)) { //* @phpstan-ignore isset.initializedProperty (may be null at destruct time) | |
| 370 | 392 | $this->cellCollection->unsetWorksheetCells(); |
| 371 | 393 | unset($this->cellCollection); |
| 372 | 394 | } |
| 373 | 395 | // detach ourself from the workbook, so that it can then delete this worksheet successfully |
| @@ -381,9 +403,9 @@ | ||
| 381 | 403 | { |
| 382 | 404 | ($nullsafeVariable1 = Calculation::getInstanceOrNull($this->parent)) ? $nullsafeVariable1->clearCalculationCacheForWorksheet($this->title) : null; |
| 383 | 405 | |
| 384 | 406 | $this->disconnectCells(); |
| 385 | - unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->drawingCollection, $this->inCellDrawingCollection, $this->chartCollection, $this->autoFilter); | |
| 407 | + unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->sparklineGroupCollection, $this->drawingCollection, $this->inCellDrawingCollection, $this->chartCollection, $this->autoFilter, $this->pivotTableCollection); | |
| 386 | 408 | } |
| 387 | 409 | |
| 388 | 410 | /** |
| 389 | 411 | * Return the cell collection. |
| @@ -463,10 +485,9 @@ | ||
| 463 | 485 | * @return string[] |
| 464 | 486 | */ |
| 465 | 487 | public function getCoordinates(bool $sorted = true): array |
| 466 | 488 | { |
| 467 | - // isset needed to avoid problems at destruct time | |
| 468 | - if (!isset($this->cellCollection)) { //* @phpstan-ignore-line | |
| 489 | + if (!isset($this->cellCollection)) { //* @phpstan-ignore isset.initializedProperty (may be null at destruct time) | |
| 469 | 490 | return []; |
| 470 | 491 | } |
| 471 | 492 | |
| 472 | 493 | if ($sorted) { |
| @@ -878,9 +899,9 @@ | ||
| 878 | 899 | * at parse time (by Readers), where titles can be assumed to be valid. |
| 879 | 900 | * |
| 880 | 901 | * @return $this |
| 881 | 902 | */ |
| 882 | - public function setTitle(string $title, bool $updateFormulaCellReferences = true, bool $validate = true) | |
| 903 | + public function setTitle(string $title, bool $updateFormulaCellReferences = true, bool $validate = true, bool $changeChartSheetNames = true) | |
| 883 | 904 | { |
| 884 | 905 | // Is this a 'rename' or not? |
| 885 | 906 | if ($this->getTitle() == $title) { |
| 886 | 907 | return $this; |
| @@ -931,12 +952,63 @@ | ||
| 931 | 952 | if ($updateFormulaCellReferences) { |
| 932 | 953 | ReferenceHelper::getInstance()->updateNamedFormulae($this->parent, $oldTitle, $newTitle); |
| 933 | 954 | } |
| 934 | 955 | } |
| 956 | + if ($changeChartSheetNames) { | |
| 957 | + $this->changeChartSheetNames($oldTitle, $title); | |
| 958 | + } | |
| 935 | 959 | |
| 936 | 960 | return $this; |
| 937 | 961 | } |
| 938 | 962 | |
| 963 | + private function changeChartSheetNames(string $oldTitle, string $title): void | |
| 964 | + { | |
| 965 | + $worksheets = [$this]; | |
| 966 | + if ($this->parent !== null) { | |
| 967 | + $sheets = $this->parent->getAllSheets(); | |
| 968 | + if (in_array($this, $sheets, true)) { | |
| 969 | + $worksheets = $sheets; | |
| 970 | + } | |
| 971 | + } | |
| 972 | + $titleq = "'$title'!"; | |
| 973 | + $oldTitleq1 = preg_quote("'$oldTitle'!"); | |
| 974 | + $oldTitleq2 = preg_quote("$oldTitle!"); | |
| 975 | + $preg1 = "/$oldTitleq1|\\b$oldTitleq2/"; | |
| 976 | + foreach ($worksheets as $sheet) { | |
| 977 | + foreach ($sheet->getChartCollection() as $chart) { | |
| 978 | + foreach (((($nullsafeVariable3 = $chart->getPlotArea()) ? $nullsafeVariable3->getPlotGroup() : null) ?? []) as $plotGroup) { | |
| 979 | + foreach ($plotGroup->getPlotCategories() as $plotCategory) { | |
| 980 | + $dataSource = (string) $plotCategory->getDataSource(); | |
| 981 | + $dataSource2 = Preg::replace($preg1, $titleq, $dataSource); | |
| 982 | + if ($dataSource2 !== $dataSource) { | |
| 983 | + $plotCategory->setDataSource( | |
| 984 | + $dataSource2 | |
| 985 | + ); | |
| 986 | + } | |
| 987 | + } | |
| 988 | + foreach ($plotGroup->getPlotLabels() as $plotLabel) { | |
| 989 | + $dataSource = (string) $plotLabel->getDataSource(); | |
| 990 | + $dataSource2 = Preg::replace($preg1, $titleq, $dataSource); | |
| 991 | + if ($dataSource2 !== $dataSource) { | |
| 992 | + $plotLabel->setDataSource( | |
| 993 | + $dataSource2 | |
| 994 | + ); | |
| 995 | + } | |
| 996 | + } | |
| 997 | + foreach ($plotGroup->getPlotValues() as $plotValue) { | |
| 998 | + $dataSource = (string) $plotValue->getDataSource(); | |
| 999 | + $dataSource2 = Preg::replace($preg1, $titleq, $dataSource); | |
| 1000 | + if ($dataSource2 !== $dataSource) { | |
| 1001 | + $plotValue->setDataSource( | |
| 1002 | + $dataSource2 | |
| 1003 | + ); | |
| 1004 | + } | |
| 1005 | + } | |
| 1006 | + } | |
| 1007 | + } | |
| 1008 | + } | |
| 1009 | + } | |
| 1010 | + | |
| 939 | 1011 | /** |
| 940 | 1012 | * Get sheet state. |
| 941 | 1013 | * |
| 942 | 1014 | * @return string Sheet state (visible, hidden, veryHidden) |
| @@ -1356,9 +1428,9 @@ | ||
| 1356 | 1428 | } |
| 1357 | 1429 | |
| 1358 | 1430 | public function getRowStyle(int $row): ?Style |
| 1359 | 1431 | { |
| 1360 | - return ($nullsafeVariable3 = $this->parent) ? $nullsafeVariable3->getCellXfByIndexOrNull(($nullsafeVariable5 = $this->rowDimensions[$row] ?? null) ? $nullsafeVariable5->getXfIndex() : null) : null; | |
| 1432 | + return ($nullsafeVariable4 = $this->parent) ? $nullsafeVariable4->getCellXfByIndexOrNull(($nullsafeVariable6 = $this->rowDimensions[$row] ?? null) ? $nullsafeVariable6->getXfIndex() : null) : null; | |
| 1361 | 1433 | } |
| 1362 | 1434 | |
| 1363 | 1435 | public function rowDimensionExists(int $row): bool |
| 1364 | 1436 | { |
| @@ -1404,9 +1476,9 @@ | ||
| 1404 | 1476 | } |
| 1405 | 1477 | |
| 1406 | 1478 | public function getColumnStyle(string $column): ?Style |
| 1407 | 1479 | { |
| 1408 | - return ($nullsafeVariable4 = $this->parent) ? $nullsafeVariable4->getCellXfByIndexOrNull(($nullsafeVariable6 = $this->columnDimensions[$column] ?? null) ? $nullsafeVariable6->getXfIndex() : null) : null; | |
| 1480 | + return ($nullsafeVariable5 = $this->parent) ? $nullsafeVariable5->getCellXfByIndexOrNull(($nullsafeVariable7 = $this->columnDimensions[$column] ?? null) ? $nullsafeVariable7->getXfIndex() : null) : null; | |
| 1409 | 1481 | } |
| 1410 | 1482 | |
| 1411 | 1483 | /** |
| 1412 | 1484 | * Get style for cell. |
| @@ -2165,8 +2237,136 @@ | ||
| 2165 | 2237 | */ |
| 2166 | 2238 | public function removeTableCollection(): self |
| 2167 | 2239 | { |
| 2168 | 2240 | $this->tableCollection = new ArrayObject(); |
| 2241 | + | |
| 2242 | + return $this; | |
| 2243 | + } | |
| 2244 | + | |
| 2245 | + /** | |
| 2246 | + * Get collection of SparklineGroups. | |
| 2247 | + * | |
| 2248 | + * @return ArrayObject<int, SparklineGroup> | |
| 2249 | + */ | |
| 2250 | + public function getSparklineGroupCollection(): ArrayObject | |
| 2251 | + { | |
| 2252 | + return $this->sparklineGroupCollection; | |
| 2253 | + } | |
| 2254 | + | |
| 2255 | + /** | |
| 2256 | + * Add a SparklineGroup. | |
| 2257 | + * | |
| 2258 | + * @return $this | |
| 2259 | + */ | |
| 2260 | + public function addSparklineGroup(SparklineGroup $sparklineGroup): self | |
| 2261 | + { | |
| 2262 | + $this->sparklineGroupCollection[] = $sparklineGroup; | |
| 2263 | + | |
| 2264 | + return $this; | |
| 2265 | + } | |
| 2266 | + | |
| 2267 | + /** | |
| 2268 | + * Add a single Sparkline, wrapping it in its own SparklineGroup. | |
| 2269 | + * | |
| 2270 | + * This is a convenience method for the common case of adding one sparkline | |
| 2271 | + * with default formatting; the created group is returned so its formatting | |
| 2272 | + * can be adjusted. | |
| 2273 | + * | |
| 2274 | + * @param mixed $type the type of sparkline (defaults to line) | |
| 2275 | + * @param \TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\SparklineType::* $type | |
| 2276 | + */ | |
| 2277 | + public function addSparkline(Sparkline $sparkline, $type = SparklineType::Line): SparklineGroup | |
| 2278 | + { | |
| 2279 | + $group = new SparklineGroup(); | |
| 2280 | + $group->setType($type); | |
| 2281 | + $group->addSparkline($sparkline); | |
| 2282 | + $this->addSparklineGroup($group); | |
| 2283 | + | |
| 2284 | + return $group; | |
| 2285 | + } | |
| 2286 | + | |
| 2287 | + /** | |
| 2288 | + * Remove all SparklineGroups. | |
| 2289 | + * | |
| 2290 | + * @return $this | |
| 2291 | + */ | |
| 2292 | + public function removeSparklineGroupCollection(): self | |
| 2293 | + { | |
| 2294 | + $this->sparklineGroupCollection = new ArrayObject(); | |
| 2295 | + | |
| 2296 | + return $this; | |
| 2297 | + } | |
| 2298 | + | |
| 2299 | + /** | |
| 2300 | + * Get collection of PivotTables. | |
| 2301 | + * | |
| 2302 | + * @return ArrayObject<int, PivotTable> | |
| 2303 | + */ | |
| 2304 | + public function getPivotTableCollection(): ArrayObject | |
| 2305 | + { | |
| 2306 | + return $this->pivotTableCollection; | |
| 2307 | + } | |
| 2308 | + | |
| 2309 | + /** | |
| 2310 | + * Get collection of PivotTables (alias of getPivotTableCollection()). | |
| 2311 | + * | |
| 2312 | + * @return ArrayObject<int, PivotTable> | |
| 2313 | + */ | |
| 2314 | + public function getPivotTables(): ArrayObject | |
| 2315 | + { | |
| 2316 | + return $this->pivotTableCollection; | |
| 2317 | + } | |
| 2318 | + | |
| 2319 | + /** | |
| 2320 | + * Add a PivotTable to this worksheet. | |
| 2321 | + * | |
| 2322 | + * @return $this | |
| 2323 | + */ | |
| 2324 | + public function addPivotTable(PivotTable $pivotTable): self | |
| 2325 | + { | |
| 2326 | + $pivotTable->setWorksheet($this); | |
| 2327 | + $this->pivotTableCollection[] = $pivotTable; | |
| 2328 | + | |
| 2329 | + return $this; | |
| 2330 | + } | |
| 2331 | + | |
| 2332 | + /** | |
| 2333 | + * @return string[] array of PivotTable names | |
| 2334 | + */ | |
| 2335 | + public function getPivotTableNames(): array | |
| 2336 | + { | |
| 2337 | + $pivotTableNames = []; | |
| 2338 | + | |
| 2339 | + foreach ($this->pivotTableCollection as $pivotTable) { | |
| 2340 | + $pivotTableNames[] = $pivotTable->getName(); | |
| 2341 | + } | |
| 2342 | + | |
| 2343 | + return $pivotTableNames; | |
| 2344 | + } | |
| 2345 | + | |
| 2346 | + /** | |
| 2347 | + * @param string $name the pivot table name to search | |
| 2348 | + * | |
| 2349 | + * @return null|PivotTable The pivot table from the collection, or null if not found | |
| 2350 | + */ | |
| 2351 | + public function getPivotTableByName(string $name): ?PivotTable | |
| 2352 | + { | |
| 2353 | + $name = StringHelper::strToUpper($name); | |
| 2354 | + foreach ($this->pivotTableCollection as $pivotTable) { | |
| 2355 | + if (StringHelper::strToUpper($pivotTable->getName()) === $name) { | |
| 2356 | + return $pivotTable; | |
| 2357 | + } | |
| 2358 | + } | |
| 2359 | + | |
| 2360 | + return null; | |
| 2361 | + } | |
| 2362 | + | |
| 2363 | + /** | |
| 2364 | + * Remove collection of PivotTables. | |
| 2365 | + */ | |
| 2366 | + public function removePivotTableCollection(): self | |
| 2367 | + { | |
| 2368 | + $this->pivotTableCollection = new ArrayObject(); | |
| 2169 | 2369 | |
| 2170 | 2370 | return $this; |
| 2171 | 2371 | } |
| 2172 | 2372 | |