Skip to content

Add Spreadsheet Method for Duplicating Worksheet #4315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

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

### Changed

Expand Down
27 changes: 23 additions & 4 deletions docs/topics/worksheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,38 @@ insert the clone into the workbook.

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

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

$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
$spreadsheet->addExternalSheet($clonedWorksheet);
```php
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
$clonedWorksheet->setTitle('Copy of Worksheet 1'); // must be unique
$spreadsheet1->addSheet($clonedWorksheet);
$spreadsheet->addExternalSheet($clonedWorksheet);
```
Starting with PhpSpreadsheet 3.8.0, this can be simplified:
```php
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Worksheet 1');
$spreadsheet1->addSheet($clonedWorksheet, null, true);
$spreadsheet->addExternalSheet($clonedWorksheet);
```
Starting with PhpSpreadsheet 3.9.0, this can be simplified even further:
```php
$clonedWorksheet = $spreadsheet1->duplicateWorksheetByTitle('sheetname');
$spreadsheet->addExternalSheet($clonedWorksheet);
```

In both cases, it is the developer's responsibility to ensure that
In the cases commented "must be unique", it is the developer's responsibility to ensure that
worksheet names are not duplicated. PhpSpreadsheet will throw an
exception if you attempt to copy worksheets that will result in a
duplicate name.
Expand Down
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Spreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@ public function sheetNameExists(string $worksheetName): bool
return $this->getSheetByName($worksheetName) !== null;
}

public function duplicateWorksheetByTitle(string $title): Worksheet
{
$original = $this->getSheetByNameOrThrow($title);
$index = $this->getIndex($original) + 1;
$clone = clone $original;

return $this->addSheet($clone, $index, true);
}

/**
* Add sheet.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/PhpSpreadsheetTests/SpreadsheetDuplicateSheetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class SpreadsheetDuplicateSheetTest extends TestCase
{
private ?Spreadsheet $spreadsheet = null;

protected function tearDown(): void
{
if ($this->spreadsheet !== null) {
$this->spreadsheet->disconnectWorksheets();
$this->spreadsheet = null;
}
}

public function testDuplicate(): void
{
$this->spreadsheet = new Spreadsheet();
$sheet = $this->spreadsheet->getActiveSheet();
$sheet->setTitle('original');
$sheet->getCell('A1')->setValue('text1');
$sheet->getCell('A2')->setValue('text2');
$sheet->getStyle('A1')
->getFont()
->setBold(true);
$sheet3 = $this->spreadsheet->createSheet();
$sheet3->setTitle('added');
$newSheet = $this->spreadsheet
->duplicateWorksheetByTitle('original');
$this->spreadsheet->duplicateWorksheetByTitle('added');
self::assertSame('original 1', $newSheet->getTitle());
self::assertSame(
'text1',
$newSheet->getCell('A1')->getValue()
);
self::assertSame(
'text2',
$newSheet->getCell('A2')->getValue()
);
self::assertTrue(
$newSheet->getStyle('A1')->getFont()->getBold()
);
self::assertFalse(
$newSheet->getStyle('A2')->getFont()->getBold()
);
$sheetNames = [];
foreach ($this->spreadsheet->getWorksheetIterator() as $worksheet) {
$sheetNames[] = $worksheet->getTitle();
}
$expected = ['original', 'original 1', 'added', 'added 1'];
self::assertSame($expected, $sheetNames);
}
}
Loading