diff --git a/example_test.go b/example_test.go index 2117bb6..9ece6b5 100644 --- a/example_test.go +++ b/example_test.go @@ -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 func ExampleWorkBook_GetSheet() { if xlFile, err := Open("Table.xls", "utf-8"); err == nil { diff --git a/workbook.go b/workbook.go index a6a4830..d80984d 100644 --- a/workbook.go +++ b/workbook.go @@ -210,16 +210,24 @@ func (w *WorkBook) prepareSheet(sheet *WorkSheet) { sheet.parse(w.rs) } +//Get one sheet by its number func (w *WorkBook) GetSheet(num int) *WorkSheet { if num < len(w.sheets) { s := w.sheets[num] - w.prepareSheet(s) + if !s.parsed { + w.prepareSheet(s) + } return s } else { 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 func (w *WorkBook) ReadAllCells(max int) (res [][]string) { res = make([][]string, 0) diff --git a/worksheet.go b/worksheet.go index 50b2cf4..bad4cf9 100644 --- a/worksheet.go +++ b/worksheet.go @@ -22,6 +22,7 @@ type WorkSheet struct { Rows map[uint16]*Row //NOTICE: this is the max row number of the sheet, so it should be count -1 MaxRow uint16 + parsed bool } func (w *WorkSheet) parse(buf io.ReadSeeker) { @@ -39,6 +40,7 @@ func (w *WorkSheet) parse(buf io.ReadSeeker) { break } } + w.parsed = true } func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {