-
Notifications
You must be signed in to change notification settings - Fork 0
/
anniversaries.go
71 lines (57 loc) · 1.59 KB
/
anniversaries.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
package main
import (
"io/ioutil"
"log"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"github.com/Iwark/spreadsheet"
"regexp"
"strings"
"time"
)
type EmployeeAnniversary struct {
Name string
Date string
WorksYears int
Color string
}
func getWorkAnniversaries() []EmployeeAnniversary {
ctx := context.Background()
b, err := ioutil.ReadFile(config.CalendarSecret)
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
calendarConfig, err := google.ConfigFromJSON(b, spreadsheet.Scope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getSheetClient(ctx, calendarConfig)
service := &spreadsheet.Service{Client: client}
sheets, _ := service.Get(config.AnniversariesSheetId)
ws1, _ := sheets.Get(0)
currentMonth := int(time.Now().Month()) - 1
employees := []EmployeeAnniversary{}
for i, row := range ws1.Rows {
if i == 0 {
continue
}
rowData := row[currentMonth]
if rowData == nil {
break
}
employees = append(employees, parseEmployee(rowData))
}
return employees
}
func parseEmployee(data *spreadsheet.Cell) EmployeeAnniversary {
r, _ := regexp.Compile("([0-9]{4}-[0-9]{2}-[0-9]{2})")
rName, _ := regexp.Compile(`[0-9\(\)\-]+`)
employeeWorkYear, _ := time.Parse("2006-01-02", r.FindString(data.Content))
worksYears := time.Now().Year() - employeeWorkYear.Year()
return EmployeeAnniversary{
Color: randomColor(),
Date: r.FindString(data.Content),
Name: strings.TrimSpace(rName.ReplaceAllString(data.Content, "")),
WorksYears: worksYears,
}
}