From 391e6308d7956d3c76a10aa5ae5a02879de795df Mon Sep 17 00:00:00 2001 From: dgeppo <107109388+dgeppo@users.noreply.github.com> Date: Wed, 8 Jun 2022 11:24:56 +0200 Subject: [PATCH 1/9] Add removeCommentByColumnAndRow function --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 4a441a9327..d7288aa4af 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2581,6 +2581,22 @@ public function setComments(array $comments) return $this; } + + + /** + * Remove comment. + * + * @param int $columnIndex Numeric column coordinate of the cell + * @param int $row Numeric row coordinate of the cell + * + */ + public function removeCommentByColumnAndRow($columnIndex, $row) + { + $pCellCoordinate = strtoupper(Coordinate::stringFromColumnIndex($columnIndex) . $row); + if(isset($this->comments[$pCellCoordinate])) { + unset($this->comments[$pCellCoordinate]); + } + } /** * Get comment for cell. From 13437384afd58acd64833cbd28fdf13070f82d2d Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:08:02 +0200 Subject: [PATCH 2/9] Change removeComment to use CellCoordinate --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index d7288aa4af..14d2426505 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2581,22 +2581,31 @@ public function setComments(array $comments) return $this; } - /** - * Remove comment. + * Remove comment from cell. * - * @param int $columnIndex Numeric column coordinate of the cell - * @param int $row Numeric row coordinate of the cell + * @param array|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; + * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. * + * @return Comment */ - public function removeCommentByColumnAndRow($columnIndex, $row) + public function removeComment($cellCoordinate) { - $pCellCoordinate = strtoupper(Coordinate::stringFromColumnIndex($columnIndex) . $row); - if(isset($this->comments[$pCellCoordinate])) { - unset($this->comments[$pCellCoordinate]); + $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); + + if (Coordinate::coordinateIsRange($cellAddress)) { + throw new Exception('Cell coordinate string can not be a range of cells.'); + } elseif (strpos($cellAddress, '$') !== false) { + throw new Exception('Cell coordinate string must not be absolute.'); + } elseif ($cellAddress == '') { + throw new Exception('Cell coordinate can not be zero-length string.'); + } + // Check if we have a comment for this cell and delete it + if (isset($this->comments[$cellAddress])) { + unset($this->comments[$cellAddress]); } - } + } /** * Get comment for cell. From 56ddbc4dd50533ebed3d2a224385f0a1ba80c0a2 Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:08:52 +0200 Subject: [PATCH 3/9] Add testRemoveComment --- tests/PhpSpreadsheetTests/CommentTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/PhpSpreadsheetTests/CommentTest.php b/tests/PhpSpreadsheetTests/CommentTest.php index e58afad44b..1d7825f4f6 100644 --- a/tests/PhpSpreadsheetTests/CommentTest.php +++ b/tests/PhpSpreadsheetTests/CommentTest.php @@ -83,4 +83,12 @@ public function testSetText(): void $comment->setText($test); self::assertEquals('This is a test comment', (string) $comment); } + + public function testRemoveComment(): void { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getComment('A2')->getText()->createText('Comment to delete'); + $sheet->removeComment('A2'); + self::assertEmpty($sheet->getComments()); + } } From 52da0dc0fcc846a2bd53137f298a2f646b692b77 Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:18:48 +0200 Subject: [PATCH 4/9] Add use PhpOffice\PhpSpreadsheet\Spreadsheet; --- tests/PhpSpreadsheetTests/CommentTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PhpSpreadsheetTests/CommentTest.php b/tests/PhpSpreadsheetTests/CommentTest.php index 1d7825f4f6..7d06162a5e 100644 --- a/tests/PhpSpreadsheetTests/CommentTest.php +++ b/tests/PhpSpreadsheetTests/CommentTest.php @@ -5,6 +5,7 @@ use PhpOffice\PhpSpreadsheet\Comment; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\RichText\TextElement; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Color; use PHPUnit\Framework\TestCase; From 801f5296e9fb2e4d8a30effc58abc0250831818a Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:21:18 +0200 Subject: [PATCH 5/9] Update Worksheet.php --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 14d2426505..b3a0bf2a40 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2590,7 +2590,7 @@ public function setComments(array $comments) * * @return Comment */ - public function removeComment($cellCoordinate) + public function removeComment($cellCoordinate):void { $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); From 861b955b3b39cee6b7f4a373d8d6359b56f3dd84 Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Tue, 14 Jun 2022 10:12:08 +0200 Subject: [PATCH 6/9] Add feature removeComment Use $cellCoordinate as argument Return $this to support the fluent interface --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index b3a0bf2a40..b9a168c204 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2588,9 +2588,9 @@ public function setComments(array $comments) * @param array|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. * - * @return Comment + * @return $this */ - public function removeComment($cellCoordinate):void + public function removeComment($cellCoordinate) { $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); @@ -2605,6 +2605,8 @@ public function removeComment($cellCoordinate):void if (isset($this->comments[$cellAddress])) { unset($this->comments[$cellAddress]); } + + return $this; } /** From 7f62fba7ef95891967a99e745e71bfac689a2fc4 Mon Sep 17 00:00:00 2001 From: Dams <107109388+dgeppo@users.noreply.github.com> Date: Tue, 14 Jun 2022 10:44:42 +0200 Subject: [PATCH 7/9] Update the method testRemoveComment Adding test to check if comment exists before delete it --- tests/PhpSpreadsheetTests/CommentTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PhpSpreadsheetTests/CommentTest.php b/tests/PhpSpreadsheetTests/CommentTest.php index 7d06162a5e..e55fe9cc85 100644 --- a/tests/PhpSpreadsheetTests/CommentTest.php +++ b/tests/PhpSpreadsheetTests/CommentTest.php @@ -89,6 +89,7 @@ public function testRemoveComment(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getComment('A2')->getText()->createText('Comment to delete'); + self::assertArrayHasKey('A2',$sheet->getComments()); $sheet->removeComment('A2'); self::assertEmpty($sheet->getComments()); } From 4d2b00dafc9d0015084354e9a064d4a1515ed88f Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 14 Jun 2022 12:19:54 +0200 Subject: [PATCH 8/9] phpcs fxes --- src/PhpSpreadsheet/Worksheet/Worksheet.php | 2 +- tests/PhpSpreadsheetTests/CommentTest.php | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index b9a168c204..c823699397 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2605,7 +2605,7 @@ public function removeComment($cellCoordinate) if (isset($this->comments[$cellAddress])) { unset($this->comments[$cellAddress]); } - + return $this; } diff --git a/tests/PhpSpreadsheetTests/CommentTest.php b/tests/PhpSpreadsheetTests/CommentTest.php index e55fe9cc85..aacd538fbb 100644 --- a/tests/PhpSpreadsheetTests/CommentTest.php +++ b/tests/PhpSpreadsheetTests/CommentTest.php @@ -84,13 +84,14 @@ public function testSetText(): void $comment->setText($test); self::assertEquals('This is a test comment', (string) $comment); } - - public function testRemoveComment(): void { + + public function testRemoveComment(): void + { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->getComment('A2')->getText()->createText('Comment to delete'); - self::assertArrayHasKey('A2',$sheet->getComments()); + self::assertArrayHasKey('A2', $sheet->getComments()); $sheet->removeComment('A2'); self::assertEmpty($sheet->getComments()); - } + } } From 8e31dbaabe2c69aefa6d7cee4db14f56e4688209 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Tue, 14 Jun 2022 12:21:44 +0200 Subject: [PATCH 9/9] Update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 290474a1c4..2c65f4ae2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Added +- Added `removeComment()` method for Worksheet [PR #2875](https://github.com/PHPOffice/PhpSpreadsheet/pull/2875/files) - 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) - Basic support for Xlsx reading/writing Chart Sheets [PR #2830](https://github.com/PHPOffice/PhpSpreadsheet/pull/2830)