Skip to content

Commit

Permalink
- API changed, use worksheet name instead of "sheet" + index, related…
Browse files Browse the repository at this point in the history
… issue #25, #43, #47, #51, #89, #101, #116 and #120.

- go test updated
  • Loading branch information
xuri committed Sep 13, 2017
1 parent 3e7192b commit f05f799
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 145 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import (
func main() {
xlsx := excelize.NewFile()
// Create a new sheet.
xlsx.NewSheet(2, "Sheet2")
index := xlsx.NewSheet("Sheet2")
// Set value of a cell.
xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
xlsx.SetCellValue("Sheet1", "B2", 100)
// Set active sheet of the workbook.
xlsx.SetActiveSheet(2)
xlsx.SetActiveSheet(index)
// Save xlsx file by the given path.
err := xlsx.SaveAs("./Workbook.xlsx")
if err != nil {
Expand All @@ -63,7 +63,6 @@ package main
import (
"fmt"
"os"
"strconv"

"github.com/xuri/excelize"
)
Expand All @@ -74,25 +73,24 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
// Get value from cell by given sheet index and axis.
// Get value from cell by given worksheet name and axis.
cell := xlsx.GetCellValue("Sheet1", "B2")
fmt.Println(cell)
// Get sheet index.
index := xlsx.GetSheetIndex("Sheet2")
// Get all the rows in a sheet.
rows := xlsx.GetRows("sheet" + strconv.Itoa(index))
// Get all the rows in the Sheet1.
rows := xlsx.GetRows("Sheet1")
for _, row := range rows {
for _, colCell := range row {
fmt.Print(colCell, "\t")
}
fmt.Println()
}
}

```

### Add chart to XLSX file

With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your sheet at all.
With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your worksheet at all.

![Excelize](./test/images/chart.png "Excelize")

Expand Down Expand Up @@ -152,7 +150,7 @@ func main() {
if err != nil {
fmt.Println(err)
}
// Insert a picture to sheet with scaling.
// Insert a picture to worksheet with scaling.
err = xlsx.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
// MergeCell provides function to merge cells by given coordinate area and sheet
// name. For example create a merged cell of D3:E9 on Sheet1:
//
// xlsx.MergeCell("sheet1", "D3", "E9")
// xlsx.MergeCell("Sheet1", "D3", "E9")
//
// If you create a merged cell that overlaps with another existing merged cell,
// those merged cells that already exist will be removed.
Expand Down
4 changes: 2 additions & 2 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (f *File) getColWidth(sheet string, col int) int {
return int(defaultColWidthPixels)
}

// GetColWidth provides function to get column width by given sheet name and
// GetColWidth provides function to get column width by given worksheet name and
// column index.
func (f *File) GetColWidth(sheet, column string) float64 {
col := TitleToNumber(strings.ToUpper(column)) + 1
Expand Down Expand Up @@ -255,7 +255,7 @@ func (f *File) InsertCol(sheet, column string) {
f.adjustHelper(sheet, col, -1, 1)
}

// RemoveCol provides function to remove single column by given worksheet index
// RemoveCol provides function to remove single column by given worksheet name
// and column index. For example, remove column C in Sheet1:
//
// xlsx.RemoveCol("Sheet1", "C")
Expand Down
14 changes: 10 additions & 4 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// File define a populated XLSX file struct.
type File struct {
checked map[string]bool
sheetMap map[string]string
ContentTypes *xlsxTypes
Path string
SharedStrings *xlsxSST
Expand All @@ -38,6 +39,8 @@ func OpenFile(filename string) (*File, error) {
return nil, err
}
f.Path = filename
f.sheetMap = f.getSheetMap()
f.Styles = f.stylesReader()
return f, nil
}

Expand Down Expand Up @@ -77,7 +80,10 @@ func (f *File) setDefaultTimeStyle(sheet, axis string) {
// workSheetReader provides function to get the pointer to the structure after
// deserialization by given worksheet index.
func (f *File) workSheetReader(sheet string) *xlsxWorksheet {
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
name, ok := f.sheetMap[sheet]
if !ok {
name = "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
}
if f.Sheet[name] == nil {
var xlsx xlsxWorksheet
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
Expand Down Expand Up @@ -159,8 +165,8 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// </row>
//
func (f *File) UpdateLinkedValue() {
for i := 1; i <= f.SheetCount; i++ {
xlsx := f.workSheetReader("sheet" + strconv.Itoa(i))
for _, name := range f.GetSheetMap() {
xlsx := f.workSheetReader(name)
for indexR, row := range xlsx.SheetData.Row {
for indexC, col := range row.C {
if col.F != nil && col.V != "" {
Expand All @@ -176,7 +182,7 @@ func (f *File) UpdateLinkedValue() {
// hyperlinks, merged cells and auto filter when inserting or deleting rows or
// columns.
//
// sheet: Worksheet index that we're editing
// sheet: Worksheet name that we're editing
// column: Index number of the column we're inserting/deleting before
// row: Index number of the row we're inserting/deleting before
// offset: Number of rows/column to insert/delete negative values indicate deletion
Expand Down

0 comments on commit f05f799

Please sign in to comment.