Skip to content

Commit

Permalink
Merge pull request #3821 from oleibman/issue3820
Browse files Browse the repository at this point in the history
Clone Worksheet With Table and/or Chart
  • Loading branch information
oleibman authored Dec 13, 2023
2 parents da58d6c + 83daee8 commit 0fd7cc3
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Case Insensitive Comparison for Sheet Names [PR #3791](https://github.com/PHPOffice/PhpSpreadsheet/pull/3791)
- Performance improvement for Xlsx Reader. [Issue #3683](https://github.com/PHPOffice/PhpSpreadsheet/issues/3683) [PR #3810](https://github.com/PHPOffice/PhpSpreadsheet/pull/3810)
- Prevent loop in Shared/File. [Issue #3807](https://github.com/PHPOffice/PhpSpreadsheet/issues/3807) [PR #3809](https://github.com/PHPOffice/PhpSpreadsheet/pull/3809)
- Consistent handling of decimal/thousands separators between StringHelper and Php setlocale. [Issue #3811](https://github.com/PHPOffice/PhpSpreadsheet/issues/3811) [PR #3815](https://github.com/PHPOffice/PhpSpreadsheet/pull/3815)
- Clone worksheet with tables or charts. [Issue #3820](https://github.com/PHPOffice/PhpSpreadsheet/issues/3820) [PR #3821](https://github.com/PHPOffice/PhpSpreadsheet/pull/3821)

## 1.29.0 - 2023-06-15

Expand Down
12 changes: 12 additions & 0 deletions src/PhpSpreadsheet/Chart/Axis.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,16 @@ public function getNoFill(): bool
{
return $this->noFill;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
parent::__clone();
$this->majorGridlines = ($this->majorGridlines === null) ? null : clone $this->majorGridlines;
$this->majorGridlines = ($this->minorGridlines === null) ? null : clone $this->minorGridlines;
$this->axisText = ($this->axisText === null) ? null : clone $this->axisText;
$this->fillColor = clone $this->fillColor;
}
}
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Chart/AxisText.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public function setFont(Font $font): self

return $this;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
parent::__clone();
$this->font = clone $this->font;
}
}
17 changes: 17 additions & 0 deletions src/PhpSpreadsheet/Chart/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,4 +835,21 @@ public function getRenderedHeight(): ?float
{
return $this->renderedHeight;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->worksheet = null;
$this->title = ($this->title === null) ? null : clone $this->title;
$this->legend = ($this->legend === null) ? null : clone $this->legend;
$this->xAxisLabel = ($this->xAxisLabel === null) ? null : clone $this->xAxisLabel;
$this->yAxisLabel = ($this->yAxisLabel === null) ? null : clone $this->yAxisLabel;
$this->plotArea = ($this->plotArea === null) ? null : clone $this->plotArea;
$this->xAxis = clone $this->xAxis;
$this->yAxis = clone $this->yAxis;
$this->borderLines = clone $this->borderLines;
$this->fillColor = clone $this->fillColor;
}
}
27 changes: 27 additions & 0 deletions src/PhpSpreadsheet/Chart/DataSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,31 @@ public function refresh(Worksheet $worksheet): void
}
}
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$plotLabels = $this->plotLabel;
$this->plotLabel = [];
foreach ($plotLabels as $plotLabel) {
$this->plotLabel[] = $plotLabel;
}
$plotCategories = $this->plotCategory;
$this->plotCategory = [];
foreach ($plotCategories as $plotCategory) {
$this->plotCategory[] = clone $plotCategory;
}
$plotValues = $this->plotValues;
$this->plotValues = [];
foreach ($plotValues as $plotValue) {
$this->plotValues[] = clone $plotValue;
}
$plotBubbleSizes = $this->plotBubbleSizes;
$this->plotBubbleSizes = [];
foreach ($plotBubbleSizes as $plotBubbleSize) {
$this->plotBubbleSizes[] = clone $plotBubbleSize;
}
}
}
25 changes: 25 additions & 0 deletions src/PhpSpreadsheet/Chart/DataSeriesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,29 @@ public function getTrendLines(): array
{
return $this->trendLines;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
parent::__clone();
$this->markerFillColor = clone $this->markerFillColor;
$this->markerBorderColor = clone $this->markerBorderColor;
if (is_array($this->fillColor)) {
$fillColor = $this->fillColor;
$this->fillColor = [];
foreach ($fillColor as $color) {
$this->fillColor[] = clone $color;
}
} elseif ($this->fillColor instanceof ChartColor) {
$this->fillColor = clone $this->fillColor;
}
$this->labelLayout = ($this->labelLayout === null) ? null : clone $this->labelLayout;
$trendLines = $this->trendLines;
$this->trendLines = [];
foreach ($trendLines as $trendLine) {
$this->trendLines[] = clone $trendLine;
}
}
}
11 changes: 11 additions & 0 deletions src/PhpSpreadsheet/Chart/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,15 @@ public function setNumFmtLinked(bool $numFmtLinked): self

