-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsx_sheet_parser.go
61 lines (55 loc) · 1.24 KB
/
xlsx_sheet_parser.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
58
59
60
61
package xlsxcfg
import "context"
type sheetParser struct {
typeName string
maxRow int
cols [][]string
param *Config
}
func newSheetParser(p *Config) *sheetParser {
return &sheetParser{param: p}
}
func (p *sheetParser) SetName(name string) {
p.typeName = name
}
func (p *sheetParser) Feed(cells []string) {
p.cols = append(p.cols, cells)
if p.maxRow < len(cells) {
p.maxRow = len(cells)
}
}
// Parse one xls sheet
func (p *sheetParser) Parse(ctx context.Context) ([]any, error) {
colCount := len(p.cols)
rowParser := newRowParser(p.typeName, p.param)
result := make([]any, 0, p.maxRow)
dataRows := make([][]string, 0, p.maxRow)
for i := 0; i < p.maxRow; i++ {
row := make([]string, 0, colCount)
for _, col := range p.cols {
if len(col) > i {
row = append(row, col[i])
} else {
row = append(row, "")
}
}
if p.param.IsMeta(i, row) {
rowParser.Meta(ctx, row)
} else if p.param.IsComment(i, row) {
// comment row, skip
} else if p.param.IsData(i, row) {
dataRows = append(dataRows, row)
}
}
for _, row := range dataRows {
rowData, err := rowParser.Parse(ctx, row)
if err != nil {
return nil, err
}
if rowData == nil {
continue
}
result = append(result, rowData)
}
return result, nil
}