-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
57 lines (48 loc) · 1.18 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package excel
import (
"github.com/xuri/excelize/v2"
)
type OnRow func(rowIndex int, rowData []string) error
type Reader interface {
Read(sheetName string, startRow int, onRow OnRow) error
GetCellValue(sheetName string, cellName string) (string, error)
ExistsSheet(sheetName string) bool
}
type excelReader struct {
File *excelize.File
}
func (r *excelReader) GetCellValue(sheetName string, cellName string) (string, error) {
return r.File.GetCellValue(sheetName, cellName)
}
func NewReader(file *excelize.File) Reader {
return &excelReader{File: file}
}
func (r *excelReader) Read(sheetName string, startRow int, onRow OnRow) error {
return r.read(sheetName, startRow, onRow)
}
func (r *excelReader) read(sheetName string, startRow int, onRow OnRow) error {
rows, err := r.File.GetRows(sheetName)
if err != nil {
return err
}
for rowIndex, row := range rows {
if len(row) <= 0 {
break
}
if rowIndex < startRow {
continue
}
err := onRow(rowIndex, row)
if err != nil {
return err
}
}
return nil
}
func (r *excelReader) ExistsSheet(sheetName string) bool {
index, err := r.File.GetSheetIndex(sheetName)
if err != nil {
return false
}
return index != -1
}