-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet.go
51 lines (40 loc) · 1.27 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func fetchData(ctx context.Context, logger *log.Logger, client *http.Client, spreadsheetId, readRange string) [][]interface{} {
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
logger.Printf("Unable to retrieve Sheets client: %v", err)
}
resp, err := srv.Spreadsheets.Values.Get(spreadsheetId, readRange).Do()
if err != nil {
logger.Printf("Unable to retrieve data from sheet: %v", err)
}
return resp.Values
}
func (cfg *Config) getScaleData(ctx context.Context, logger *log.Logger, client *http.Client, nats bool, commit, cluster string) []byte {
var allSheetData [][]interface{}
var sheets []string
clusters := strings.Split(cluster, ",")
for _, sheet := range clusters {
rr := fmt.Sprintf("%s!%s", sheet, cfg.SpreadSheet.ReadRange)
sheets = append(sheets, rr)
}
for _, rr := range sheets {
sheetData := fetchData(ctx, logger, client, cfg.SpreadSheet.Id, rr)
allSheetData = append(allSheetData, sheetData...)
}
data := spreadSheet2Data(allSheetData)
scaleData, err := toJson(data, nats, commit)
if err != nil {
logger.Printf("Unable to get Json data: %v", err)
}
return scaleData
}