-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsv.go
203 lines (160 loc) · 4.2 KB
/
tsv.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package commands
import (
"encoding/csv"
"fmt"
"io"
"regexp"
"strconv"
"time"
"google.golang.org/api/sheets/v4"
)
func sheetToTSV(f io.Writer, data *sheets.ValueRange, withPIN bool) error {
if len(data.Values) == 0 {
return fmt.Errorf("empty sheet")
}
// .. build index
index := map[string]int{}
row := data.Values[0]
for i, v := range row {
k := normalise(v.(string))
if _, ok := index[k]; ok {
return fmt.Errorf("duplicate column name '%s'", v.(string))
}
index[k] = i
}
// ... header
row = data.Values[0]
header := []string{}
if ix, ok := index["cardnumber"]; ok {
header = append(header, clean(row[ix].(string)))
}
if ix, ok := index["pin"]; ok && withPIN {
header = append(header, clean(row[ix].(string)))
}
if ix, ok := index["from"]; ok {
header = append(header, clean(row[ix].(string)))
}
if ix, ok := index["to"]; ok {
header = append(header, clean(row[ix].(string)))
}
for _, v := range row {
k := normalise(v.(string))
if k != "cardnumber" && k != "pin" && k != "from" && k != "to" {
header = append(header, clean(v.(string)))
}
}
if len(header) == 0 {
return fmt.Errorf("missing/invalid header row")
}
if len(header) < 1 || normalise(header[0]) != "cardnumber" {
return fmt.Errorf("missing 'card number' column")
}
if withPIN {
if len(header) < 2 || normalise(header[1]) != "pin" {
return fmt.Errorf("missing 'PIN' column")
}
if len(header) < 3 || normalise(header[2]) != "from" {
return fmt.Errorf("missing 'from' column")
}
if len(header) < 4 || normalise(header[3]) != "to" {
return fmt.Errorf("missing 'to' column")
}
} else {
if len(header) < 2 || normalise(header[1]) != "from" {
return fmt.Errorf("missing 'from' column")
}
if len(header) < 3 || normalise(header[2]) != "to" {
return fmt.Errorf("missing 'to' column")
}
}
// ... records
records := [][]string{}
for _, row := range data.Values[1:] {
if cardnumber, ok := row[index["cardnumber"]].(string); !ok {
continue
} else if ok := regexp.MustCompile(`^\s*[0-9]+\s*$`).MatchString(cardnumber); !ok {
continue
}
if withPIN {
if PIN, ok := row[index["pin"]].(string); !ok {
continue
} else if ok := regexp.MustCompile(`^\s*[0-9]*\s*$`).MatchString(PIN); !ok {
continue
}
}
if from, ok := row[index["from"]].(string); !ok {
continue
} else if _, err := time.ParseInLocation("2006-01-02", from, time.Local); err != nil {
continue
}
if to, ok := row[index["to"]].(string); !ok {
continue
} else if _, err := time.ParseInLocation("2006-01-02", to, time.Local); err != nil {
continue
}
record := []string{}
for _, h := range header {
k := normalise(h)
v := ""
if ix, ok := index[k]; ok {
v = row[ix].(string)
}
record = append(record, clean(v))
}
records = append(records, record)
}
// ... write to file
w := csv.NewWriter(f)
w.Comma = '\t'
w.Write(header)
for _, record := range records {
w.Write(record)
}
w.Flush()
return nil
}
func tsvToSheet(f io.Reader, area string) (*sheets.ValueRange, *sheets.ValueRange, error) {
match := regexp.MustCompile(`(.+?)!([a-zA-Z]+)([0-9]+):([a-zA-Z]+)([0-9]+)?`).FindStringSubmatch(area)
if len(match) < 5 {
return nil, nil, fmt.Errorf("invalid spreadsheet range '%s'", area)
}
name := match[1]
left := match[2]
top, _ := strconv.Atoi(match[3])
right := match[4]
r := csv.NewReader(f)
r.Comma = '\t'
records, err := r.ReadAll()
if err != nil {
return nil, nil, err
}
if len(records) == 0 {
return nil, nil, fmt.Errorf("TSV file is empty")
}
// header
if len(records) < 1 {
return nil, nil, fmt.Errorf("TSV file missing header")
}
h := make([]interface{}, len(records[0]))
for i, v := range records[0] {
h[i] = fmt.Sprintf("%v", v)
}
header := sheets.ValueRange{
Range: fmt.Sprintf("%s!%s%v:%s%v", name, left, top, right, top),
Values: [][]interface{}{h},
}
// data
rows := make([][]interface{}, 0)
for _, record := range records[1:] {
row := make([]interface{}, len(record))
for i, v := range record {
row[i] = fmt.Sprintf("%v", v)
}
rows = append(rows, row)
}
data := sheets.ValueRange{
Range: fmt.Sprintf("%s!%s%v:%s", name, left, top+1, right),
Values: rows,
}
return &header, &data, nil
}