forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow.go
101 lines (86 loc) · 2.48 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package xlsx
import (
"fmt"
)
// Row represents a single Row in the current Sheet.
type Row struct {
Hidden bool // Hidden determines whether this Row is hidden or not.
Sheet *Sheet // Sheet is a reference back to the Sheet that this Row is within.
Height float64 // Height is the current height of the Row in PostScript Points
OutlineLevel uint8 // OutlineLevel contains the outline level of this Row. Used for collapsing.
isCustom bool // isCustom is a flag that is set to true when the Row has been modified
num int // Num hold the positional number of the Row in the Sheet
cellCount int // The current number of cells
cells []*Cell // the cells
}
// SetHeight sets the height of the Row in PostScript Points
func (r *Row) SetHeight(ht float64) {
r.Height = ht
r.isCustom = true
}
// SetHeightCM sets the height of the Row in centimetres, inherently converting it to PostScript points.
func (r *Row) SetHeightCM(ht float64) {
r.Height = ht * 28.3464567 // Convert CM to postscript points
r.isCustom = true
}
// AddCell adds a new Cell to the Row
func (r *Row) AddCell() *Cell {
cell := newCell(r, r.cellCount)
r.cellCount++
r.cells = append(r.cells, cell)
return cell
}
func (r *Row) makeCellKey(colIdx int) string {
return fmt.Sprintf("%s:%06d:%06d", r.Sheet.Name, r.num, colIdx)
}
func (r *Row) key() string {
return r.makeCellKeyRowPrefix()
}
func (r *Row) makeCellKeyRowPrefix() string {
return fmt.Sprintf("%s:%06d", r.Sheet.Name, r.num)
}
func (r *Row) growCellsSlice(newSize int) {
capacity := cap(r.cells)
if newSize >= capacity {
newCap := 2 * capacity
if newSize > newCap {
newCap = newSize
}
newSlice := make([]*Cell, newCap, newCap)
copy(newSlice, r.cells)
r.cells = newSlice
}
}
// GetCell returns the Cell at a given column index, creating it if it doesn't exist.
func (r *Row) GetCell(colIdx int) *Cell {
if colIdx >= len(r.cells) {
cell := newCell(r, colIdx)
r.growCellsSlice(colIdx + 1)
r.cells[colIdx] = cell
return cell
}
cell := r.cells[colIdx]
if cell == nil {
cell = newCell(r, colIdx)
r.cells[colIdx] = cell
}
return cell
}
// ForEachCell will call the provided CellVisitorFunc for each
// currently defined cell in the Row.
func (r *Row) ForEachCell(cvf CellVisitorFunc) error {
fn := func(c *Cell) error {
if c != nil {
c.Row = r
return cvf(c)
}
return nil
}
for _, cell := range r.cells {
err := fn(cell)
if err != nil {
return err
}
}
return nil
}