Skip to content

Commit 7c1a65e

Browse files
committed
Add Spreadsheet Method for Duplicating Worksheet
Cloning a worksheet attached to a spreadsheet creates a clone which is detached from the spreadsheet. This can have its uses, but I think it would also be useful to have the ability to duplicate the worksheet and keep the duplicate attached to the spreadsheet. You can do that in Excel and LibreOffice, and you can now do it in PhpSpreadsheet as well. The duplicated worksheet will come immediately after its source. The worksheet being duplicated could be identified in a number of ways - by passing the worksheet itself to the new method, by passing the worksheet title, or by passing the index of the worksheet within the spreadsheet. For now, I am just implementing the one I think is most useful (title).
1 parent 949c799 commit 7c1a65e

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

docs/topics/worksheets.md

Lines changed: 23 additions & 4 deletions
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

Lines changed: 9 additions & 0 deletions
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
*
Lines changed: 59 additions & 0 deletions
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)