diff options
Diffstat (limited to 'comment.go')
-rw-r--r-- | comment.go | 85 |
1 files changed, 56 insertions, 29 deletions
@@ -1,18 +1,22 @@ -// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of +// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of // this source code is governed by a BSD-style license that can be found in // the LICENSE file. // // Package excelize providing a set of functions that allow you to write to // and read from XLSX files. Support reads and writes XLSX file generated by // Microsoft Excelâ„¢ 2007 and later. Support save file without losing original -// charts of XLSX. This library needs Go version 1.8 or later. +// charts of XLSX. This library needs Go version 1.10 or later. package excelize import ( + "bytes" "encoding/json" "encoding/xml" "fmt" + "io" + "log" + "path/filepath" "strconv" "strings" ) @@ -32,8 +36,8 @@ func parseFormatCommentsSet(formatSet string) (*formatComment, error) { // the worksheet comments. func (f *File) GetComments() (comments map[string][]Comment) { comments = map[string][]Comment{} - for n := range f.sheetMap { - if d := f.commentsReader("xl" + strings.TrimPrefix(f.getSheetComments(f.GetSheetIndex(n)), "..")); d != nil { + for n, path := range f.sheetMap { + if d := f.commentsReader("xl" + strings.TrimPrefix(f.getSheetComments(filepath.Base(path)), "..")); d != nil { sheetComments := []Comment{} for _, comment := range d.CommentList.Comment { sheetComment := Comment{} @@ -42,8 +46,13 @@ func (f *File) GetComments() (comments map[string][]Comment) { } sheetComment.Ref = comment.Ref sheetComment.AuthorID = comment.AuthorID + if comment.Text.T != nil { + sheetComment.Text += *comment.Text.T + } for _, text := range comment.Text.R { - sheetComment.Text += text.T + if text.T != nil { + sheetComment.Text += text.T.Val + } } sheetComments = append(sheetComments, sheetComment) } @@ -54,10 +63,10 @@ func (f *File) GetComments() (comments map[string][]Comment) { } // 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" - if sheetRels := f.workSheetRelsReader(rels); sheetRels != nil { +// given worksheet file path. +func (f *File) getSheetComments(sheetFile string) string { + var rels = "xl/worksheets/_rels/" + sheetFile + ".rels" + if sheetRels := f.relsReader(rels); sheetRels != nil { for _, v := range sheetRels.Relationships { if v.Type == SourceRelationshipComments { return v.Target @@ -95,12 +104,12 @@ func (f *File) AddComment(sheet, cell, format string) error { drawingVML = strings.Replace(sheetRelationshipsDrawingVML, "..", "xl", -1) } else { // Add first comment for given sheet. - rID := f.addSheetRelationships(sheet, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "") - f.addSheetRelationships(sheet, SourceRelationshipComments, sheetRelationshipsComments, "") + sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels" + rID := f.addRels(sheetRels, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "") + f.addRels(sheetRels, SourceRelationshipComments, sheetRelationshipsComments, "") f.addSheetLegacyDrawing(sheet, rID) } commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml" - f.addComment(commentsXML, cell, formatSet) var colCount int for i, l := range strings.Split(formatSet.Text, "\n") { if ll := len(l); ll > colCount { @@ -114,6 +123,7 @@ func (f *File) AddComment(sheet, cell, format string) error { if err != nil { return err } + f.addComment(commentsXML, cell, formatSet) f.addContentTypePart(commentID, "comments") return err } @@ -239,6 +249,7 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { }, } } + defaultFont := f.GetDefaultFont() cmt := xlsxComment{ Ref: cell, AuthorID: 0, @@ -247,25 +258,25 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { { RPr: &xlsxRPr{ B: " ", - Sz: &attrValFloat{Val: 9}, + Sz: &attrValFloat{Val: float64Ptr(9)}, Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, - Family: &attrValInt{Val: 2}, + RFont: &attrValString{Val: stringPtr(defaultFont)}, + Family: &attrValInt{Val: intPtr(2)}, }, - T: a, + T: &xlsxT{Val: a}, }, { RPr: &xlsxRPr{ - Sz: &attrValFloat{Val: 9}, + Sz: &attrValFloat{Val: float64Ptr(9)}, Color: &xlsxColor{ Indexed: 81, }, - RFont: &attrValString{Val: "Calibri"}, - Family: &attrValInt{Val: 2}, + RFont: &attrValString{Val: stringPtr(defaultFont)}, + Family: &attrValInt{Val: intPtr(2)}, }, - T: t, + T: &xlsxT{Val: t}, }, }, }, @@ -277,24 +288,36 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) { // countComments provides a function to get comments files count storage in // the folder xl. func (f *File) countComments() int { - count := 0 + c1, c2 := 0, 0 for k := range f.XLSX { if strings.Contains(k, "xl/comments") { - count++ + c1++ + } + } + for rel := range f.Comments { + if strings.Contains(rel, "xl/comments") { + c2++ } } - return count + if c1 < c2 { + return c2 + } + return c1 } // decodeVMLDrawingReader provides a function to get the pointer to the // structure after deserialization of xl/drawings/vmlDrawing%d.xml. func (f *File) decodeVMLDrawingReader(path string) *decodeVmlDrawing { + var err error + if f.DecodeVMLDrawing[path] == nil { c, ok := f.XLSX[path] if ok { - d := decodeVmlDrawing{} - _ = xml.Unmarshal(namespaceStrictToTransitional(c), &d) - f.DecodeVMLDrawing[path] = &d + f.DecodeVMLDrawing[path] = new(decodeVmlDrawing) + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(c))). + Decode(f.DecodeVMLDrawing[path]); err != nil && err != io.EOF { + log.Printf("xml decode error: %s", err) + } } } return f.DecodeVMLDrawing[path] @@ -314,12 +337,16 @@ func (f *File) vmlDrawingWriter() { // commentsReader provides a function to get the pointer to the structure // after deserialization of xl/comments%d.xml. func (f *File) commentsReader(path string) *xlsxComments { + var err error + if f.Comments[path] == nil { content, ok := f.XLSX[path] if ok { - c := xlsxComments{} - _ = xml.Unmarshal(namespaceStrictToTransitional(content), &c) - f.Comments[path] = &c + f.Comments[path] = new(xlsxComments) + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(content))). + Decode(f.Comments[path]); err != nil && err != io.EOF { + log.Printf("xml decode error: %s", err) + } } } return f.Comments[path] |