Skip to content

Commit f58dabd

Browse files
committed
Breaking change: changed the function signature for 11 exported functions
* Change `func (f *File) NewConditionalStyle(style string) (int, error)` to `func (f *File) NewConditionalStyle(style *Style) (int, error)` * Change `func (f *File) NewStyle(style interface{}) (int, error)` to `func (f *File) NewStyle(style *Style) (int, error)` * Change `func (f *File) AddChart(sheet, cell, opts string, combo ...string) error` to `func (f *File) AddChart(sheet, cell string, chart *ChartOptions, combo ...*ChartOptions) error` * Change `func (f *File) AddChartSheet(sheet, opts string, combo ...string) error` to `func (f *File) AddChartSheet(sheet string, chart *ChartOptions, combo ...*ChartOptions) error` * Change `func (f *File) AddShape(sheet, cell, opts string) error` to `func (f *File) AddShape(sheet, cell string, opts *Shape) error` * Change `func (f *File) AddPictureFromBytes(sheet, cell, opts, name, extension string, file []byte) error` to `func (f *File) AddPictureFromBytes(sheet, cell, name, extension string, file []byte, opts *PictureOptions) error` * Change `func (f *File) AddTable(sheet, hCell, vCell, opts string) error` to `func (f *File) AddTable(sheet, reference string, opts *TableOptions) error` * Change `func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error` to `func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error` * Change `func (f *File) AutoFilter(sheet, hCell, vCell, opts string) error` to `func (f *File) AutoFilter(sheet, reference string, opts *AutoFilterOptions) error` * Change `func (f *File) SetPanes(sheet, panes string) error` to `func (f *File) SetPanes(sheet string, panes *Panes) error` * Change `func (sw *StreamWriter) AddTable(hCell, vCell, opts string) error` to `func (sw *StreamWriter) AddTable(reference string, opts *TableOptions) error` * Change `func (f *File) SetConditionalFormat(sheet, reference, opts string) error` to `func (f *File) SetConditionalFormat(sheet, reference string, opts []ConditionalFormatOptions) error` * Add exported types: * AutoFilterListOptions * AutoFilterOptions * Chart * ChartAxis * ChartDimension * ChartLegend * ChartLine * ChartMarker * ChartPlotArea * ChartSeries * ChartTitle * ConditionalFormatOptions * PaneOptions * Panes * PictureOptions * Shape * ShapeColor * ShapeLine * ShapeParagraph * TableOptions * This added support for set sheet visible as very hidden * Return error when missing required parameters for set defined name * Update unit test and comments
1 parent a57203a commit f58dabd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2317
-1980
lines changed

README.md

+42-41
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,40 @@ import (
121121
)
122122

