/
sheets.go
57 lines (51 loc) · 1.96 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
package google
import (
"context"
"fmt"
"github.com/caarlos0/env"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
// Creates default sheets config
//
// GOOGLE_APPLICATION_CREDENTIALS - (mandatory) path to service json file (https://cloud.google.com/docs/authentication/production#creating_a_service_account)
func DefaultSheets() *SheetsConfig {
cfg := &SheetsConfig{}
if err := env.Parse(cfg); err != nil {
panic(err)
}
if cfg.CredentialsFile == "" {
panic("credentials file location required")
}
return cfg
}
// Google spread sheets config.
//
// Don't forget to share your spreadsheet with service account email
type SheetsConfig struct {
CredentialsFile string `env:"GOOGLE_APPLICATION_CREDENTIALS"` // (mandatory) path to service json file (https://cloud.google.com/docs/authentication/production#creating_a_service_account)
SheetId string `env:"GOOGLE_SHEET_ID"` // default spreadsheet id
}
// Append values as row to first empty row to default sheet
func (cfg SheetsConfig) AppendRow(values ...interface{}) error {
return cfg.AppendRowToContext(context.Background(), cfg.SheetId, "A1", values...)
}
// Append values as row to first empty row to default sheet
func (cfg SheetsConfig) AppendRowContext(ctx context.Context, values ...interface{}) error {
return cfg.AppendRowToContext(ctx, cfg.SheetId, "A1", values...)
}
// Append row to google spread sheets
func (cfg SheetsConfig) AppendRowToContext(ctx context.Context, sheetID, appendRange string, values ...interface{}) error {
srv, err := sheets.NewService(ctx, option.WithCredentialsFile(cfg.CredentialsFile))
if err != nil {
return fmt.Errorf("sheets - append row: initialize google service: %w", err)
}
vals := sheets.ValueRange{
Values: [][]interface{}{values},
}
_, err = srv.Spreadsheets.Values.Append(sheetID, appendRange, &vals).ValueInputOption("USER_ENTERED").Do()
if err != nil {
return fmt.Errorf("sheets - append row: %w", err)
}
return nil
}