-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.go
77 lines (63 loc) · 1.66 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
package xlsx
import (
"github.com/pkg/errors"
)
// Row represents a row in a worksheet. A worksheet has a collection of rows.
type Row struct {
worksheet *Worksheet
index int
cells []*Cell
cellsMap map[string]*Cell
committed bool
}
// CellOptions has options used when creating a new cell.
type CellOptions struct {
Key *string
Value interface{}
}
// AddCell adds a new cell to the row.
func (r *Row) AddCell() (*Cell, error) {
if r.committed {
return nil, errors.New("can't add cells to a committed row")
}
if r.worksheet.columns != nil {
return nil, errors.New("can't add cells without keys to this worksheet as columns were defined")
}
cellIndex := len(r.cells)
cell := &Cell{
row: r,
index: cellIndex,
identifier: createIdentifierFromCoords(cellIndex, r.index),
}
r.cells = append(r.cells, cell)
return cell, nil
}
// AddCellWithKey is just like AddCell but adds a key to the cell. Should be used if columns were defined on the worksheet.
func (r *Row) AddCellWithKey(key string) (*Cell, error) {
if r.committed {
return nil, errors.New("can't add cells to a committed row")
}
if r.worksheet.columns == nil {
return nil, errors.New("can't add cells with keys if no columns were defined")
}
var cellIndex int
var found bool
for i := 0; i < len(r.worksheet.columns); i++ {
if r.worksheet.columns[i].Key == key {
cellIndex = i
found = true
break
}
}
if !found {
return nil, errors.Errorf("undefined column named %s", key)
}
cell := &Cell{
row: r,
index: cellIndex,
identifier: createIdentifierFromCoords(cellIndex, r.index),
Key: key,
}
r.cellsMap[key] = cell
return cell, nil
}