-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonly.go
50 lines (40 loc) · 1.03 KB
/
only.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
package simplecsv
// OnlyThisRows removes all rows that are not in the index and sorts the csv by the index order
// All rows must exist or it fails
func (s SimpleCsv) OnlyThisRows(rowsIndex []int, header bool) (SimpleCsv, bool) {
ok := true
lenghtS := len(s)
for _, v := range rowsIndex {
if v < 0 || v >= lenghtS {
ok = false
return s, ok
}
}
newCsv := SimpleCsv{}
if header == true {
headers := s.GetHeaders()
newCsv = append(newCsv, headers)
}
var rowToAdd []string
for _, g := range rowsIndex {
rowToAdd, _ = s.GetRow(g)
newCsv = append(newCsv, rowToAdd)
}
return newCsv, ok
}
// OnlyThisFields returns a simplecsv with the fields
func (s SimpleCsv) OnlyThisFields(fields []string) (SimpleCsv, bool) {
var row []string
var fieldToAdd string
lenghS := len(s)
newCsv := CreateEmpyCsv(fields)
for i := 1; i < lenghS; i++ {
for _, n := range fields {
fieldToAdd, _ = s.GetCellByField(n, i)
row = append(row, fieldToAdd)
}
newCsv, _ = newCsv.AddRow(row)
row = []string{}
}
return newCsv, true
}