Skip to content

Commit d97f1a3

Browse files
Return error if adding comment on a cell which already exist comment (qax-os#2227)
Co-authored-by: felixdevelopper-hue <felixdevelopper@gmail.com>
1 parent 8325cf6 commit d97f1a3

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ func (err ErrSheetNotExist) Error() string {
193193
return fmt.Sprintf("sheet %s does not exist", err.SheetName)
194194
}
195195

196+
// newAddCommentError defined the error message on the comment already exist in
197+
// the cell.
198+
func newAddCommentError(cell string) error {
199+
return fmt.Errorf("comment already exist on cell %s", cell)
200+
}
201+
196202
// newCellNameToCoordinatesError defined the error message on converts
197203
// alphanumeric cell name to coordinates.
198204
func newCellNameToCoordinatesError(cell string, err error) error {

vml.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ func (f *File) getSheetComments(sheetFile string) string {
108108
// AddComment provides the method to add comments in a sheet by giving the
109109
// worksheet name, cell reference, and format set (such as author and text).
110110
// Note that the maximum author name length is 255 and the max text length is
111-
// 32512. For example, add a rich-text comment with a specified comments box
112-
// size in Sheet1!A5:
111+
// 32512, and each cell can only have one comment, an error will return if
112+
// adding comment on a cell which already exist comment. For example, add a
113+
// rich-text comment with a specified comments box size in Sheet1!A5:
113114
//
114115
// err := f.AddComment("Sheet1", excelize.Comment{
115116
// Cell: "A5",
@@ -169,10 +170,17 @@ func (f *File) DeleteComment(sheet, cell string) error {
169170
cmts.CommentList.Comment[:i],
170171
cmts.CommentList.Comment[i+1:]...,
171172
)
173+
if idx := inStrSlice(cmts.cells, cell, true); idx != -1 {
174+
cmts.cells = append(
175+
cmts.cells[:idx],
176+
cmts.cells[idx+1:]...,
177+
)
178+
}
172179
i--
173180
continue
174181
}
175182
cmts.CommentList.Comment = nil
183+
cmts.cells = nil
176184
}
177185
f.Comments[commentsXML] = cmts
178186
}
@@ -272,6 +280,9 @@ func (f *File) addComment(commentsXML string, opts vmlOptions) error {
272280
if cmts == nil {
273281
cmts = &xlsxComments{Authors: xlsxAuthor{Author: []string{opts.Author}}}
274282
}
283+
if inStrSlice(cmts.cells, opts.Comment.Cell, true) != -1 {
284+
return newAddCommentError(opts.Comment.Cell)
285+
}
275286
if inStrSlice(cmts.Authors.Author, opts.Author, true) == -1 {
276287
cmts.Authors.Author = append(cmts.Authors.Author, opts.Author)
277288
authorID = len(cmts.Authors.Author) - 1
@@ -320,6 +331,7 @@ func (f *File) addComment(commentsXML string, opts vmlOptions) error {
320331
cmt.Text.R = append(cmt.Text.R, r)
321332
}
322333
cmts.CommentList.Comment = append(cmts.CommentList.Comment, cmt)
334+
cmts.cells = append(cmts.cells, cmt.Ref)
323335
f.Comments[commentsXML] = cmts
324336
return err
325337
}
@@ -355,6 +367,12 @@ func (f *File) commentsReader(path string) (*xlsxComments, error) {
355367
}
356368
}
357369
}
370+
if cmts := f.Comments[path]; cmts != nil && len(cmts.cells) == 0 {
371+
for idx := 0; idx < len(cmts.CommentList.Comment); idx++ {
372+
cmts.cells = append(cmts.cells, cmts.CommentList.Comment[idx].Ref)
373+
}
374+
f.Comments[path] = cmts
375+
}
358376
return f.Comments[path], nil
359377
}
360378

vml_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func TestAddComment(t *testing.T) {
3232
assert.NoError(t, f.AddComment("Sheet1", Comment{Cell: "A30", Author: s, Text: s, Paragraph: []RichTextRun{{Text: s}, {Text: s}}}))
3333
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "B7", Author: "Excelize", Text: s[:TotalCellChars-1], Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}))
3434

35+
// Test add comment on a cell which already exists a comment
36+
assert.Equal(t, f.AddComment("Sheet2", Comment{Cell: "B7", Author: s, Text: s}), newAddCommentError("B7"))
3537
// Test add comment on not exists worksheet
3638
assert.EqualError(t, f.AddComment("SheetN", Comment{Cell: "B7", Author: "Excelize", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment."}}}), "sheet SheetN does not exist")
3739
// Test add comment on with illegal cell reference
@@ -90,15 +92,13 @@ func TestDeleteComment(t *testing.T) {
9092
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "A40", Text: "Excelize: This is a comment1."}))
9193
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "A41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment2."}}}))
9294
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment3."}}}))
93-
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment3-1."}}}))
9495
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C42", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment4."}}}))
95-
assert.NoError(t, f.AddComment("Sheet2", Comment{Cell: "C41", Paragraph: []RichTextRun{{Text: "Excelize: ", Font: &Font{Bold: true}}, {Text: "This is a comment2."}}}))
9696

9797
assert.NoError(t, f.DeleteComment("Sheet2", "A40"))
9898

9999
comments, err := f.GetComments("Sheet2")
100100
assert.NoError(t, err)
101-
assert.Len(t, comments, 5)
101+
assert.Len(t, comments, 3)
102102

103103
comments, err = NewFile().GetComments("Sheet1")
104104
assert.NoError(t, err)

xmlComments.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type xlsxComments struct {
2626
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main comments"`
2727
Authors xlsxAuthor `xml:"authors"`
2828
CommentList xlsxCommentList `xml:"commentList"`
29+
cells []string
2930
}
3031

3132
// xlsxAuthor directly maps the author element. This element holds a string

0 commit comments

Comments
 (0)