-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
60 lines (51 loc) · 1.43 KB
/
client.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 herschel
import (
"github.com/pkg/errors"
"github.com/yokoe/herschel/option"
sheets "google.golang.org/api/sheets/v4"
)
// Client provides methods to manipulate spreadsheets.
type Client struct {
service *sheets.Service
}
// NewClient returns a new instance
func NewClient(option option.ClientOption) (*Client, error) {
client, err := option.GetClient()
if err != nil {
return nil, errors.Wrap(err, "failed to get client from option")
}
service, err := sheets.New(client)
if err != nil {
return nil, errors.Wrap(err, "failed to create service with client")
}
return &Client{service: service}, nil
}
/*
* Low-level Spreadsheet api calls
*/
func (c Client) updateCellValues(spreadsheetID string, sheetName string, values [][]interface{}) error {
if c.service == nil {
return errors.New("service not initiallized")
}
if _, err := c.service.Spreadsheets.Values.Update(spreadsheetID, sheetName, &sheets.ValueRange{
MajorDimension: "ROWS",
Values: values,
}).ValueInputOption("USER_ENTERED").Do(); err != nil {
return err
}
return nil
}
func (c Client) batchUpdate(spreadsheetID string, requests []*sheets.Request) error {
if c.service == nil {
return errors.New("service not initiallized")
}
if len(requests) == 0 {
return nil
}
if _, err := c.service.Spreadsheets.BatchUpdate(spreadsheetID, &sheets.BatchUpdateSpreadsheetRequest{
Requests: requests,
}).Do(); err != nil {
return err
}
return nil
}