-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsheets.go
192 lines (162 loc) · 5.21 KB
/
sheets.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"errors"
"io/ioutil"
"regexp"
"strconv"
"strings"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"gopkg.in/Iwark/spreadsheet.v2"
)
// Board members of a Toastmasters meeting.
type Board struct {
President string
VPE string
VPM string
VPPR string
Secretary string
Treasurer string
SAA string
}
// NewBoard is a factory function using a spreadsheet to fill in Board members.
func NewBoard(sheet *spreadsheet.Sheet) *Board {
if len(sheet.Columns) < 2 || len(sheet.Columns[1]) < 7 {
return &Board{}
}
return &Board{
President: sheet.Columns[1][0].Value,
VPE: sheet.Columns[1][1].Value,
VPM: sheet.Columns[1][2].Value,
VPPR: sheet.Columns[1][3].Value,
Secretary: sheet.Columns[1][4].Value,
Treasurer: sheet.Columns[1][5].Value,
SAA: sheet.Columns[1][6].Value,
}
}
// AgendaRoles contains the editable fields on a Toastmasters agenda.
type AgendaRoles struct {
Toastmaster string
GE string
Timer string
AhCounter string
Grammarian string
TableTopicsMaster string
JokeMaster string
Speakers []*Speaker
BoardMembers *Board
FutureWeeks [][]string
}
// NewAgendaRoles is a factory function to create agenda roles from a google doc based on the date of the meeting.
func NewAgendaRoles(agendaDate string) (*AgendaRoles, error) {
spreadsheets, err := fetchSheet()
if err != nil {
return &AgendaRoles{}, err
}
agendaRoles := &AgendaRoles{
BoardMembers: NewBoard(spreadsheets.boardSheet),
}
const speakerCellStart = 7
const speakerCellEnd = 13
rolesSheet := spreadsheets.meetingRoles
for i, currentColumn := range rolesSheet.Columns {
if currentColumn[0].Value == agendaDate {
agendaRoles.Toastmaster = currentColumn[1].Value
agendaRoles.JokeMaster = currentColumn[2].Value
agendaRoles.GE = currentColumn[3].Value
agendaRoles.Timer = currentColumn[4].Value
agendaRoles.AhCounter = currentColumn[5].Value
agendaRoles.Grammarian = currentColumn[6].Value
for j := speakerCellStart; j <= speakerCellEnd; j += 2 {
agendaRoles.Speakers = append(agendaRoles.Speakers, NewSpeaker(currentColumn[j].Value,
currentColumn[j+1].Value))
}
agendaRoles.TableTopicsMaster = currentColumn[16].Value
agendaRoles.FutureWeeks = futureWeeks(rolesSheet, i)
break
}
}
return agendaRoles, nil
}
// A Speaker in a Toastmasters meeting.
type Speaker struct {
Name string
*Speech
Evaluator string
}
// Helper method that returns the first Name of a Speaker.
func (s *Speaker) firstName() string {
return strings.Split(s.Name, " ")[0]
}
// Find the Speaker Name, manual and number from a string that looks like "Ann Addicks\nCC #9".
func parseManualAndNumber(speaker string) (string, string, int) {
re := regexp.MustCompile(`([a-zA-Z]+ [a-zA-Z]+)\n([a-zA-Z]+) #(\d{1,2})`)
result := re.FindStringSubmatch(speaker)
name := speaker
var manual string
var speechNum int
if len(result) > 0 {
name = result[1]
manual = result[2]
speechNum, _ = strconv.Atoi(result[3])
}
return name, manual, speechNum
}
// NewSpeaker is a factory function to create a Speaker based on the spreadsheet Speaker and Evaluator.
func NewSpeaker(s string, eval string) *Speaker {
name, manual, number := parseManualAndNumber(s)
return &Speaker{
Name: name,
Evaluator: eval,
Speech: NewSpeech(manual, number),
}
}
// Represents the spreadsheet tabs. This is to stay unexported.
type googleDocsSheet struct {
boardSheet *spreadsheet.Sheet
meetingRoles *spreadsheet.Sheet
}
// GetSheet reads a Google Docs spreadsheet and returns a sheet with roles and another sheet with the Board members.
func fetchSheet() (googleDocsSheet, error) {
data, err := ioutil.ReadFile("client_secret.json")
if err != nil {
return googleDocsSheet{}, errors.New("cannot read client_secret.json")
}
conf, err := google.JWTConfigFromJSON(data, spreadsheet.Scope)
if err != nil {
return googleDocsSheet{}, errors.New("problem with google.JWTConfigFromJSON(data, s.Scope)")
}
client := conf.Client(context.TODO())
service := spreadsheet.NewServiceWithClient(client)
s, err := service.FetchSpreadsheet("1CBlORqCzL6YvyAUZTk8jezvhyuDzjjumghwGKk5VIK8")
if err != nil {
return googleDocsSheet{}, errors.New("cannot fetch spread sheet: ")
}
roles, err := s.SheetByIndex(0)
if err != nil {
return googleDocsSheet{}, errors.New("cannot read s by index 0")
}
board, err := s.SheetByIndex(1)
if err != nil {
return googleDocsSheet{}, errors.New("cannot read s by index 1")
}
return googleDocsSheet{boardSheet: board, meetingRoles: roles}, nil
}
// FutureWeeks finds the next several weeks after the current week based on the constant futureWeeks.
func futureWeeks(sheet *spreadsheet.Sheet, thisWeek int) [][]string {
// The number of weeks in the future to capture.
const numOfWeeks = 4
const numberOfRoles = 17
var week int
nextSchedule := make([][]string, 0, numOfWeeks)
colLen := len(sheet.Columns)
for i := thisWeek + 1; i < colLen && week <= numOfWeeks; i++ {
nextWeek := make([]string, numberOfRoles)
for j := 0; j < numberOfRoles; j++ {
nextWeek[j] = sheet.Columns[i][j].Value
}
nextSchedule = append(nextSchedule, nextWeek)
week++
}
return nextSchedule
}