From 3ce4b91be96589847823b6c1b6c123ba7880310f Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 31 Mar 2020 00:02:00 +0800 Subject: [PATCH] Resolve #345, fix missing comments by GetComments --- chart.go | 2 +- codelingo.yaml | 3 --- comment.go | 13 +++++++------ comment_test.go | 9 ++++++++- drawing.go | 21 +++++++-------------- drawing_test.go | 27 +++++++++++++++++++++++++++ excelize_test.go | 2 +- 7 files changed, 51 insertions(+), 26 deletions(-) delete mode 100644 codelingo.yaml create mode 100644 drawing_test.go diff --git a/chart.go b/chart.go index cae833d2..2c802ee8 100644 --- a/chart.go +++ b/chart.go @@ -789,7 +789,7 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error { drawingID := f.countDrawings() + 1 chartID := f.countCharts() + 1 drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml" - drawingID, drawingXML = f.prepareChartSheetDrawing(&cs, drawingID, sheet, drawingXML) + f.prepareChartSheetDrawing(&cs, drawingID, sheet) drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels" drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "") f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format) diff --git a/codelingo.yaml b/codelingo.yaml deleted file mode 100644 index dfe344b4..00000000 --- a/codelingo.yaml +++ /dev/null @@ -1,3 +0,0 @@ -tenets: - - import: codelingo/effective-go - - import: codelingo/code-review-comments diff --git a/comment.go b/comment.go index a5b6085f..610eae86 100644 --- a/comment.go +++ b/comment.go @@ -16,6 +16,7 @@ import ( "fmt" "io" "log" + "path/filepath" "strconv" "strings" ) @@ -35,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{} @@ -60,9 +61,9 @@ 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" +// 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 { @@ -107,7 +108,6 @@ func (f *File) AddComment(sheet, cell, format string) error { 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 { @@ -121,6 +121,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 } diff --git a/comment_test.go b/comment_test.go index 5b83162c..955d4e87 100644 --- a/comment_test.go +++ b/comment_test.go @@ -29,10 +29,17 @@ func TestAddComments(t *testing.T) { // Test add comment on not exists worksheet. assert.EqualError(t, f.AddComment("SheetN", "B7", `{"author":"Excelize: ","text":"This is a comment."}`), "sheet SheetN is not exist") - + // Test add comment on with illegal cell coordinates + assert.EqualError(t, f.AddComment("Sheet1", "A", `{"author":"Excelize: ","text":"This is a comment."}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`) if assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddComments.xlsx"))) { assert.Len(t, f.GetComments(), 2) } + + f.Comments["xl/comments2.xml"] = nil + f.XLSX["xl/comments2.xml"] = []byte(`Excelize: Excelize: `) + comments := f.GetComments() + assert.EqualValues(t, 2, len(comments["Sheet1"])) + assert.EqualValues(t, 1, len(comments["Sheet2"])) } func TestDecodeVMLDrawingReader(t *testing.T) { diff --git a/drawing.go b/drawing.go index b291d986..13bdab45 100644 --- a/drawing.go +++ b/drawing.go @@ -40,22 +40,15 @@ func (f *File) prepareDrawing(xlsx *xlsxWorksheet, drawingID int, sheet, drawing // prepareChartSheetDrawing provides a function to prepare drawing ID and XML // by given drawingID, worksheet name and default drawingXML. -func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, sheet, drawingXML string) (int, string) { +func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, sheet string) { sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml" - if xlsx.Drawing != nil { - // The worksheet already has a picture or chart relationships, use the relationships drawing ../drawings/drawing%d.xml. - sheetRelationshipsDrawingXML = f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID) - drawingID, _ = strconv.Atoi(strings.TrimSuffix(strings.TrimPrefix(sheetRelationshipsDrawingXML, "../drawings/drawing"), ".xml")) - drawingXML = strings.Replace(sheetRelationshipsDrawingXML, "..", "xl", -1) - } else { - // Add first picture for given sheet. - sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels" - rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "") - xlsx.Drawing = &xlsxDrawing{ - RID: "rId" + strconv.Itoa(rID), - } + // Only allow one chart in a chartsheet. + sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels" + rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "") + xlsx.Drawing = &xlsxDrawing{ + RID: "rId" + strconv.Itoa(rID), } - return drawingID, drawingXML + return } // addChart provides a function to create chart as xl/charts/chart%d.xml by diff --git a/drawing_test.go b/drawing_test.go new file mode 100644 index 00000000..0a380eda --- /dev/null +++ b/drawing_test.go @@ -0,0 +1,27 @@ +// 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.10 or later. + +package excelize + +import ( + "testing" +) + +func TestDrawingParser(t *testing.T) { + f := File{ + Drawings: make(map[string]*xlsxWsDr), + XLSX: map[string][]byte{ + "charset": MacintoshCyrillicCharset, + "wsDr": []byte(``)}, + } + // Test with one cell anchor + f.drawingParser("wsDr") + // Test with unsupport charset + f.drawingParser("charset") +} diff --git a/excelize_test.go b/excelize_test.go index 815a08d8..e8d3a307 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -934,7 +934,7 @@ func TestCopySheetError(t *testing.T) { func TestGetSheetComments(t *testing.T) { f := NewFile() - assert.Equal(t, "", f.getSheetComments(0)) + assert.Equal(t, "", f.getSheetComments("sheet0")) } func TestSetActiveSheet(t *testing.T) {