-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.go
150 lines (136 loc) · 3.59 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package slidesutil
import (
"strings"
slides "google.golang.org/api/slides/v1"
)
func IsEven(i int) bool {
return i%2 == 0
}
func IsOdd(i int) bool {
return !IsEven(i)
}
func AlternateRowBgColor(objectID string, rowCount, columnCount int64, evenColorHex, oddColorHex string) ([]*slides.Request, error) {
reqs := []*slides.Request{}
colorizeEven := false
colorizeOdd := false
evenColor := &slides.RgbColor{}
oddColor := &slides.RgbColor{}
if len(evenColorHex) > 0 {
c, err := ParseRgbColorHex(evenColorHex)
if err != nil {
return reqs, err
}
evenColor = c
colorizeEven = true
}
if len(oddColorHex) > 0 {
c, err := ParseRgbColorHex(oddColorHex)
if err != nil {
return reqs, err
}
oddColor = c
colorizeOdd = true
}
for i := 0; i < int(rowCount); i++ {
if IsEven(i) && colorizeEven {
reqs = append(reqs,
&slides.Request{
UpdateTableCellProperties: UpdateTableCellPropertiesRequestTableCellBackgroundFill(objectID, int64(i), columnCount, evenColor),
},
)
} else if IsOdd(i) && colorizeOdd {
reqs = append(reqs,
&slides.Request{
UpdateTableCellProperties: UpdateTableCellPropertiesRequestTableCellBackgroundFill(objectID, int64(i), columnCount, oddColor),
},
)
}
}
return reqs, nil
}
func UpdateTableCellPropertiesRequestTableCellBackgroundFill(objectID string, rowIndex, columnSpan int64, bgColor *slides.RgbColor) *slides.UpdateTableCellPropertiesRequest {
return &slides.UpdateTableCellPropertiesRequest{
ObjectId: objectID,
Fields: "*",
TableRange: &slides.TableRange{
Location: &slides.TableCellLocation{
ColumnIndex: 0,
RowIndex: rowIndex,
},
ColumnSpan: columnSpan,
RowSpan: 1,
},
TableCellProperties: &slides.TableCellProperties{
TableCellBackgroundFill: &slides.TableCellBackgroundFill{
SolidFill: &slides.SolidFill{
Color: &slides.OpaqueColor{
RgbColor: bgColor,
},
},
},
},
}
}
// https://developers.google.com/slides/samples/tables#format_a_table_header_row
type UpdateTextStyle struct {
ObjectID string
RowIndex int64
ColumnIndex int64
ForegroundColorHex string
Bold bool
FontFamily string
FontSizeMagnitude float64
FontSizeUnit string
TextRangeType string
Fields string
}
func (item *UpdateTextStyle) RequestsColumnSpan(columnSpan int64) ([]*slides.Request, error) {
reqs := []*slides.Request{}
for i := 0; i < int(columnSpan); i++ {
item.ColumnIndex = int64(i)
req, err := item.Request()
if err != nil {
return reqs, err
}
reqs = append(reqs, req)
}
return reqs, nil
}
func (item *UpdateTextStyle) Request() (*slides.Request, error) {
fields := []string{"bold"}
req := &slides.UpdateTextStyleRequest{
ObjectId: item.ObjectID,
CellLocation: &slides.TableCellLocation{
ColumnIndex: item.ColumnIndex,
RowIndex: item.RowIndex,
},
Style: &slides.TextStyle{
Bold: item.Bold,
},
TextRange: &slides.Range{
Type: item.TextRangeType,
},
Fields: item.Fields,
}
if len(item.ForegroundColorHex) > 0 {
c, err := ParseRgbColorHex(item.ForegroundColorHex)
if err != nil {
return nil, err
}
req.Style.ForegroundColor = &slides.OptionalColor{
OpaqueColor: &slides.OpaqueColor{
RgbColor: c,
},
}
fields = append(fields, "foregroundColor")
}
if item.FontSizeMagnitude > 0 && len(item.FontSizeUnit) > 0 {
req.Style.FontSize = &slides.Dimension{
Magnitude: item.FontSizeMagnitude,
Unit: item.FontSizeUnit,
}
fields = append(fields, "fontSize")
}
req.Fields = strings.Join(fields, ",")
return &slides.Request{UpdateTextStyle: req}, nil
}