-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
67 lines (52 loc) · 1.24 KB
/
utils.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
61
62
63
64
65
66
67
package main
import (
"fmt"
"net/http"
"strconv"
gsheets "google.golang.org/api/sheets/v4"
"io.bytenix.com/gsheetcsv/sheets"
)
func GetSpreadsheetValues(c *http.Client, id string, r sheets.Range, formula bool) ([][]string, error) {
s, err := gsheets.New(c)
if err != nil {
return [][]string{}, err
}
var renderOption string
if formula {
renderOption = "FORMULA"
} else {
renderOption = "FORMATTED_VALUE"
}
cells, err := s.Spreadsheets.Values.Get(id, r.String()).ValueRenderOption(renderOption).DateTimeRenderOption("FORMATTED_STRING").Do()
if err != nil {
return [][]string{}, err
}
values := make([][]string, len(cells.Values))
for i, v := range cells.Values {
values[i], err = GetValueNStrings(v, int(r.End.Column-r.Start.Column+1))
if err != nil {
return nil, err
}
}
return values, nil
}
func GetValueNStrings(v []interface{}, l int) ([]string, error) {
s := make([]string, l)
for i := range v {
switch v[i].(type) {
case string:
s[i] = v[i].(string)
case float64:
s[i] = strconv.FormatFloat(v[i].(float64), 'f', -1, 64)
case bool:
if v[i].(bool) {
s[i] = "TRUE"
} else {
s[i] = "FALSE"
}
default:
return nil, fmt.Errorf("Cannot convert: %s", v[i])
}
}
return s, nil
}