21
0

Add NumSheets to get the number of sheets

This commit is contained in:
Liu Ming 2016-01-21 13:14:37 +08:00
parent 1dd2ce626e
commit 1375b24d9d
3 changed files with 20 additions and 1 deletions

View File

@ -10,6 +10,15 @@ func ExampleOpen() {
} }
} }
func ExampleNumberSheets() {
if xlFile, err := Open("Table.xls", "utf-8"); err == nil {
for i := 0; i < xlFile.NumSheets(); i++ {
sheet := xlFile.GetSheet(i)
fmt.Println(sheet.Name)
}
}
}
//Output: read the content of first two cols in each row //Output: read the content of first two cols in each row
func ExampleWorkBook_GetSheet() { func ExampleWorkBook_GetSheet() {
if xlFile, err := Open("Table.xls", "utf-8"); err == nil { if xlFile, err := Open("Table.xls", "utf-8"); err == nil {

View File

@ -210,16 +210,24 @@ func (w *WorkBook) prepareSheet(sheet *WorkSheet) {
sheet.parse(w.rs) sheet.parse(w.rs)
} }
//Get one sheet by its number
func (w *WorkBook) GetSheet(num int) *WorkSheet { func (w *WorkBook) GetSheet(num int) *WorkSheet {
if num < len(w.sheets) { if num < len(w.sheets) {
s := w.sheets[num] s := w.sheets[num]
if !s.parsed {
w.prepareSheet(s) w.prepareSheet(s)
}
return s return s
} else { } else {
return nil return nil
} }
} }
//Get the number of all sheets, look into example
func (w *WorkBook) NumSheets() int {
return len(w.sheets)
}
//helper function to read all cells from file //helper function to read all cells from file
func (w *WorkBook) ReadAllCells(max int) (res [][]string) { func (w *WorkBook) ReadAllCells(max int) (res [][]string) {
res = make([][]string, 0) res = make([][]string, 0)

View File

@ -22,6 +22,7 @@ type WorkSheet struct {
Rows map[uint16]*Row Rows map[uint16]*Row
//NOTICE: this is the max row number of the sheet, so it should be count -1 //NOTICE: this is the max row number of the sheet, so it should be count -1
MaxRow uint16 MaxRow uint16
parsed bool
} }
func (w *WorkSheet) parse(buf io.ReadSeeker) { func (w *WorkSheet) parse(buf io.ReadSeeker) {
@ -39,6 +40,7 @@ func (w *WorkSheet) parse(buf io.ReadSeeker) {
break break
} }
} }
w.parsed = true
} }
func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof { func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {