File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed
src/PhpSpreadsheet/Worksheet
tests/PhpSpreadsheetTests Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 55use PhpOffice \PhpSpreadsheet \Comment ;
66use PhpOffice \PhpSpreadsheet \RichText \RichText ;
77use PhpOffice \PhpSpreadsheet \RichText \TextElement ;
8+ use PhpOffice \PhpSpreadsheet \Spreadsheet ;
89use PhpOffice \PhpSpreadsheet \Style \Alignment ;
910use PhpOffice \PhpSpreadsheet \Style \Color ;
1011use 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}
You can’t perform that action at this time.
0 commit comments