-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheet.go
51 lines (34 loc) · 834 Bytes
/
sheet.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
package main
import (
"context"
"errors"
"google.golang.org/api/sheets/v4"
"log"
)
type sheet struct {
Name string
Id string
}
func (s *sheet) Query() ([][]interface{}, error) {
srv, err := sheets.NewService(context.TODO())
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
}
if s.Id == "" {
log.Fatal("Google Sheets ID not specified")
return nil, errors.New("Google Sheets ID not specified")
}
//NOTE: Set env variable
spreadsheetId := s.Id
readRange := "A:B"
if s.Name != "" {
readRange = s.Name + "!A:B"
}
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
if err != nil {
log.Fatalf("Unable to retrieve data from sheet: %v", err)
return nil, err
}
log.Printf("Number of quering rows: %d\n", len(resp.Values))
return resp.Values, nil
}