-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet.go
59 lines (42 loc) · 1.3 KB
/
spreadsheet.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
package spreadsheet
import (
"context"
"io/ioutil"
"github.com/kangata/gonotes/internal/client"
"github.com/kangata/gonotes/internal/env"
"github.com/kangata/gonotes/models"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
type Spreadsheet struct {
SpreadsheetID string
WriteRange string
ValueRange sheets.ValueRange
Service sheets.Service
}
func (s *Spreadsheet) NewService() (*sheets.Service, error) {
s.SpreadsheetID = env.Get("SPREADSHEET_ID")
s.WriteRange = "A1"
ctx := context.Background()
contentBytes, err := ioutil.ReadFile("storage/credentials.json")
if err != nil {
return nil, err
}
config, err := google.ConfigFromJSON(contentBytes, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return nil, err
}
client := client.GetClient(config)
Service, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, err
}
s.Service = *Service
return &s.Service, nil
}
func (s *Spreadsheet) AddItem(m models.Message) (*sheets.AppendValuesResponse, error) {
valueRange := sheets.ValueRange{}
valueRange.Values = append(valueRange.Values, m.ToSheetRow())
return s.Service.Spreadsheets.Values.Append(s.SpreadsheetID, s.WriteRange, &valueRange).ValueInputOption("RAW").Do()
}