From 66e5d1fa801680f87a647a968fd4965ef9668b9a Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Wed, 28 Jun 2017 17:03:20 +0800 Subject: [PATCH] API changed, use `NewFile()` instead of `CreateFile()` and use `SaveAs()` instead of `WriteTo()`. --- README.md | 8 ++++---- chart.go | 4 ++-- col.go | 2 +- excelize_test.go | 22 +++++++++++----------- file.go | 12 ++++++------ picture.go | 4 ++-- rows.go | 2 +- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2cefd0c..c6ed92e 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ import ( ) func main() { - xlsx := excelize.CreateFile() + xlsx := excelize.NewFile() // Create a new sheet. xlsx.NewSheet(2, "Sheet2") // Set value of a cell. @@ -45,7 +45,7 @@ func main() { // Set active sheet of the workbook. xlsx.SetActiveSheet(2) // Save xlsx file by the given path. - err := xlsx.WriteTo("./Workbook.xlsx") + err := xlsx.SaveAs("./Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) @@ -109,7 +109,7 @@ import ( func main() { categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"} values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8} - xlsx := excelize.CreateFile() + xlsx := excelize.NewFile() for k, v := range categories { xlsx.SetCellValue("Sheet1", k, v) } @@ -118,7 +118,7 @@ func main() { } xlsx.AddChart("Sheet1", "E1", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$2","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$2:$D$2"},{"name":"=Sheet1!$A$2","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$3:$D$3"},{"name":"=Sheet1!$A$3","categories":"=Sheet1!$B$1:$D$1","values":"=Sheet1!$B$4:$D$4"}],"title":{"name":"Fruit 3D Line Chart"}}`) // Save xlsx file by the given path. - err := xlsx.WriteTo("./Workbook.xlsx") + err := xlsx.SaveAs("./Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) diff --git a/chart.go b/chart.go index f83e0a7..a7ca5ce 100644 --- a/chart.go +++ b/chart.go @@ -71,7 +71,7 @@ func parseFormatChartSet(formatSet string) *formatChart { // func main() { // categories := map[string]string{"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"} // values := map[string]int{"B2": 2, "C2": 3, "D2": 3, "B3": 5, "C3": 2, "D3": 4, "B4": 6, "C4": 7, "D4": 8} -// xlsx := excelize.CreateFile() +// xlsx := excelize.NewFile() // for k, v := range categories { // xlsx.SetCellValue("Sheet1", k, v) // } @@ -80,7 +80,7 @@ func parseFormatChartSet(formatSet string) *formatChart { // } // xlsx.AddChart("SHEET1", "F2", `{"type":"bar3D","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Line Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) // // Save xlsx file by the given path. -// err := xlsx.WriteTo("./Workbook.xlsx") +// err := xlsx.SaveAs("./Workbook.xlsx") // if err != nil { // fmt.Println(err) // os.Exit(1) diff --git a/col.go b/col.go index 2081e40..56c114e 100644 --- a/col.go +++ b/col.go @@ -69,7 +69,7 @@ func (f *File) SetColVisible(sheet, column string, visible bool) { // SetColWidth provides function to set the width of a single column or multiple // columns. For example: // -// xlsx := excelize.CreateFile() +// xlsx := excelize.NewFile() // xlsx.SetColWidth("Sheet1", "A", "H", 20) // err := xlsx.Save() // if err != nil { diff --git a/excelize_test.go b/excelize_test.go index 77413ed..d7f6fb0 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -89,7 +89,7 @@ func TestOpenFile(t *testing.T) { t.Log(err) } // Test write file to not exist directory. - err = xlsx.WriteTo("") + err = xlsx.SaveAs("") if err != nil { t.Log(err) } @@ -121,7 +121,7 @@ func TestAddPicture(t *testing.T) { t.Log(err) } // Test write file to given path. - err = xlsx.WriteTo("./test/Workbook_2.xlsx") + err = xlsx.SaveAs("./test/Workbook_2.xlsx") if err != nil { t.Log(err) } @@ -135,7 +135,7 @@ func TestBrokenFile(t *testing.T) { t.Log(err) } // Test write file with broken file struct with given path. - err = xlsx.WriteTo("./test/Workbook_3.xlsx") + err = xlsx.SaveAs("./test/Workbook_3.xlsx") if err != nil { t.Log(err) } @@ -154,9 +154,9 @@ func TestBrokenFile(t *testing.T) { } } -func TestCreateFile(t *testing.T) { +func TestNewFile(t *testing.T) { // Test create a XLSX file. - xlsx := CreateFile() + xlsx := NewFile() xlsx.NewSheet(2, "XLSXSheet2") xlsx.NewSheet(3, "XLSXSheet3") xlsx.SetCellInt("Sheet2", "A23", 56) @@ -171,17 +171,17 @@ func TestCreateFile(t *testing.T) { if err != nil { t.Log(err) } - err = xlsx.WriteTo("./test/Workbook_3.xlsx") + err = xlsx.SaveAs("./test/Workbook_3.xlsx") if err != nil { t.Log(err) } } func TestSetColWidth(t *testing.T) { - xlsx := CreateFile() + xlsx := NewFile() xlsx.SetColWidth("sheet1", "B", "A", 12) xlsx.SetColWidth("sheet1", "A", "B", 12) - err := xlsx.WriteTo("./test/Workbook_4.xlsx") + err := xlsx.SaveAs("./test/Workbook_4.xlsx") if err != nil { t.Log(err) } @@ -269,10 +269,10 @@ func TestMergeCell(t *testing.T) { } func TestSetRowHeight(t *testing.T) { - xlsx := CreateFile() + xlsx := NewFile() xlsx.SetRowHeight("Sheet1", 0, 50) xlsx.SetRowHeight("Sheet1", 3, 90) - err := xlsx.WriteTo("./test/Workbook_5.xlsx") + err := xlsx.SaveAs("./test/Workbook_5.xlsx") if err != nil { t.Log(err) } @@ -639,7 +639,7 @@ func TestAddChart(t *testing.T) { xlsx.AddChart("SHEET2", "P1", `{"type":"radar","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"top_right","show_legend_key":false},"title":{"name":"Fruit Radar Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"span"}`) xlsx.AddChart("SHEET2", "X1", `{"type":"scatter","series":[{"name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"},{"name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"},{"name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"bottom","show_legend_key":false},"title":{"name":"Fruit Scatter Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"}`) // Save xlsx file by the given path. - err = xlsx.WriteTo("./test/Workbook_6.xlsx") + err = xlsx.SaveAs("./test/Workbook_6.xlsx") if err != nil { t.Log(err) } diff --git a/file.go b/file.go index a094899..4af636b 100644 --- a/file.go +++ b/file.go @@ -8,12 +8,12 @@ import ( "os" ) -// CreateFile provides function to create new file by default template. For +// NewFile provides function to create new file by default template. For // example: // -// xlsx := CreateFile() +// xlsx := NewFile() // -func CreateFile() *File { +func NewFile() *File { file := make(map[string]string) file["_rels/.rels"] = XMLHeader + templateRels file["docProps/app.xml"] = XMLHeader + templateDocpropsApp @@ -35,12 +35,12 @@ func (f *File) Save() error { if f.Path == "" { return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write") } - return f.WriteTo(f.Path) + return f.SaveAs(f.Path) } -// WriteTo provides function to create or update to an xlsx file at the provided +// SaveAs provides function to create or update to an xlsx file at the provided // path. -func (f *File) WriteTo(name string) error { +func (f *File) SaveAs(name string) error { file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666) if err != nil { return err diff --git a/picture.go b/picture.go index da9bb4e..e2c0ff7 100644 --- a/picture.go +++ b/picture.go @@ -47,7 +47,7 @@ func parseFormatPictureSet(formatSet string) *formatPicture { // ) // // func main() { -// xlsx := excelize.CreateFile() +// xlsx := excelize.NewFile() // // Insert a picture. // err := xlsx.AddPicture("Sheet1", "A2", "./image1.jpg", "") // if err != nil { @@ -63,7 +63,7 @@ func parseFormatPictureSet(formatSet string) *formatPicture { // if err != nil { // fmt.Println(err) // } -// err = xlsx.WriteTo("./Workbook.xlsx") +// err = xlsx.SaveAs("./Workbook.xlsx") // if err != nil { // fmt.Println(err) // os.Exit(1) diff --git a/rows.go b/rows.go index 1d94a6c..a913b7a 100644 --- a/rows.go +++ b/rows.go @@ -103,7 +103,7 @@ func (f *File) getTotalRowsCols(sheet string) (int, int) { // SetRowHeight provides a function to set the height of a single row. // For example: // -// xlsx := excelize.CreateFile() +// xlsx := excelize.NewFile() // xlsx.SetRowHeight("Sheet1", 0, 50) // err := xlsx.Save() // if err != nil {