-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexcel_data.go
More file actions
140 lines (120 loc) · 3.4 KB
/
excel_data.go
File metadata and controls
140 lines (120 loc) · 3.4 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package excelstructure
import (
"fmt"
"strconv"
sliceutil "github.com/booyangcc/utils/sliceutil"
"github.com/hashicorp/go-multierror"
)
// Cell excel cell info
type Cell struct {
RowIndex int
ColIndex int
Value string
Key string
Coordinates string
ErrMsg string
IsEmpty bool
}
// SheetData sheet data.
type SheetData struct {
// RowTotal row total.
RowTotal int
// DataTotal data total.
DataTotal int
SheetName string
FileName string
// Rows map[rowIndex]map[keyField]*Cell
Rows map[int]map[string]*Cell
FieldKeys []string
// DataIndexOffset data index offset.
DataIndexOffset int
}
// Data excel data.
type Data struct {
FileName string
SheetIndexData map[int]*SheetData
SheetNameData map[string]*SheetData
SheetTotal int
SheetList []string
}
// GetCell get cell.
func (s *SheetData) GetCell(rowIndex int, fieldKey string, isCheckEmpty ...bool) (*Cell, error) {
if s.DataTotal < rowIndex-s.DataIndexOffset {
return nil, NewError(s.FileName, s.SheetName, fmt.Sprintf("rowIndex %d", rowIndex), ErrorDataRowOutOfRange)
}
if rowIndex == 1 {
return nil, NewError(s.FileName, s.SheetName, fmt.Sprintf("rowIndex %d", rowIndex), ErrorRowIndexIsHeader)
}
row := s.Rows[rowIndex]
if !sliceutil.InSlice(fieldKey, s.FieldKeys) {
return nil, NewError(s.FileName, s.SheetName,
fmt.Sprintf("rowIndex %d, fieldKey %s", rowIndex, fieldKey), ErrorFieldNotExist)
}
cell, ok := row[fieldKey]
if !ok {
return nil, NewError(s.FileName, s.SheetName,
fmt.Sprintf("rowIndex %d, fieldKey %s", rowIndex, fieldKey), ErrorFieldNotExist)
}
isCheck := false
if len(isCheckEmpty) > 0 {
isCheck = isCheckEmpty[0]
}
if isCheck && cell.IsEmpty {
return nil, NewError(s.FileName, s.SheetName,
fmt.Sprintf("rowIndex %d, fieldKey %s", rowIndex, fieldKey), ErrorFieldValueEmpty)
}
return cell, nil
}
// GetIntValue cong string 获取值
func (s *SheetData) GetIntValue(rowIndex int, fieldKey string, isCheckEmpty ...bool) (int, error) {
v, err := s.GetCell(rowIndex, fieldKey, isCheckEmpty...)
if err != nil {
return 0, err
}
if v.Value == "" {
return 0, nil
}
res, err := strconv.Atoi(v.Value)
if err != nil {
return 0, NewError(s.FileName, s.SheetName, v.Coordinates, ErrorFieldNotMatch)
}
return res, nil
}
// GetStringValue cong string 获取值
func (s *SheetData) GetStringValue(rowIndex int, fieldKey string, isCheckEmpty ...bool) (string, error) {
v, err := s.GetCell(rowIndex, fieldKey, isCheckEmpty...)
if err != nil {
return "", err
}
return v.Value, nil
}
// GetIntValueWithMultiError cong string 获取值
func (s *SheetData) GetIntValueWithMultiError(
rowIndex int, fieldKey string, errs error, isCheckEmpty ...bool,
) (int, error) {
v, err := s.GetCell(rowIndex, fieldKey, isCheckEmpty...)
if err != nil {
errs = multierror.Append(errs, err)
return 0, errs
}
if v.Value == "" {
return 0, errs
}
res, err := strconv.Atoi(v.Value)
if err != nil {
errs = multierror.Append(errs, NewError(s.FileName, s.SheetName, v.Coordinates, ErrorFieldNotMatch))
return 0, errs
}
return res, errs
}
// GetStringValueWithMultiError cong string 获取值
func (s *SheetData) GetStringValueWithMultiError(
rowIndex int, fieldKey string, errs error, isCheckEmpty ...bool,
) (string, error) {
v, err := s.GetCell(rowIndex, fieldKey, isCheckEmpty...)
if err != nil {
errs = multierror.Append(errs, err)
return "", errs
}
return v.Value, errs
}