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).
9
9
10
10
### Added
11
11
12
+ - Added ` removeComment() ` method for Worksheet [ PR #2875 ] ( https://github.com/PHPOffice/PhpSpreadsheet/pull/2875/files )
12
13
- 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 )
13
14
- Basic support for Xlsx reading/writing Chart Sheets [ PR #2830 ] ( https://github.com/PHPOffice/PhpSpreadsheet/pull/2830 )
14
15
Original file line number Diff line number Diff line change @@ -2600,6 +2600,33 @@ public function setComments(array $comments)
2600
2600
return $ this ;
2601
2601
}
2602
2602
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
+
2603
2630
/**
2604
2631
* Get comment for cell.
2605
2632
*
Original file line number Diff line number Diff line change 5
5
use PhpOffice \PhpSpreadsheet \Comment ;
6
6
use PhpOffice \PhpSpreadsheet \RichText \RichText ;
7
7
use PhpOffice \PhpSpreadsheet \RichText \TextElement ;
8
+ use PhpOffice \PhpSpreadsheet \Spreadsheet ;
8
9
use PhpOffice \PhpSpreadsheet \Style \Alignment ;
9
10
use PhpOffice \PhpSpreadsheet \Style \Color ;
10
11
use PHPUnit \Framework \TestCase ;
@@ -83,4 +84,14 @@ public function testSetText(): void
83
84
$ comment ->setText ($ test );
84
85
self ::assertEquals ('This is a test comment ' , (string ) $ comment );
85
86
}
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
+ }
86
97
}
You can’t perform that action at this time.
0 commit comments