Skip to content

Commit f05f799

Browse files
committed
- API changed, use worksheet name instead of "sheet" + index, related issue qax-os#25, qax-os#43, qax-os#47, qax-os#51, qax-os#89, qax-os#101, qax-os#116 and qax-os#120.
- go test updated
1 parent 3e7192b commit f05f799

11 files changed

+159
-145
lines changed

README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ import (
3838
func main() {
3939
xlsx := excelize.NewFile()
4040
// Create a new sheet.
41-
xlsx.NewSheet(2, "Sheet2")
41+
index := xlsx.NewSheet("Sheet2")
4242
// Set value of a cell.
4343
xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
4444
xlsx.SetCellValue("Sheet1", "B2", 100)
4545
// Set active sheet of the workbook.
46-
xlsx.SetActiveSheet(2)
46+
xlsx.SetActiveSheet(index)
4747
// Save xlsx file by the given path.
4848
err := xlsx.SaveAs("./Workbook.xlsx")
4949
if err != nil {
@@ -63,7 +63,6 @@ package main
6363
import (
6464
"fmt"
6565
"os"
66-
"strconv"
6766

6867
"github.com/xuri/excelize"
6968
)
@@ -74,25 +73,24 @@ func main() {
7473
fmt.Println(err)
7574
os.Exit(1)
7675
}
77-
// Get value from cell by given sheet index and axis.
76+
// Get value from cell by given worksheet name and axis.
7877
cell := xlsx.GetCellValue("Sheet1", "B2")
7978
fmt.Println(cell)
80-
// Get sheet index.
81-
index := xlsx.GetSheetIndex("Sheet2")
82-
// Get all the rows in a sheet.
83-
rows := xlsx.GetRows("sheet" + strconv.Itoa(index))
79+
// Get all the rows in the Sheet1.
80+
rows := xlsx.GetRows("Sheet1")
8481
for _, row := range rows {
8582
for _, colCell := range row {
8683
fmt.Print(colCell, "\t")
8784
}
8885
fmt.Println()
8986
}
9087
}
88+
9189
```
9290

9391
### Add chart to XLSX file
9492

95-
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.
93+
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.
9694

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

@@ -152,7 +150,7 @@ func main() {
152150
if err != nil {
153151
fmt.Println(err)
154152
}
155-
// Insert a picture to sheet with scaling.
153+
// Insert a picture to worksheet with scaling.
156154
err = xlsx.AddPicture("Sheet1", "D2", "./image2.jpg", `{"x_scale": 0.5, "y_scale": 0.5}`)
157155
if err != nil {
158156
fmt.Println(err)

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
263263
// MergeCell provides function to merge cells by given coordinate area and sheet
264264
// name. For example create a merged cell of D3:E9 on Sheet1:
265265
//
266-
// xlsx.MergeCell("sheet1", "D3", "E9")
266+
// xlsx.MergeCell("Sheet1", "D3", "E9")
267267
//
268268
// If you create a merged cell that overlaps with another existing merged cell,
269269
// those merged cells that already exist will be removed.

col.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (f *File) getColWidth(sheet string, col int) int {
225225
return int(defaultColWidthPixels)
226226
}
227227

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

258-
// RemoveCol provides function to remove single column by given worksheet index
258+
// RemoveCol provides function to remove single column by given worksheet name
259259
// and column index. For example, remove column C in Sheet1:
260260
//
261261
// xlsx.RemoveCol("Sheet1", "C")

excelize.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// File define a populated XLSX file struct.
1515
type File struct {
1616
checked map[string]bool
17+
sheetMap map[string]string
1718
ContentTypes *xlsxTypes
1819
Path string
1920
SharedStrings *xlsxSST
@@ -38,6 +39,8 @@ func OpenFile(filename string) (*File, error) {
3839
return nil, err
3940
}
4041
f.Path = filename
42+
f.sheetMap = f.getSheetMap()
43+
f.Styles = f.stylesReader()
4144
return f, nil
4245
}
4346

@@ -77,7 +80,10 @@ func (f *File) setDefaultTimeStyle(sheet, axis string) {
7780
// workSheetReader provides function to get the pointer to the structure after
7881
// deserialization by given worksheet index.
7982
func (f *File) workSheetReader(sheet string) *xlsxWorksheet {
80-
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
83+
name, ok := f.sheetMap[sheet]
84+
if !ok {
85+
name = "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
86+
}
8187
if f.Sheet[name] == nil {
8288
var xlsx xlsxWorksheet
8389
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
@@ -159,8 +165,8 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
159165
// </row>
160166
//
161167
func (f *File) UpdateLinkedValue() {
162-
for i := 1; i <= f.SheetCount; i++ {
163-
xlsx := f.workSheetReader("sheet" + strconv.Itoa(i))
168+
for _, name := range f.GetSheetMap() {
169+
xlsx := f.workSheetReader(name)
164170
for indexR, row := range xlsx.SheetData.Row {
165171
for indexC, col := range row.C {
166172
if col.F != nil && col.V != "" {
@@ -176,7 +182,7 @@ func (f *File) UpdateLinkedValue() {
176182
// hyperlinks, merged cells and auto filter when inserting or deleting rows or
177183
// columns.
178184
//
179-
// sheet: Worksheet index that we're editing
185+
// sheet: Worksheet name that we're editing
180186
// column: Index number of the column we're inserting/deleting before
181187
// row: Index number of the row we're inserting/deleting before
182188
// offset: Number of rows/column to insert/delete negative values indicate deletion

0 commit comments

Comments
 (0)