forked from Luna-CY/go-office
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_row.go
117 lines (90 loc) · 2.09 KB
/
table_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package table
import (
"errors"
"fmt"
"github.com/wangcan-null/go-office/docx/table/row"
"sync"
)
// Row 表格行结构定义
type Row struct {
// pr 表格行的属性指针
pr *row.TrPr
cm sync.RWMutex
// 单元格列表
cells []*Cell
}
// GetProperties 获取属性配置结构
func (r *Row) GetProperties() *row.TrPr {
if nil == r.pr {
r.pr = new(row.TrPr)
}
return r.pr
}
// GetCells 获取全部单元格
func (r *Row) GetCells() []*Cell {
r.cm.RLock()
r.cm.RUnlock()
return r.cells
}
// GetCell 获取指定位置的单元格
func (r *Row) GetCell(index uint) (*Cell, error) {
if index >= uint(len(r.cells)) {
return nil, errors.New(fmt.Sprintf("索引溢出"))
}
r.cm.RLock()
defer r.cm.RUnlock()
return r.cells[index], nil
}
// AddCellText 添加指定数量单元格的文本内容
func (r *Row) AddCellText(cells ...interface{}) error {
r.cm.RLock()
defer r.cm.RUnlock()
if len(cells) > len(r.cells) {
return errors.New(fmt.Sprintf("索引溢出: 单元格数量为 %v 当前设置数量为 %v", len(r.cells), len(cells)))
}
for i, text := range cells {
cell := r.cells[i]
cell.AddParagraph().AddRun().AddText(text)
}
return nil
}
// addCell 添加一个自动宽度的单元格
func (r *Row) addCell() {
cell := new(Cell)
r.cm.Lock()
defer r.cm.Unlock()
r.cells = append(r.cells, cell)
}
// addCellWithWidth 添加一个单元格并指定宽度
func (r *Row) addCellWithWidth(width int) {
cell := new(Cell)
cell.GetProperties().SetWidth(width)
r.cm.Lock()
defer r.cm.Unlock()
r.cells = append(r.cells, cell)
}
// addCellWithIndexAndWidth 添加一列到指定位置以及宽度
func (r *Row) addCellWithIndexAndWidth(index int, width int) {
cell := new(Cell)
cell.GetProperties().SetWidth(width)
r.cm.Lock()
defer r.cm.Unlock()
if index >= len(r.cells) {
r.cells = append(r.cells, cell)
return
}
before := r.cells[:index]
after := r.cells[index:]
i := 0
r.cells = make([]*Cell, len(r.cells)+1)
for _, c := range before {
r.cells[i] = c
i += 1
}
r.cells[i] = cell
i += 1
for _, c := range after {
r.cells[i] = c
i += 1
}
}