Skip to content

Commit c223815

Browse files
committed
Resolve qax-os#345, get comments by target reference
1 parent 0072bb7 commit c223815

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

comment.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
3333
func (f *File) GetComments() (comments map[string][]Comment) {
3434
comments = map[string][]Comment{}
3535
for n := range f.sheetMap {
36-
commentID := f.GetSheetIndex(n)
37-
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
38-
c, ok := f.XLSX[commentsXML]
36+
c, ok := f.XLSX["xl"+strings.TrimPrefix(f.getSheetComments(f.GetSheetIndex(n)), "..")]
3937
if ok {
4038
d := xlsxComments{}
4139
xml.Unmarshal([]byte(c), &d)
@@ -58,6 +56,20 @@ func (f *File) GetComments() (comments map[string][]Comment) {
5856
return
5957
}
6058

59+
// getSheetComments provides the method to get the target comment reference by
60+
// given worksheet index.
61+
func (f *File) getSheetComments(sheetID int) string {
62+
var rels = "xl/worksheets/_rels/sheet" + strconv.Itoa(sheetID) + ".xml.rels"
63+
var sheetRels xlsxWorkbookRels
64+
_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels)
65+
for _, v := range sheetRels.Relationships {
66+
if v.Type == SourceRelationshipComments {
67+
return v.Target
68+
}
69+
}
70+
return ""
71+
}
72+
6173
// AddComment provides the method to add comment in a sheet by given worksheet
6274
// index, cell and format set (such as author and text). Note that the max
6375
// author length is 255 and the max text length is 32512. For example, add a

0 commit comments

Comments
 (0)