123123
func main() {
124-
categories := map[string]string{
125-
"A2": "Small", "A3": "Normal", "A4": "Large",
126-
"B1": "Apple", "C1": "Orange", "D1": "Pear"}
127-
values := map[string]int{
128-
"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
129124
f := excelize.NewFile()
130-
for k, v := range categories {
131-
f.SetCellValue("Sheet1", k, v)
132-
}
133-
for k, v := range values {
134-
f.SetCellValue("Sheet1", k, v)
125+
for idx, row := range [][]interface{}{
126+
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
127+
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
128+
} {
129+
cell, err := excelize.CoordinatesToCellName(1, idx+1)
130+
if err != nil {
131+
fmt.Println(err)
132+
return
133+
}
134+
f.SetSheetRow("Sheet1", cell, &row)
135135
}
136-
if err := f.AddChart("Sheet1", "E1", `{
137-
"type": "col3DClustered",
138-
"series": [
139-
{
140-
"name": "Sheet1!$A$2",
141-
"categories": "Sheet1!$B$1:$D$1",
142-
"values": "Sheet1!$B$2:$D$2"
136+
if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
137+
Type: "col3DClustered",
138+
Series: []excelize.ChartSeries{
139+
{
140+
Name: "Sheet1!$A$2",
141+
Categories: "Sheet1!$B$1:$D$1",
142+
Values: "Sheet1!$B$2:$D$2",
143+
},
144+
{
145+
Name: "Sheet1!$A$3",
146+
Categories: "Sheet1!$B$1:$D$1",
147+
Values: "Sheet1!$B$3:$D$3",
148+
},
149+
{
150+
Name: "Sheet1!$A$4",
151+
Categories: "Sheet1!$B$1:$D$1",
152+
Values: "Sheet1!$B$4:$D$4",
153+
}},
154+
Title: excelize.ChartTitle{
155+
Name: "Fruit 3D Clustered Column Chart",
143156
},
144-
{
145-
"name": "Sheet1!$A$3",
146-
"categories": "Sheet1!$B$1:$D$1",
147-
"values": "Sheet1!$B$3:$D$3"
148-
},
149-
{
150-
"name": "Sheet1!$A$4",
151-
"categories": "Sheet1!$B$1:$D$1",
152-
"values": "Sheet1!$B$4:$D$4"
153-
}],
154-
"title":
155-
{
156-
"name": "Fruit 3D Clustered Column Chart"
157-
}
158-
}`); err != nil {
157+
}); err != nil {
159158
fmt.Println(err)
160159
return
161160
}
@@ -193,22 +192,24 @@ func main() {
193192
}
194193
}()
195194
// Insert a picture.
196-
if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
195+
if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil {
197196
fmt.Println(err)
198197
}
199198
// Insert a picture to worksheet with scaling.
199+
enable, disable, scale := true, false, 0.5
200200
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
201-
`{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
201+
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
202202
fmt.Println(err)
203203
}
204204
// Insert a picture offset in the cell with printing support.
205-
if err := f.AddPicture("Sheet1", "H2", "image.gif", `{
206-
"x_offset": 15,
207-
"y_offset": 10,
208-
"print_obj": true,
209-
"lock_aspect_ratio": false,
210-
"locked": false
211-
}`); err != nil {
205+
if err := f.AddPicture("Sheet1", "H2", "image.gif",
206+
&excelize.PictureOptions{
207+
PrintObject: &enable,
208+
LockAspectRatio: false,
209+
OffsetX: 15,
210+
OffsetY: 10,
211+
Locked: &disable,
212+
}); err != nil {
212213
fmt.Println(err)
213214
}
214215
// Save the spreadsheet with the origin path.

README_zh.md

+42-41
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,40 @@ import (
121121
)
122122

123123
func main() {
124-
categories := map[string]string{
125-
"A2": "Small", "A3": "Normal", "A4": "Large",
126-
"B1": "Apple", "C1": "Orange", "D1": "Pear"}
127-
values := map[string]int{
128-
"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8}
129124
f := excelize.NewFile()
130-
for k, v := range categories {
131-
f.SetCellValue("Sheet1", k, v)
132-
}
133-
for k, v := range values {
134-
f.SetCellValue("Sheet1", k, v)
125+
for idx, row := range [][]interface{}{
126+
{nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3},
127+
{"Normal", 5, 2, 4}, {"Large", 6, 7, 8},
128+
} {
129+
cell, err := excelize.CoordinatesToCellName(1, idx+1)
130+
if err != nil {
131+
fmt.Println(err)
132+
return
133+
}
134+
f.SetSheetRow("Sheet1", cell, &row)
135135
}
136-
if err := f.AddChart("Sheet1", "E1", `{
137-
"type": "col3DClustered",
138-
"series": [
139-
{
140-
"name": "Sheet1!$A$2",
141-
"categories": "Sheet1!$B$1:$D$1",
142-
"values": "Sheet1!$B$2:$D$2"
136+
if err := f.AddChart("Sheet1", "E1", &excelize.Chart{
137+
Type: "col3DClustered",
138+
Series: []excelize.ChartSeries{
139+
{
140+
Name: "Sheet1!$A$2",
141+
Categories: "Sheet1!$B$1:$D$1",
142+
Values: "Sheet1!$B$2:$D$2",
143+
},
144+
{
145+
Name: "Sheet1!$A$3",
146+
Categories: "Sheet1!$B$1:$D$1",
147+
Values: "Sheet1!$B$3:$D$3",
148+
},
149+
{
150+
Name: "Sheet1!$A$4",
151+
Categories: "Sheet1!$B$1:$D$1",
152+
Values: "Sheet1!$B$4:$D$4",
153+
}},
154+
Title: excelize.ChartTitle{
155+
Name: "Fruit 3D Clustered Column Chart",
143156
},
144-
{
145-
"name": "Sheet1!$A$3",
146-
"categories": "Sheet1!$B$1:$D$1",
147-
"values": "Sheet1!$B$3:$D$3"
148-
},
149-
{
150-
"name": "Sheet1!$A$4",
151-
"categories": "Sheet1!$B$1:$D$1",
152-
"values": "Sheet1!$B$4:$D$4"
153-
}],
154-
"title":
155-
{
156-
"name": "Fruit 3D Clustered Column Chart"
157-
}
158-
}`); err != nil {
157+
}); err != nil {
159158
fmt.Println(err)
160159
return
161160
}
@@ -193,22 +192,24 @@ func main() {
193192
}
194193
}()
195194
// 插入图片
196-
if err := f.AddPicture("Sheet1", "A2", "image.png", ""); err != nil {
195+
if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil {
197196
fmt.Println(err)
198197
}
199198
// 在工作表中插入图片,并设置图片的缩放比例
199+
enable, disable, scale := true, false, 0.5
200200
if err := f.AddPicture("Sheet1", "D2", "image.jpg",
201-
`{"x_scale": 0.5, "y_scale": 0.5}`); err != nil {
201+
&excelize.PictureOptions{XScale: &scale, YScale: &scale}); err != nil {
202202
fmt.Println(err)
203203
}
204204
// 在工作表中插入图片,并设置图片的打印属性
205-
if err := f.AddPicture("Sheet1", "H2", "image.gif", `{
206-
"x_offset": 15,
207-
"y_offset": 10,
208-
"print_obj": true,
209-
"lock_aspect_ratio": false,
210-
"locked": false
211-
}`); err != nil {
205+
if err := f.AddPicture("Sheet1", "H2", "image.gif",
206+
&excelize.PictureOptions{
207+
PrintObject: &enable,
208+
LockAspectRatio: false,
209+
OffsetX: 15,
210+
OffsetY: 10,
211+
Locked: &disable,
212+
}); err != nil {
212213
fmt.Println(err)
213214
}
214215
// 保存工作簿

adjust_test.go

+22-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestAdjustMergeCells(t *testing.T) {
1212
f := NewFile()
13-
// Test adjustAutoFilter with illegal cell reference.
13+
// Test adjustAutoFilter with illegal cell reference
1414
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
1515
MergeCells: &xlsxMergeCells{
1616
Cells: []*xlsxMergeCell{
@@ -57,7 +57,7 @@ func TestAdjustMergeCells(t *testing.T) {
5757
},
5858
}, columns, 1, -1))
5959

60-
// Test adjustMergeCells.
60+
// Test adjust merge cells
6161
var cases []struct {
6262
label string
6363
ws *xlsxWorksheet
@@ -68,7 +68,7 @@ func TestAdjustMergeCells(t *testing.T) {
6868
expectRect []int
6969
}
7070

71-
// Test insert.
71+
// Test adjust merged cell when insert rows and columns
7272
cases = []struct {
7373
label string
7474
ws *xlsxWorksheet
@@ -139,7 +139,7 @@ func TestAdjustMergeCells(t *testing.T) {
139139
assert.Equal(t, c.expectRect, c.ws.MergeCells.Cells[0].rect, c.label)
140140
}
141141

142-
// Test delete,
142+
// Test adjust merged cells when delete rows and columns
143143
cases = []struct {
144144
label string
145145
ws *xlsxWorksheet
@@ -292,7 +292,7 @@ func TestAdjustAutoFilter(t *testing.T) {
292292
Ref: "A1:A3",
293293
},
294294
}, rows, 1, -1))
295-
// Test adjustAutoFilter with illegal cell reference.
295+
// Test adjustAutoFilter with illegal cell reference
296296
assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
297297
AutoFilter: &xlsxAutoFilter{
298298
Ref: "A:B1",
@@ -307,47 +307,48 @@ func TestAdjustAutoFilter(t *testing.T) {
307307

308308
func TestAdjustTable(t *testing.T) {
309309
f, sheetName := NewFile(), "Sheet1"
310-
for idx, tableRange := range [][]string{{"B2", "C3"}, {"E3", "F5"}, {"H5", "H8"}, {"J5", "K9"}} {
311-
assert.NoError(t, f.AddTable(sheetName, tableRange[0], tableRange[1], fmt.Sprintf(`{
312-
"table_name": "table%d",
313-
"table_style": "TableStyleMedium2",
314-
"show_first_column": true,
315-
"show_last_column": true,
316-
"show_row_stripes": false,
317-
"show_column_stripes": true
318-
}`, idx)))
310+
for idx, reference := range []string{"B2:C3", "E3:F5", "H5:H8", "J5:K9"} {
311+
assert.NoError(t, f.AddTable(sheetName, reference, &TableOptions{
312+
Name: fmt.Sprintf("table%d", idx),
313+
StyleName: "TableStyleMedium2",
314+
ShowFirstColumn: true,
315+
ShowLastColumn: true,
316+
ShowRowStripes: boolPtr(false),
317+
ShowColumnStripes: true,
318+
}))
319319
}
320320
assert.NoError(t, f.RemoveRow(sheetName, 2))
321321
assert.NoError(t, f.RemoveRow(sheetName, 3))
322322
assert.NoError(t, f.RemoveCol(sheetName, "H"))
323323
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAdjustTable.xlsx")))
324324

325325
f = NewFile()
326-
assert.NoError(t, f.AddTable(sheetName, "A1", "D5", ""))
327-
// Test adjust table with non-table part.
326+
assert.NoError(t, f.AddTable(sheetName, "A1:D5", nil))
327+
// Test adjust table with non-table part
328328
f.Pkg.Delete("xl/tables/table1.xml")
329329
assert.NoError(t, f.RemoveRow(sheetName, 1))
330-
// Test adjust table with unsupported charset.
330+
// Test adjust table with unsupported charset
331331
f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset)
332332
assert.NoError(t, f.RemoveRow(sheetName, 1))
333-
// Test adjust table with invalid table range reference.
333+
// Test adjust table with invalid table range reference
334334
f.Pkg.Store("xl/tables/table1.xml", []byte(`<table ref="-" />`))
335335
assert.NoError(t, f.RemoveRow(sheetName, 1))
336336
}
337337

338338
func TestAdjustHelper(t *testing.T) {
339339
f := NewFile()
340-
f.NewSheet("Sheet2")
340+
_, err := f.NewSheet("Sheet2")
341+
assert.NoError(t, err)
341342
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
342343
MergeCells: &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:B1"}}},
343344
})
344345
f.Sheet.Store("xl/worksheets/sheet2.xml", &xlsxWorksheet{
345346
AutoFilter: &xlsxAutoFilter{Ref: "A1:B"},
346347
})
347-
// Test adjustHelper with illegal cell reference.
348+
// Test adjustHelper with illegal cell reference
348349
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
349350
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
350-
// Test adjustHelper on not exists worksheet.
351+
// Test adjustHelper on not exists worksheet
351352
assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN does not exist")
352353
}
353354

calc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const (
4848
formulaErrorSPILL = "#SPILL!"
4949
formulaErrorCALC = "#CALC!"
5050
formulaErrorGETTINGDATA = "#GETTING_DATA"
51-
// formula criteria condition enumeration.
51+
// Formula criteria condition enumeration
5252
_ byte = iota
5353
criteriaEq
5454
criteriaLe
@@ -100,7 +100,7 @@ const (
100100
)
101101

102102
var (
103-
// tokenPriority defined basic arithmetic operator priority.
103+
// tokenPriority defined basic arithmetic operator priority
104104
tokenPriority = map[string]int{
105105
"^": 5,
106106
"*": 4,

calc_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5478,7 +5478,8 @@ func TestCalcSLOP(t *testing.T) {
54785478

54795479
func TestCalcSHEET(t *testing.T) {
54805480
f := NewFile()
5481-
f.NewSheet("Sheet2")
5481+
_, err := f.NewSheet("Sheet2")
5482+
assert.NoError(t, err)
54825483
formulaList := map[string]string{
54835484
"=SHEET(\"Sheet2\")": "2",
54845485
"=SHEET(Sheet2!A1)": "2",
@@ -5494,7 +5495,8 @@ func TestCalcSHEET(t *testing.T) {
54945495

54955496
func TestCalcSHEETS(t *testing.T) {
54965497
f := NewFile()
5497-
f.NewSheet("Sheet2")
5498+
_, err := f.NewSheet("Sheet2")
5499+
assert.NoError(t, err)
54985500
formulaList := map[string]string{
54995501
"=SHEETS(Sheet1!A1:B1)": "1",
55005502
"=SHEETS(Sheet1!A1:Sheet1!A1)": "1",

calcchain_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestCalcChainReader(t *testing.T) {
1010
f := NewFile()
11-
// Test read calculation chain with unsupported charset.
11+
// Test read calculation chain with unsupported charset
1212
f.CalcChain = nil
1313
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
1414
_, err := f.calcChainReader()
@@ -34,12 +34,12 @@ func TestDeleteCalcChain(t *testing.T) {
3434
formulaType, ref := STCellFormulaTypeShared, "C1:C5"
3535
assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=A1+B1", FormulaOpts{Ref: &ref, Type: &formulaType}))
3636

37-
// Test delete calculation chain with unsupported charset calculation chain.
37+
// Test delete calculation chain with unsupported charset calculation chain
3838
f.CalcChain = nil
3939
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
4040
assert.EqualError(t, f.SetCellValue("Sheet1", "C1", true), "XML syntax error on line 1: invalid UTF-8")
4141

42-
// Test delete calculation chain with unsupported charset content types.
42+
// Test delete calculation chain with unsupported charset content types
4343
f.ContentTypes = nil
4444
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
4545
assert.EqualError(t, f.deleteCalcChain(1, "A1"), "XML syntax error on line 1: invalid UTF-8")

0 commit comments

Comments
 (0)