-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.go
41 lines (36 loc) · 1.26 KB
/
cell.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
package simplecsv
// 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 column >= 0 && column < len(s[0]) && row >= 0 && row < len(s) {
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 < len(s) {
return s[row][column], true
}
return "", false
}
// SetCell changes the value of a cell
func (s SimpleCsv) SetCell(column int, row int, value string) (SimpleCsv, bool) {
if column >= 0 && column < len(s[0]) && row >= 0 && row < len(s) {
s[row][column] = value
return s, true
}
return s, false
}
// SetCellByField changes the value of a cell with a specific column
func (s SimpleCsv) SetCellByField(columnName string, row int, value string) (SimpleCsv, bool) {
var changed bool
if s.GetHeaderPosition(columnName) == -1 {
return s, false
}
columnPosition := s.GetHeaderPosition(columnName)
s, changed = s.SetCell(columnPosition, row, value)
return s, changed
}