-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_sheets_api.go
60 lines (48 loc) · 1.17 KB
/
google_sheets_api.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
package google_sheets_api
import (
"context"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
"os"
)
type SheetClient struct {
srv *sheets.Service
spreadsheetID string
}
func NewSheetClient(spreadsheetID string) (*SheetClient, error) {
ctx := context.Background()
fileName := "secret.json"
_, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
srv, err := sheets.NewService(ctx, option.WithCredentialsFile(fileName), option.WithScopes(sheets.SpreadsheetsScope))
if err != nil {
return nil, err
}
return &SheetClient{
srv: srv,
spreadsheetID: spreadsheetID,
}, nil
}
// https://developers.google.com/sheets/api/guides/values#reading_a_single_range
func (s *SheetClient) Get(range_ string) ([][]interface{}, error) {
resp, err := s.srv.Spreadsheets.Values.Get(s.spreadsheetID, range_).Do()
if err != nil {
return nil, err
}
return resp.Values, nil
}
func Main(sheetNameRange string) [][]interface{} {
client, err := NewSheetClient(os.Getenv("SPREAD_SHEET_ID"))
if err != nil {
panic(err)
return nil
}
res, err := client.Get(sheetNameRange)
if err != nil {
panic(err)
return nil
}
return res
}