-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (79 loc) · 2.13 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/Tobi696/googlesheetsparser"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
type Workout struct {
ID uint
NameLine1 string
NameLine2 *string
ImagePath string
Description string
Difficulty uint
Combustion float64
CombustionUnit string
IsFree bool
CategoryID uint
}
type jwtConfig struct {
Email string `json:"client_email"`
PrivateKey string `json:"private_key"`
PrivateKeyID string `json:"private_key_id"`
TokenURI string `json:"token_uri"`
Scopes []string `json:"scopes"`
}
// getService returns a Google Sheets API service
// using the credentials in "credentials.json"
// this code works for Service Accounts only
func getService() *sheets.Service {
// Authenticating, creating the googlesheets Service
var fileConf jwtConfig
confFile, err := os.Open("credentials.json")
if err != nil {
log.Fatalf("Unable to read credentials file: %v", err)
}
defer confFile.Close()
if err := json.NewDecoder(confFile).Decode(&fileConf); err != nil {
log.Fatalf("Unable to parse credentials file: %v", err)
}
conf := &jwt.Config{
Email: fileConf.Email,
PrivateKey: []byte(fileConf.PrivateKey),
PrivateKeyID: fileConf.PrivateKeyID,
TokenURL: fileConf.TokenURI,
Scopes: []string{
"https://www.googleapis.com/auth/spreadsheets.readonly",
},
}
ctx := context.Background()
srv, err := sheets.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
return srv
}
func main() {
srv := getService()
// Acutal usage of the Library
users, err := googlesheetsparser.ParseSheetIntoStructSlice[Workout](googlesheetsparser.Options{
Service: srv,
SpreadsheetID: "15PTbwnLdGJXb4kgLVVBtZ7HbK3QEj-olOxsY7XTzvCc",
DatetimeFormats: []string{
"2.1.2006",
"02.01.2006",
"02.01.2006 15:04:05",
},
}.Build())
if err != nil {
log.Fatalf("Unable to parse page: %v", err)
}
// Do anything you want with the users
fmt.Println(users)
}