-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.go
More file actions
88 lines (80 loc) · 2.92 KB
/
Copy pathcell.go
File metadata and controls
88 lines (80 loc) · 2.92 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
package simplecsv
import "strings"
// GetCell Returns the value of a cell by position.
// If the cell does not exist, it returns the second value as false.
func (s SimpleCsv) GetCell(column int, row int) (string, bool) {
if row >= 0 && row < len(s) && column >= 0 && column < len(s[row]) {
return s[row][column], true
}
return "", false
}
// GetCellByField returns the value of a cell by column name.
// If the cell does not exist, it returns the second value as false.
func (s SimpleCsv) GetCellByField(columnName string, row int) (string, bool) {
column := s.GetHeaderPosition(columnName)
if column >= 0 && row >= 0 && row < len(s) && column < len(s[row]) {
return s[row][column], true
}
return "", false
}
// SetCell changes the value of a cell and returns a new csv
// The original csv is not modified. Setting a header cell (row 0) that would
// produce duplicate header names is rejected.
func (s SimpleCsv) SetCell(column int, row int, value string) (SimpleCsv, bool) {
if row >= 0 && row < len(s) && column >= 0 && column < len(s[row]) {
newCsv := copyRows(s)
newCsv[row][column] = value
if row == 0 && hasDuplicateHeaders(newCsv[0]) {
return s.fail()
}
return newCsv, true
}
return s.fail()
}
// SetCellByField changes the value of a cell with a specific column
func (s SimpleCsv) SetCellByField(columnName string, row int, value string) (SimpleCsv, bool) {
columnPosition := s.GetHeaderPosition(columnName)
if columnPosition == -1 {
return s.fail()
}
return s.SetCell(columnPosition, row, value)
}
// ReplaceInField returns a new csv where every data cell of the column with
// the header name columnName whose value is exactly equal to old
// (case-sensitive, whole cell) is replaced by new. The header cell in row 0
// is not modified and the original csv is not modified. If the csv is empty
// or the field name does not exist, it returns a copy of the csv and false.
func (s SimpleCsv) ReplaceInField(columnName string, old string, new string) (SimpleCsv, bool) {
columnPosition := s.GetHeaderPosition(columnName)
if columnPosition == -1 || len(s) == 0 {
return s.fail()
}
newCsv := copyRows(s)
for i := 1; i < len(s); i++ {
if newCsv[i][columnPosition] == old {
newCsv[i][columnPosition] = new
}
}
return newCsv, true
}
// TrimSpace returns a new csv where the surrounding whitespace of every
// cell, including the header cells in row 0, has been removed with
// strings.TrimSpace. The original csv is not modified. Trimming can merge
// header names, so if the trimmed header row contains duplicate names the
// operation is rejected (the library requires unique headers): it returns a
// copy of the csv and false.
func (s SimpleCsv) TrimSpace() (SimpleCsv, bool) {
if len(s) == 0 {
return s.fail()
}
newCsv := copyRows(s)
for i, row := range newCsv {
for j, cell := range row {
newCsv[i][j] = strings.TrimSpace(cell)
}
}
if hasDuplicateHeaders(newCsv[0]) {
return s.fail()
}
return newCsv, true
}