From a34d3b8c86d67d3ad0bc0dbedb69d3b4ebbc210f Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 24 Sep 2019 21:53:19 +0800 Subject: [PATCH] Compatibility improvement --- chart.go | 2 +- col.go | 10 +++++++--- col_test.go | 35 ++++++++++++----------------------- picture.go | 2 +- rows.go | 8 ++++++-- sparkline.go | 10 ++-------- test/Book1.xlsx | Bin 20899 -> 20750 bytes xmlChart.go | 3 ++- xmlStyles.go | 10 +++++----- xmlWorksheet.go | 10 +++------- 10 files changed, 39 insertions(+), 51 deletions(-) diff --git a/chart.go b/chart.go index db2df1e0..7db7eeee 100644 --- a/chart.go +++ b/chart.go @@ -1845,7 +1845,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI graphicFrame := xlsxGraphicFrame{ NvGraphicFramePr: xlsxNvGraphicFramePr{ CNvPr: &xlsxCNvPr{ - ID: f.countCharts() + f.countMedia() + 1, + ID: len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2, Name: "Chart " + strconv.Itoa(cNvPrID), }, }, diff --git a/col.go b/col.go index ffa0ca6d..be08c290 100644 --- a/col.go +++ b/col.go @@ -10,6 +10,7 @@ package excelize import ( + "errors" "math" "strings" ) @@ -112,19 +113,22 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) { for c := range xlsx.Cols.Col { colData := &xlsx.Cols.Col[c] if colData.Min <= colNum && colNum <= colData.Max { - level = colData.OutlineLevel + level = colData.OutlineLevel + 1 } } return level, err } // SetColOutlineLevel provides a function to set outline level of a single -// column by given worksheet name and column name. For example, set outline -// level of column D in Sheet1 to 2: +// column by given worksheet name and column name. The value of parameter +// 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2: // // err := f.SetColOutlineLevel("Sheet1", "D", 2) // func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error { + if level > 7 || level < 1 { + return errors.New("invalid outline level") + } colNum, err := ColumnNameToNumber(col) if err != nil { return err diff --git a/col_test.go b/col_test.go index e3164d49..a696caad 100644 --- a/col_test.go +++ b/col_test.go @@ -10,9 +10,7 @@ import ( func TestColumnVisibility(t *testing.T) { t.Run("TestBook1", func(t *testing.T) { f, err := prepareTestBook1() - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) assert.NoError(t, f.SetColVisible("Sheet1", "F", false)) assert.NoError(t, f.SetColVisible("Sheet1", "F", true)) @@ -38,9 +36,7 @@ func TestColumnVisibility(t *testing.T) { t.Run("TestBook3", func(t *testing.T) { f, err := prepareTestBook3() - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) f.GetColVisible("Sheet1", "B") }) } @@ -49,12 +45,14 @@ func TestOutlineLevel(t *testing.T) { f := NewFile() f.GetColOutlineLevel("Sheet1", "D") f.NewSheet("Sheet2") - f.SetColOutlineLevel("Sheet1", "D", 4) + assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4)) f.GetColOutlineLevel("Sheet1", "D") f.GetColOutlineLevel("Shee2", "A") - f.SetColWidth("Sheet2", "A", "D", 13) - f.SetColOutlineLevel("Sheet2", "B", 2) - f.SetRowOutlineLevel("Sheet1", 2, 250) + assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13)) + assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2)) + assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7)) + assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level") + assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level") // Test set and get column outline level with illegal cell coordinates. assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`) @@ -67,7 +65,7 @@ func TestOutlineLevel(t *testing.T) { assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0") level, err := f.GetRowOutlineLevel("Sheet1", 2) assert.NoError(t, err) - assert.Equal(t, uint8(250), level) + assert.Equal(t, uint8(7), level) _, err = f.GetRowOutlineLevel("Sheet1", 0) assert.EqualError(t, err, `invalid row number 0`) @@ -76,15 +74,10 @@ func TestOutlineLevel(t *testing.T) { assert.NoError(t, err) assert.Equal(t, uint8(0), level) - err = f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx"))) f, err = OpenFile(filepath.Join("test", "Book1.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } + assert.NoError(t, err) f.SetColOutlineLevel("Sheet2", "B", 2) } @@ -138,11 +131,7 @@ func TestInsertCol(t *testing.T) { f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") f.MergeCell(sheet1, "A1", "C3") - err := f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`) - if !assert.NoError(t, err) { - t.FailNow() - } - + assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`)) assert.NoError(t, f.InsertCol(sheet1, "A")) // Test insert column with illegal cell coordinates. diff --git a/picture.go b/picture.go index 518463a5..4470fa17 100644 --- a/picture.go +++ b/picture.go @@ -272,7 +272,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he twoCellAnchor.To = &to pic := xlsxPic{} pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect - pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1 + pic.NvPicPr.CNvPr.ID = len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2 pic.NvPicPr.CNvPr.Descr = file pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID) if hyperlinkRID != 0 { diff --git a/rows.go b/rows.go index 6281e625..37964411 100644 --- a/rows.go +++ b/rows.go @@ -11,6 +11,7 @@ package excelize import ( "encoding/xml" + "errors" "fmt" "math" "strconv" @@ -257,8 +258,8 @@ func (f *File) GetRowVisible(sheet string, row int) (bool, error) { } // SetRowOutlineLevel provides a function to set outline level number of a -// single row by given worksheet name and Excel row number. For example, -// outline row 2 in Sheet1 to level 1: +// single row by given worksheet name and Excel row number. The value of +// parameter 'level' is 1-7. For example, outline row 2 in Sheet1 to level 1: // // err := f.SetRowOutlineLevel("Sheet1", 2, 1) // @@ -266,6 +267,9 @@ func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error { if row < 1 { return newInvalidRowNumberError(row) } + if level > 7 || level < 1 { + return errors.New("invalid outline level") + } xlsx, err := f.workSheetReader(sheet) if err != nil { return err diff --git a/sparkline.go b/sparkline.go index 314ea834..b09dbf40 100644 --- a/sparkline.go +++ b/sparkline.go @@ -429,11 +429,6 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error { return err } for idx, ext := range decodeExtLst.Ext { - // hack: add back missing namespace - decodeExtLst.Ext[idx].XMLNSX14 = decodeExtLst.Ext[idx].X14 - decodeExtLst.Ext[idx].XMLNSX15 = decodeExtLst.Ext[idx].X15 - decodeExtLst.Ext[idx].XMLNSX14 = "" - decodeExtLst.Ext[idx].XMLNSX15 = "" if ext.URI == ExtURISparklineGroups { decodeSparklineGroups := decodeX14SparklineGroups{} _ = xml.Unmarshal([]byte(ext.Content), &decodeSparklineGroups) @@ -458,9 +453,8 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error { } sparklineGroupsBytes, _ := xml.Marshal(groups) extLst := xlsxWorksheetExt{ - XMLNSX14: NameSpaceSpreadSheetX14, - URI: ExtURISparklineGroups, - Content: string(sparklineGroupsBytes), + URI: ExtURISparklineGroups, + Content: string(sparklineGroupsBytes), } extBytes, _ := xml.Marshal(extLst) ws.ExtLst.Ext = string(extBytes) diff --git a/test/Book1.xlsx b/test/Book1.xlsx index 78431dceaa3c5bd2ee952b1bbbf8482fdc5cd39f..d5a059121b7d591b7fab2dc10f7ed6202255dc28 100644 GIT binary patch delta 1143 zcmZ3yn6YmWBVT|wGm8iV2L}g3QiX;8Mm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F;yP zJbP~a#;z)kw{p906-DjpDqgRm7C0r@wbgC!p)31b55FmE`ZzVQ_tCBIO4GbwG2Rbd zRMtEzDSu+{6+6??6FsXOleCw7I6qf|OJsK5w_6drrUra7Jja%LBhEwn;F`j;=j$C_ znLlyeTiEh=hkm8qZO)VORkxX=uFqeYwb?_&yFxqYXPCtG|eslM))(6~wC z)y#82Wj78>y;yfK{#Gqdec0~21=BZd4`!6!t@+sR(IYF%-G1lRO}2YHp)K%a0^hBr z!fZ=8L}p%Gvr+6%#G%uEX6M)}^=*2IC=&xi1S3@=j%W1*K7gmK@?8p4qCc7ZU`ygea|%}~Z9A3KPE6O7T~V-FEI4r6fn zLe==d7=6A_8_xLpLKMsTIYAi7ey$M48b7G6cQA&5zd1xI+aD^m*58&7T=IZI02~05 zfB5S|6ln)YF|Bl(Y~Z9iIVXSzB31(wGjW-m?P; diff --git a/xmlChart.go b/xmlChart.go index bb4b4bc8..19e86e2c 100644 --- a/xmlChart.go +++ b/xmlChart.go @@ -293,9 +293,10 @@ type cAutoTitleDeleted struct { type cView3D struct { RotX *attrValInt `xml:"rotX"` RotY *attrValInt `xml:"rotY"` + RAngAx *attrValInt `xml:"rAngAx"` DepthPercent *attrValInt `xml:"depthPercent"` Perspective *attrValInt `xml:"perspective"` - RAngAx *attrValInt `xml:"rAngAx"` + ExtLst *xlsxExtLst `xml:"extLst"` } // cPlotArea directly maps the plotArea element. This element specifies the diff --git a/xmlStyles.go b/xmlStyles.go index 7e02d6ea..16a89abd 100644 --- a/xmlStyles.go +++ b/xmlStyles.go @@ -85,9 +85,6 @@ type xlsxFonts struct { // xlsxFont directly maps the font element. This element defines the // properties for one of the fonts used in this workbook. type xlsxFont struct { - Name *attrValString `xml:"name"` - Charset *attrValInt `xml:"charset"` - Family *attrValInt `xml:"family"` B *bool `xml:"b,omitempty"` I *bool `xml:"i,omitempty"` Strike *bool `xml:"strike,omitempty"` @@ -95,9 +92,12 @@ type xlsxFont struct { Shadow *bool `xml:"shadow,omitempty"` Condense *bool `xml:"condense,omitempty"` Extend *bool `xml:"extend,omitempty"` - Color *xlsxColor `xml:"color"` - Sz *attrValFloat `xml:"sz"` U *attrValString `xml:"u"` + Sz *attrValFloat `xml:"sz"` + Color *xlsxColor `xml:"color"` + Name *attrValString `xml:"name"` + Family *attrValInt `xml:"family"` + Charset *attrValInt `xml:"charset"` Scheme *attrValString `xml:"scheme"` } diff --git a/xmlWorksheet.go b/xmlWorksheet.go index 7e8cfde5..fa079742 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -629,13 +629,9 @@ type xlsxLegacyDrawing struct { // xlsxWorksheetExt directly maps the ext element in the worksheet. type xlsxWorksheetExt struct { - XMLName xml.Name `xml:"ext"` - XMLNSX14 string `xml:"xmlns:x14,attr,omitempty"` - XMLNSX15 string `xml:"xmlns:x15,attr,omitempty"` - X14 string `xml:"x14,attr,omitempty"` - X15 string `xml:"x15,attr,omitempty"` - URI string `xml:"uri,attr"` - Content string `xml:",innerxml"` + XMLName xml.Name `xml:"ext"` + URI string `xml:"uri,attr"` + Content string `xml:",innerxml"` } // decodeWorksheetExt directly maps the ext element.