diff options
author | NaturalGao <43291304+NaturalGao@users.noreply.github.com> | 2022-08-19 23:24:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-19 23:24:13 +0800 |
commit | 76f336809f5419343702de5b3284d46feb9ed266 (patch) | |
tree | 207546e0abb538b3fb6d646b046b4c919e4c5711 /comment.go | |
parent | d1e76fc432ac5c9bde99591ec5e88e46b62d9c3d (diff) |
This closes #849, add new function `DeleteComment` for delete comment (#1317)
- Update unit tests for the delete comment
- Add 3 errors function for error messages
Diffstat (limited to 'comment.go')
-rw-r--r-- | comment.go | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -140,6 +140,39 @@ func (f *File) AddComment(sheet, cell, format string) error { return err } +// DeleteComment provides the method to delete comment in a sheet by given +// worksheet. For example, delete the comment in Sheet1!$A$30: +// +// err := f.DeleteComment("Sheet1", "A30") +func (f *File) DeleteComment(sheet, cell string) (err error) { + sheetXMLPath, ok := f.getSheetXMLPath(sheet) + if !ok { + err = newNoExistSheetError(sheet) + return + } + commentsXML := f.getSheetComments(filepath.Base(sheetXMLPath)) + if !strings.HasPrefix(commentsXML, "/") { + commentsXML = "xl" + strings.TrimPrefix(commentsXML, "..") + } + commentsXML = strings.TrimPrefix(commentsXML, "/") + if comments := f.commentsReader(commentsXML); comments != nil { + for i, cmt := range comments.CommentList.Comment { + if cmt.Ref == cell { + if len(comments.CommentList.Comment) > 1 { + comments.CommentList.Comment = append( + comments.CommentList.Comment[:i], + comments.CommentList.Comment[i+1:]..., + ) + continue + } + comments.CommentList.Comment = nil + } + } + f.Comments[commentsXML] = comments + } + return +} + // addDrawingVML provides a function to create comment as // xl/drawings/vmlDrawing%d.vml by given commit ID and cell. func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount, colCount int) error { |