forked from menduo/exsoul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.go
158 lines (138 loc) · 3.84 KB
/
row.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Created by @menduo @ 2020/6/19
package exsoul
import (
"fmt"
"github.com/shopspring/decimal"
)
type Row struct {
colList []string
}
func (r *Row) GetCol(colIdx int) string {
return getStrFromArray(r.colList, colIdx)
}
func (r *Row) IsEmptyRow() bool {
return isEmptyStrList(r.colList)
}
// GetColAsInt col->str->int32
func (r *Row) GetColAsStr(colIdx int, dftVal ...string) (string, error) {
var err error
var val string
if len(r.colList) < colIdx+1 {
if len(dftVal) > 0 {
val = dftVal[0]
return val, nil
}
err = ErrorOutOfRange
return "", err
}
val = r.GetCol(colIdx)
return val, err
}
// GetColAsInt col->str->int32
func (r *Row) GetColAsInt(colIdx int, dftVal ...int) (int, error) {
val, err := toInt(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsInt32 col->str->int32
func (r *Row) GetColAsInt32(colIdx int, dftVal ...int32) (int32, error) {
val, err := toInt32(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsInt64 col到int64
func (r *Row) GetColAsInt64(colIdx int, dftVal ...int64) (int64, error) {
val, err := toInt64(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsFloat 到 float, float64
func (r *Row) GetColAsFloat(colIdx int, dftVal ...float64) (float64, error) {
val, err := toFloat64(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsFloat32 转为 flot32
func (r *Row) GetColAsFloat32(colIdx int, dftVal ...float32) (float32, error) {
val, err := toFloat32(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsFloat64 转为 flot64
func (r *Row) GetColAsFloat64(colIdx int, dftVal ...float64) (float64, error) {
val, err := toFloat64(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
val = dftVal[0]
}
}
return val, err
}
// GetColAsYuanToFen 从 元 转成 分。把 cell 的值转为 float64 ,再 乘以 100?
func (r *Row) GetColAsYuanToFen(colIdx int, dftVal ...int64) (int64, error) {
return r.GetColAsYuanToFenWithDigit(colIdx, 2, dftVal...)
}
// GetColAsYuanToFenWithDigit 从 元 转成 分。把 cell 的值转为 float64 ,再 乘以 100?
func (r *Row) GetColAsYuanToFenWithDigit(colIdx, digit int, dftVal ...int64) (int64, error) {
var value int64
tval, err := toFloat64(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
value = dftVal[0]
}
}
digit2, _ := decimal.NewFromString(fmt.Sprintf("1%0*d", digit, 0))
value = decimal.NewFromFloat(tval).Mul(digit2).IntPart()
return value, err
}
// GetColAsFenToYuan 从 分转成 元。把 cell 的值转为 int64,再除以 100?
func (r *Row) GetColAsFenToYuan(colIdx int, dftVal ...float64) (float64, error) {
return r.GetColAsFenToYuanWithDigit(colIdx, 2, dftVal...)
}
// GetColAsFenToYuanWithDigit 从 分转成 元。把 cell 的值转为 int64,再除以 100?
func (r *Row) GetColAsFenToYuanWithDigit(colIdx, digit int, dftVal ...float64) (float64, error) {
var value float64
tval, err := toInt64(r.GetCol(colIdx))
if err != nil {
if len(dftVal) > 0 {
value = dftVal[0]
}
}
digit2, _ := toInt64(fmt.Sprintf("1%0*d", digit, 0))
value, _ = decimal.NewFromInt(tval).Div(decimal.NewFromInt(digit2)).Float64()
return value, err
}
// IsColValidMonthStr 是否为有效 月份 字符串
func (r *Row) IsColValidMonthStr(colIdx int, layouts ...string) (bool, error) {
if _, err := validateMonth(r.GetCol(colIdx), layouts...); err != nil {
return false, err
}
return true, nil
}
// IsColValidDateStr 是否为有效 日期 字符串
func (r *Row) IsColValidDateStr(colIdx int, layouts ...string) (bool, error) {
if _, err := validateDate(r.GetCol(colIdx), layouts...); err != nil {
return false, err
}
return true, nil
}