From 76f336809f5419343702de5b3284d46feb9ed266 Mon Sep 17 00:00:00 2001 From: NaturalGao <43291304+NaturalGao@users.noreply.github.com> Date: Fri, 19 Aug 2022 23:24:13 +0800 Subject: 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 --- comment.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'comment.go') diff --git a/comment.go b/comment.go index 82d1f88..ac22ec7 100644 --- a/comment.go +++ b/comment.go @@ -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 { -- cgit v1.2.1