forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
91 lines (82 loc) · 2.38 KB
/
table.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
// Copyright 2017 FoxyUtils ehf. All rights reserved.
//
// Use of this software package and source code is governed by the terms of the
// UniDoc End User License Agreement (EULA) that is available at:
// https://unidoc.io/eula/
// A trial license code for evaluation can be obtained at https://unidoc.io.
package document
import (
"github.com/unidoc/unioffice/schema/soo/wml"
)
// Table is a table within a document.
type Table struct {
d *Document
x *wml.CT_Tbl
}
// X returns the inner wrapped XML type.
func (t Table) X() *wml.CT_Tbl {
return t.x
}
// Properties returns the table properties.
func (t Table) Properties() TableProperties {
if t.x.TblPr == nil {
t.x.TblPr = wml.NewCT_TblPr()
}
return TableProperties{t.x.TblPr}
}
// AddRow adds a row to a table.
func (t Table) AddRow() Row {
c := wml.NewEG_ContentRowContent()
t.x.EG_ContentRowContent = append(t.x.EG_ContentRowContent, c)
tr := wml.NewCT_Row()
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}
// InsertRowAfter inserts a row after another row
func (t Table) InsertRowAfter(r Row) Row {
for i, rc := range t.x.EG_ContentRowContent {
if len(rc.Tr) > 0 && r.X() == rc.Tr[0] {
c := wml.NewEG_ContentRowContent()
if len(t.x.EG_ContentRowContent) <= i+2 {
return t.AddRow()
}
t.x.EG_ContentRowContent = append(t.x.EG_ContentRowContent, nil)
copy(t.x.EG_ContentRowContent[i+2:], t.x.EG_ContentRowContent[i+1:])
t.x.EG_ContentRowContent[i+1] = c
tr := wml.NewCT_Row()
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}
}
return t.AddRow()
}
// InsertRowBefore inserts a row before another row
func (t Table) InsertRowBefore(r Row) Row {
for i, rc := range t.x.EG_ContentRowContent {
if len(rc.Tr) > 0 && r.X() == rc.Tr[0] {
c := wml.NewEG_ContentRowContent()
t.x.EG_ContentRowContent = append(t.x.EG_ContentRowContent, nil)
copy(t.x.EG_ContentRowContent[i+1:], t.x.EG_ContentRowContent[i:])
t.x.EG_ContentRowContent[i] = c
tr := wml.NewCT_Row()
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}
}
return t.AddRow()
}
// Rows returns the rows defined in the table.
func (t Table) Rows() []Row {
ret := []Row{}
for _, rc := range t.x.EG_ContentRowContent {
for _, ctRow := range rc.Tr {
ret = append(ret, Row{t.d, ctRow})
}
if rc.Sdt != nil && rc.Sdt.SdtContent != nil {
for _, ctRow := range rc.Sdt.SdtContent.Tr {
ret = append(ret, Row{t.d, ctRow})
}
}
}
return ret
}