Skip to content

Commit 3ce4b91

Browse files
committed
Resolve qax-os#345, fix missing comments by GetComments
1 parent 3f89c6e commit 3ce4b91

File tree

7 files changed

+51
-26
lines changed

7 files changed

+51
-26
lines changed

chart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
789789
drawingID := f.countDrawings() + 1
790790
chartID := f.countCharts() + 1
791791
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
792-
drawingID, drawingXML = f.prepareChartSheetDrawing(&cs, drawingID, sheet, drawingXML)
792+
f.prepareChartSheetDrawing(&cs, drawingID, sheet)
793793
drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
794794
drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
795795
f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)

codelingo.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

comment.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"fmt"
1717
"io"
1818
"log"
19+
"path/filepath"
1920
"strconv"
2021
"strings"
2122
)
@@ -35,8 +36,8 @@ func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
3536
// the worksheet comments.
3637
func (f *File) GetComments() (comments map[string][]Comment) {
3738
comments = map[string][]Comment{}
38-
for n := range f.sheetMap {
39-
if d := f.commentsReader("xl" + strings.TrimPrefix(f.getSheetComments(f.GetSheetIndex(n)), "..")); d != nil {
39+
for n, path := range f.sheetMap {
40+
if d := f.commentsReader("xl" + strings.TrimPrefix(f.getSheetComments(filepath.Base(path)), "..")); d != nil {
4041
sheetComments := []Comment{}
4142
for _, comment := range d.CommentList.Comment {
4243
sheetComment := Comment{}
@@ -60,9 +61,9 @@ func (f *File) GetComments() (comments map[string][]Comment) {
6061
}
6162

6263
// getSheetComments provides the method to get the target comment reference by
63-
// given worksheet index.
64-
func (f *File) getSheetComments(sheetID int) string {
65-
var rels = "xl/worksheets/_rels/sheet" + strconv.Itoa(sheetID) + ".xml.rels"
64+
// given worksheet file path.
65+
func (f *File) getSheetComments(sheetFile string) string {
66+
var rels = "xl/worksheets/_rels/" + sheetFile + ".rels"
6667
if sheetRels := f.relsReader(rels); sheetRels != nil {
6768
for _, v := range sheetRels.Relationships {
6869
if v.Type == SourceRelationshipComments {
@@ -107,7 +108,6 @@ func (f *File) AddComment(sheet, cell, format string) error {
107108
f.addSheetLegacyDrawing(sheet, rID)
108109
}
109110
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
110-
f.addComment(commentsXML, cell, formatSet)
111111
var colCount int
112112
for i, l := range strings.Split(formatSet.Text, "\n") {
113113
if ll := len(l); ll > colCount {
@@ -121,6 +121,7 @@ func (f *File) AddComment(sheet, cell, format string) error {
121121
if err != nil {
122122
return err
123123
}
124+
f.addComment(commentsXML, cell, formatSet)
124125
f.addContentTypePart(commentID, "comments")
125126
return err
126127
}

comment_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ func TestAddComments(t *testing.T) {
2929

3030
// Test add comment on not exists worksheet.
3131
assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN is not exist")
32-
32+
// Test add comment on with illegal cell coordinates
33+
assert.EqualError(t, f.AddComment("Sheet1", "A", `{"author":"Excelize: ","text":"This is a comment."}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
3334
if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) {
3435
assert.Len(t, f.GetComments(), 2)
3536
}
37+
38+
f.Comments["xl/comments2.xml"] = nil
39+
f.XLSX["xl/comments2.xml"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><authors><author>Excelize: </author></authors><commentList><comment ref="B7" authorId="0"><text><t>Excelize: </t></text></comment></commentList></comments>`)
40+
comments := f.GetComments()
41+
assert.EqualValues(t, 2, len(comments["Sheet1"]))
42+
assert.EqualValues(t, 1, len(comments["Sheet2"]))
3643
}
3744

3845
func TestDecodeVMLDrawingReader(t *testing.T) {

drawing.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,15 @@ func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawing
4040

4141
// prepareChartSheetDrawing provides a function to prepare drawing ID and XML
4242
// by given drawingID, worksheet name and default drawingXML.
43-
func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, sheet, drawingXML string) (int, string) {
43+
func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, sheet string) {
4444
sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
45-
if xlsx.Drawing != nil {
46-
// The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml.
47-
sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
48-
drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml"))
49-
drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1)
50-
} else {
51-
// Add first picture for given sheet.
52-
sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels"
53-
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
54-
xlsx.Drawing = &xlsxDrawing{
55-
RID: "rId" + strconv.Itoa(rID),
56-
}
45+
// Only allow one chart in a chartsheet.
46+
sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels"
47+
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
48+
xlsx.Drawing = &xlsxDrawing{
49+
RID: "rId" + strconv.Itoa(rID),
5750
}
58-
return drawingID, drawingXML
51+
return
5952
}
6053

6154
// addChart provides a function to create chart as xl/charts/chart%d.xml by

drawing_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
2+
// this source code is governed by a BSD-style license that can be found in
3+
// the LICENSE file.
4+
//
5+
// Package excelize providing a set of functions that allow you to write to
6+
// and read from XLSX files. Support reads and writes XLSX file generated by
7+
// Microsoft Excel™ 2007 and later. Support save file without losing original
8+
// charts of XLSX. This library needs Go version 1.10 or later.
9+
10+
package excelize
11+
12+
import (
13+
"testing"
14+
)
15+
16+
func TestDrawingParser(t *testing.T) {
17+
f := File{
18+
Drawings: make(map[string]*xlsxWsDr),
19+
XLSX: map[string][]byte{
20+
"charset": MacintoshCyrillicCharset,
21+
"wsDr": []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"><xdr:oneCellAnchor><xdr:graphicFrame/></xdr:oneCellAnchor></xdr:wsDr>`)},
22+
}
23+
// Test with one cell anchor
24+
f.drawingParser("wsDr")
25+
// Test with unsupport charset
26+
f.drawingParser("charset")
27+
}

excelize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ func TestCopySheetError(t *testing.T) {
934934

935935
func TestGetSheetComments(t *testing.T) {
936936
f := NewFile()
937-
assert.Equal(t, "", f.getSheetComments(0))
937+
assert.Equal(t, "", f.getSheetComments("sheet0"))
938938
}
939939

940940
func TestSetActiveSheet(t *testing.T) {

0 commit comments

Comments
 (0)