Skip to content

Commit ed34a45

Browse files
authored
Merge pull request #2875 from dgeppo/master
Add removeCommentByColumnAndRow function
2 parents 7b7d3bc + c749cb5 commit ed34a45

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- Added `removeComment()` method for Worksheet [PR #2875](https://github.com/PHPOffice/PhpSpreadsheet/pull/2875/files)
1213
- Add point size option for scatter charts [Issue #2298](https://github.com/PHPOffice/PhpSpreadsheet/issues/2298) [PR #2801](https://github.com/PHPOffice/PhpSpreadsheet/pull/2801)
1314
- Basic support for Xlsx reading/writing Chart Sheets [PR #2830](https://github.com/PHPOffice/PhpSpreadsheet/pull/2830)
1415

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,33 @@ public function setComments(array $comments)
26002600
return $this;
26012601
}
26022602

2603+
/**
2604+
* Remove comment from cell.
2605+
*
2606+
* @param array<int>|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5';
2607+
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
2608+
*
2609+
* @return $this
2610+
*/
2611+
public function removeComment($cellCoordinate)
2612+
{
2613+
$cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate));
2614+
2615+
if (Coordinate::coordinateIsRange($cellAddress)) {
2616+
throw new Exception('Cell coordinate string can not be a range of cells.');
2617+
} elseif (strpos($cellAddress, '$') !== false) {
2618+
throw new Exception('Cell coordinate string must not be absolute.');
2619+
} elseif ($cellAddress == '') {
2620+
throw new Exception('Cell coordinate can not be zero-length string.');
2621+
}
2622+
// Check if we have a comment for this cell and delete it
2623+
if (isset($this->comments[$cellAddress])) {
2624+
unset($this->comments[$cellAddress]);
2625+
}
2626+
2627+
return $this;
2628+
}
2629+
26032630
/**
26042631
* Get comment for cell.
26052632
*

tests/PhpSpreadsheetTests/CommentTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpOffice\PhpSpreadsheet\Comment;
66
use PhpOffice\PhpSpreadsheet\RichText\RichText;
77
use PhpOffice\PhpSpreadsheet\RichText\TextElement;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
89
use PhpOffice\PhpSpreadsheet\Style\Alignment;
910
use PhpOffice\PhpSpreadsheet\Style\Color;
1011
use PHPUnit\Framework\TestCase;
@@ -83,4 +84,14 @@ public function testSetText(): void
8384
$comment->setText($test);
8485
self::assertEquals('This is a test comment', (string) $comment);
8586
}
87+
88+
public function testRemoveComment(): void
89+
{
90+
$spreadsheet = new Spreadsheet();
91+
$sheet = $spreadsheet->getActiveSheet();
92+
$sheet->getComment('A2')->getText()->createText('Comment to delete');
93+
self::assertArrayHasKey('A2', $sheet->getComments());
94+
$sheet->removeComment('A2');
95+
self::assertEmpty($sheet->getComments());
96+
}
8697
}

0 commit comments

Comments
 (0)