Skip to content

Commit f98fd23

Browse files
authored
Merge pull request #4315 from oleibman/duplicatesheet
Add Spreadsheet Method for Duplicating Worksheet
2 parents 5b96bcc + c176cc4 commit f98fd23

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
99

1010
### Added
1111

12-
- Nothing yet.
12+
- Method for duplicating worksheet in spreadsheet. [PR #4315](https://github.com/PHPOffice/PhpSpreadsheet/pull/4315)
1313

1414
### Changed
1515

docs/topics/worksheets.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,38 @@ insert the clone into the workbook.
9595

9696
```php
9797
$clonedWorksheet = clone $spreadsheet->getSheetByName('Worksheet 1');
98-
$clonedWorksheet->setTitle('Copy of Worksheet 1');
98+
$clonedWorksheet->setTitle('Copy of Worksheet 1'); // must be unique
9999
$spreadsheet->addSheet($clonedWorksheet);
100100
```
101+
Starting with PhpSpreadsheet 3.9.0, this can be done more simply (copied sheet's title will be set to something unique):
102+
```php
103+
$copiedWorksheet = $spreadsheet->duplicateWorksheetByTitle('sheetname');
104+
```
101105

102106
You can also copy worksheets from one workbook to another, though this
103107
is more complex as PhpSpreadsheet also has to replicate the styling
104108
between the two workbooks. The `addExternalSheet()` method is provided for
105109
this purpose.
106110

107-
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
108-
$spreadsheet->addExternalSheet($clonedWorksheet);
111+
```php
112+
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
113+
$clonedWorksheet->setTitle('Copy of Worksheet 1'); // must be unique
114+
$spreadsheet1->addSheet($clonedWorksheet);
115+
$spreadsheet->addExternalSheet($clonedWorksheet);
116+
```
117+
Starting with PhpSpreadsheet 3.8.0, this can be simplified:
118+
```php
119+
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
120+
$spreadsheet1->addSheet($clonedWorksheet, null, true);
121+
$spreadsheet->addExternalSheet($clonedWorksheet);
122+
```
123+
Starting with PhpSpreadsheet 3.9.0, this can be simplified even further:
124+
```php
125+
$clonedWorksheet = $spreadsheet1->duplicateWorksheetByTitle('sheetname');
126+
$spreadsheet->addExternalSheet($clonedWorksheet);
127+
```
109128

110-
In both cases, it is the developer's responsibility to ensure that
129+
In the cases commented "must be unique", it is the developer's responsibility to ensure that
111130
worksheet names are not duplicated. PhpSpreadsheet will throw an
112131
exception if you attempt to copy worksheets that will result in a
113132
duplicate name.

src/PhpSpreadsheet/Spreadsheet.php

+9
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,15 @@ public function sheetNameExists(string $worksheetName): bool
529529
return $this->getSheetByName($worksheetName) !== null;
530530
}
531531

532+
public function duplicateWorksheetByTitle(string $title): Worksheet
533+
{
534+
$original = $this->getSheetByNameOrThrow($title);
535+
$index = $this->getIndex($original) + 1;
536+
$clone = clone $original;
537+
538+
return $this->addSheet($clone, $index, true);
539+
}
540+
532541
/**
533542
* Add sheet.
534543
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SpreadsheetDuplicateSheetTest extends TestCase
11+
{
12+
private ?Spreadsheet $spreadsheet = null;
13+
14+
protected function tearDown(): void
15+
{
16+
if ($this->spreadsheet !== null) {
17+
$this->spreadsheet->disconnectWorksheets();
18+
$this->spreadsheet = null;
19+
}
20+
}
21+
22+
public function testDuplicate(): void
23+
{
24+
$this->spreadsheet = new Spreadsheet();
25+
$sheet = $this->spreadsheet->getActiveSheet();
26+
$sheet->setTitle('original');
27+
$sheet->getCell('A1')->setValue('text1');
28+
$sheet->getCell('A2')->setValue('text2');
29+
$sheet->getStyle('A1')
30+
->getFont()
31+
->setBold(true);
32+
$sheet3 = $this->spreadsheet->createSheet();
33+
$sheet3->setTitle('added');
34+
$newSheet = $this->spreadsheet
35+
->duplicateWorksheetByTitle('original');
36+
$this->spreadsheet->duplicateWorksheetByTitle('added');
37+
self::assertSame('original 1', $newSheet->getTitle());
38+
self::assertSame(
39+
'text1',
40+
$newSheet->getCell('A1')->getValue()
41+
);
42+
self::assertSame(
43+
'text2',
44+
$newSheet->getCell('A2')->getValue()
45+
);
46+
self::assertTrue(
47+
$newSheet->getStyle('A1')->getFont()->getBold()
48+
);
49+
self::assertFalse(
50+
$newSheet->getStyle('A2')->getFont()->getBold()
51+
);
52+
$sheetNames = [];
53+
foreach ($this->spreadsheet->getWorksheetIterator() as $worksheet) {
54+
$sheetNames[] = $worksheet->getTitle();
55+
}
56+
$expected = ['original', 'original 1', 'added', 'added 1'];
57+
self::assertSame($expected, $sheetNames);
58+
}
59+
}

0 commit comments

Comments
 (0)