Skip to content

Commit a34d3b8

Browse files
committed
Compatibility improvement
1 parent 75d66a0 commit a34d3b8

10 files changed

+39
-51
lines changed

chart.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
18451845
graphicFrame := xlsxGraphicFrame{
18461846
NvGraphicFramePr: xlsxNvGraphicFramePr{
18471847
CNvPr: &xlsxCNvPr{
1848-
ID: f.countCharts() + f.countMedia() + 1,
1848+
ID: len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2,
18491849
Name: "Chart " + strconv.Itoa(cNvPrID),
18501850
},
18511851
},

col.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package excelize
1111

1212
import (
13+
"errors"
1314
"math"
1415
"strings"
1516
)
@@ -112,19 +113,22 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
112113
for c := range xlsx.Cols.Col {
113114
colData := &xlsx.Cols.Col[c]
114115
if colData.Min <= colNum && colNum <= colData.Max {
115-
level = colData.OutlineLevel
116+
level = colData.OutlineLevel + 1
116117
}
117118
}
118119
return level, err
119120
}
120121

121122
// SetColOutlineLevel provides a function to set outline level of a single
122-
// column by given worksheet name and column name. For example, set outline
123-
// level of column D in Sheet1 to 2:
123+
// column by given worksheet name and column name. The value of parameter
124+
// 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
124125
//
125126
// err := f.SetColOutlineLevel("Sheet1", "D", 2)
126127
//
127128
func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
129+
if level > 7 || level < 1 {
130+
return errors.New("invalid outline level")
131+
}
128132
colNum, err := ColumnNameToNumber(col)
129133
if err != nil {
130134
return err

col_test.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
func TestColumnVisibility(t *testing.T) {
1111
t.Run("TestBook1", func(t *testing.T) {
1212
f, err := prepareTestBook1()
13-
if !assert.NoError(t, err) {
14-
t.FailNow()
15-
}
13+
assert.NoError(t, err)
1614

1715
assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
1816
assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
@@ -38,9 +36,7 @@ func TestColumnVisibility(t *testing.T) {
3836

3937
t.Run("TestBook3", func(t *testing.T) {
4038
f, err := prepareTestBook3()
41-
if !assert.NoError(t, err) {
42-
t.FailNow()
43-
}
39+
assert.NoError(t, err)
4440
f.GetColVisible("Sheet1", "B")
4541
})
4642
}
@@ -49,12 +45,14 @@ func TestOutlineLevel(t *testing.T) {
4945
f := NewFile()
5046
f.GetColOutlineLevel("Sheet1", "D")
5147
f.NewSheet("Sheet2")
52-
f.SetColOutlineLevel("Sheet1", "D", 4)
48+
assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
5349
f.GetColOutlineLevel("Sheet1", "D")
5450
f.GetColOutlineLevel("Shee2", "A")
55-
f.SetColWidth("Sheet2", "A", "D", 13)
56-
f.SetColOutlineLevel("Sheet2", "B", 2)
57-
f.SetRowOutlineLevel("Sheet1", 2, 250)
51+
assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
52+
assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
53+
assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
54+
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
55+
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
5856

5957
// Test set and get column outline level with illegal cell coordinates.
6058
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
@@ -67,7 +65,7 @@ func TestOutlineLevel(t *testing.T) {
6765
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
6866
level, err := f.GetRowOutlineLevel("Sheet1", 2)
6967
assert.NoError(t, err)
70-
assert.Equal(t, uint8(250), level)
68+
assert.Equal(t, uint8(7), level)
7169

7270
_, err = f.GetRowOutlineLevel("Sheet1", 0)
7371
assert.EqualError(t, err, `invalid row number 0`)
@@ -76,15 +74,10 @@ func TestOutlineLevel(t *testing.T) {
7674
assert.NoError(t, err)
7775
assert.Equal(t, uint8(0), level)
7876

79-
err = f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx"))
80-
if !assert.NoError(t, err) {
81-
t.FailNow()
82-
}
77+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
8378

8479
f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
85-
if !assert.NoError(t, err) {
86-
t.FailNow()
87-
}
80+
assert.NoError(t, err)
8881
f.SetColOutlineLevel("Sheet2", "B", 2)
8982
}
9083

@@ -138,11 +131,7 @@ func TestInsertCol(t *testing.T) {
138131
f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
139132
f.MergeCell(sheet1, "A1", "C3")
140133

141-
err := f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`)
142-
if !assert.NoError(t, err) {
143-
t.FailNow()
144-
}
145-
134+
assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
146135
assert.NoError(t, f.InsertCol(sheet1, "A"))
147136

148137
// Test insert column with illegal cell coordinates.

picture.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
272272
twoCellAnchor.To = &to
273273
pic := xlsxPic{}
274274
pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
275-
pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1
275+
pic.NvPicPr.CNvPr.ID = len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2
276276
pic.NvPicPr.CNvPr.Descr = file
277277
pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
278278
if hyperlinkRID != 0 {

rows.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package excelize
1111

1212
import (
1313
"encoding/xml"
14+
"errors"
1415
"fmt"
1516
"math"
1617
"strconv"
@@ -257,15 +258,18 @@ func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
257258
}
258259

259260
// SetRowOutlineLevel provides a function to set outline level number of a
260-
// single row by given worksheet name and Excel row number. For example,
261-
// outline row 2 in Sheet1 to level 1:
261+
// single row by given worksheet name and Excel row number. The value of
262+
// parameter 'level' is 1-7. For example, outline row 2 in Sheet1 to level 1:
262263
//
263264
// err := f.SetRowOutlineLevel("Sheet1", 2, 1)
264265
//
265266
func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {
266267
if row < 1 {
267268
return newInvalidRowNumberError(row)
268269
}
270+
if level > 7 || level < 1 {
271+
return errors.New("invalid outline level")
272+
}
269273
xlsx, err := f.workSheetReader(sheet)
270274
if err != nil {
271275
return err

sparkline.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,6 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error {
429429
return err
430430
}
431431
for idx, ext := range decodeExtLst.Ext {
432-
// hack: add back missing namespace
433-
decodeExtLst.Ext[idx].XMLNSX14 = decodeExtLst.Ext[idx].X14
434-
decodeExtLst.Ext[idx].XMLNSX15 = decodeExtLst.Ext[idx].X15
435-
decodeExtLst.Ext[idx].XMLNSX14 = ""
436-
decodeExtLst.Ext[idx].XMLNSX15 = ""
437432
if ext.URI == ExtURISparklineGroups {
438433
decodeSparklineGroups := decodeX14SparklineGroups{}
439434
_ = xml.Unmarshal([]byte(ext.Content), &decodeSparklineGroups)
@@ -458,9 +453,8 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error {
458453
}
459454
sparklineGroupsBytes, _ := xml.Marshal(groups)
460455
extLst := xlsxWorksheetExt{
461-
XMLNSX14: NameSpaceSpreadSheetX14,
462-
URI: ExtURISparklineGroups,
463-
Content: string(sparklineGroupsBytes),
456+
URI: ExtURISparklineGroups,
457+
Content: string(sparklineGroupsBytes),
464458
}
465459
extBytes, _ := xml.Marshal(extLst)
466460
ws.ExtLst.Ext = string(extBytes)

test/Book1.xlsx

-149 Bytes
Binary file not shown.

xmlChart.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,10 @@ type cAutoTitleDeleted struct {
293293
type cView3D struct {
294294
RotX *attrValInt `xml:"rotX"`
295295
RotY *attrValInt `xml:"rotY"`
296+
RAngAx *attrValInt `xml:"rAngAx"`
296297
DepthPercent *attrValInt `xml:"depthPercent"`
297298
Perspective *attrValInt `xml:"perspective"`
298-
RAngAx *attrValInt `xml:"rAngAx"`
299+
ExtLst *xlsxExtLst `xml:"extLst"`
299300
}
300301

301302
// cPlotArea directly maps the plotArea element. This element specifies the

xmlStyles.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ type xlsxFonts struct {
8585
// xlsxFont directly maps the font element. This element defines the
8686
// properties for one of the fonts used in this workbook.
8787
type xlsxFont struct {
88-
Name *attrValString `xml:"name"`
89-
Charset *attrValInt `xml:"charset"`
90-
Family *attrValInt `xml:"family"`
9188
B *bool `xml:"b,omitempty"`
9289
I *bool `xml:"i,omitempty"`
9390
Strike *bool `xml:"strike,omitempty"`
9491
Outline *bool `xml:"outline,omitempty"`
9592
Shadow *bool `xml:"shadow,omitempty"`
9693
Condense *bool `xml:"condense,omitempty"`
9794
Extend *bool `xml:"extend,omitempty"`
98-
Color *xlsxColor `xml:"color"`
99-
Sz *attrValFloat `xml:"sz"`
10095
U *attrValString `xml:"u"`
96+
Sz *attrValFloat `xml:"sz"`
97+
Color *xlsxColor `xml:"color"`
98+
Name *attrValString `xml:"name"`
99+
Family *attrValInt `xml:"family"`
100+
Charset *attrValInt `xml:"charset"`
101101
Scheme *attrValString `xml:"scheme"`
102102
}
103103

xmlWorksheet.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,9 @@ type xlsxLegacyDrawing struct {
629629

630630
// xlsxWorksheetExt directly maps the ext element in the worksheet.
631631
type xlsxWorksheetExt struct {
632-
XMLName xml.Name `xml:"ext"`
633-
XMLNSX14 string `xml:"xmlns:x14,attr,omitempty"`
634-
XMLNSX15 string `xml:"xmlns:x15,attr,omitempty"`
635-
X14 string `xml:"x14,attr,omitempty"`
636-
X15 string `xml:"x15,attr,omitempty"`
637-
URI string `xml:"uri,attr"`
638-
Content string `xml:",innerxml"`
632+
XMLName xml.Name `xml:"ext"`
633+
URI string `xml:"uri,attr"`
634+
Content string `xml:",innerxml"`
639635
}
640636

641637
// decodeWorksheetExt directly maps the ext element.

0 commit comments

Comments
 (0)