Resolve #345, get comments by target reference

This commit is contained in:
xuri 2019-02-23 16:20:44 +08:00
parent 0072bb7310
commit c223815cfe
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
1 changed files with 15 additions and 3 deletions

View File

@ -33,9 +33,7 @@ func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
func (f *File) GetComments() (comments map[string][]Comment) { func (f *File) GetComments() (comments map[string][]Comment) {
comments = map[string][]Comment{} comments = map[string][]Comment{}
for n := range f.sheetMap { for n := range f.sheetMap {
commentID := f.GetSheetIndex(n) c, ok := f.XLSX["xl"+strings.TrimPrefix(f.getSheetComments(f.GetSheetIndex(n)), "..")]
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
c, ok := f.XLSX[commentsXML]
if ok { if ok {
d := xlsxComments{} d := xlsxComments{}
xml.Unmarshal([]byte(c), &d) xml.Unmarshal([]byte(c), &d)
@ -58,6 +56,20 @@ func (f *File) GetComments() (comments map[string][]Comment) {
return return
} }
// getSheetComments provides the method to get the target comment reference by
// given worksheet index.
func (f *File) getSheetComments(sheetID int) string {
var rels = "xl/worksheets/_rels/sheet" + strconv.Itoa(sheetID) + ".xml.rels"
var sheetRels xlsxWorkbookRels
_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(rels)), &sheetRels)
for _, v := range sheetRels.Relationships {
if v.Type == SourceRelationshipComments {
return v.Target
}
}
return ""
}
// AddComment provides the method to add comment in a sheet by given worksheet // AddComment provides the method to add comment in a sheet by given worksheet
// index, cell and format set (such as author and text). Note that the max // index, cell and format set (such as author and text). Note that the max
// author length is 255 and the max text length is 32512. For example, add a // author length is 255 and the max text length is 32512. For example, add a