return $this;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->labelFillColor = ($this->labelFillColor === null) ? null : clone $this->labelFillColor;
$this->labelBorderColor = ($this->labelBorderColor === null) ? null : clone $this->labelBorderColor;
$this->labelFont = ($this->labelFont === null) ? null : clone $this->labelFont;
$this->labelEffects = ($this->labelEffects === null) ? null : clone $this->labelEffects;
}
}
11 changes: 11 additions & 0 deletions src/PhpSpreadsheet/Chart/Legend.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,15 @@ public function setBorderLines(GridLines $borderLines): self

return $this;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->layout = ($this->layout === null) ? null : clone $this->layout;
$this->legendText = ($this->legendText === null) ? null : clone $this->legendText;
$this->borderLines = clone $this->borderLines;
$this->fillColor = clone $this->fillColor;
}
}
13 changes: 13 additions & 0 deletions src/PhpSpreadsheet/Chart/PlotArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,17 @@ public function setUseDownBars(bool $useDownBars): self

return $this;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->layout = ($this->layout === null) ? null : clone $this->layout;
$plotSeries = $this->plotSeries;
$this->plotSeries = [];
foreach ($plotSeries as $series) {
$this->plotSeries[] = clone $series;
}
}
}
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Chart/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -957,4 +957,14 @@ public function getLineStyleArrowLength($arrow)
{
return $this->getLineStyleProperty(['arrow', $arrow, 'len']);
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->lineColor = clone $this->lineColor;
$this->glowColor = clone $this->glowColor;
$this->shadowColor = clone $this->shadowColor;
}
}
18 changes: 18 additions & 0 deletions src/PhpSpreadsheet/Chart/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,22 @@ public function setFont(?Font $font): self

return $this;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->layout = ($this->layout === null) ? null : clone $this->layout;
$this->font = ($this->font === null) ? null : clone $this->font;
if (is_array($this->caption)) {
$captions = $this->caption;
$this->caption = [];
foreach ($captions as $caption) {
$this->caption[] = is_object($caption) ? (clone $caption) : $caption;
}
} else {
$this->caption = is_object($this->caption) ? (clone $this->caption) : $this->caption;
}
}
}
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,4 +878,14 @@ public function getCap(): ?string
{
return $this->cap;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$this->color = clone $this->color;
$this->chartColor = ($this->chartColor === null) ? null : clone $this->chartColor;
$this->underlineColor = ($this->underlineColor === null) ? null : clone $this->underlineColor;
}
}
21 changes: 17 additions & 4 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3739,10 +3739,23 @@ public function __clone()
$currentCollection = $this->drawingCollection;
$this->drawingCollection = new ArrayObject();
foreach ($currentCollection as $item) {
if (is_object($item)) {
$newDrawing = clone $item;
$newDrawing->setWorksheet($this);
}
$newDrawing = clone $item;
$newDrawing->setWorksheet($this);
}
} elseif ($key == 'tableCollection') {
$currentCollection = $this->tableCollection;
$this->tableCollection = new ArrayObject();
foreach ($currentCollection as $item) {
$newTable = clone $item;
$newTable->setName($item->getName() . 'clone');
$this->addTable($newTable);
}
} elseif ($key == 'chartCollection') {
$currentCollection = $this->chartCollection;
$this->chartCollection = new ArrayObject();
foreach ($currentCollection as $item) {
$newChart = clone $item;
$this->addChart($newChart);
}
} elseif (($key == 'autoFilter') && ($this->autoFilter instanceof AutoFilter)) {
$newAutoFilter = clone $this->autoFilter;
Expand Down
Loading

0 comments on commit 0fd7cc3

Please sign in to comment.