forked from deejiw/xk6-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets_helper.go
127 lines (111 loc) · 3.48 KB
/
sheets_helper.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package gcp
import (
"fmt"
"log"
"strconv"
"strings"
)
// This function generates a unique ID for a new row in a Google Sheet.
// Parameters:
// - rows: a slice of slices of interface{} representing the rows of a Google Sheet.
// Returns:
// - string: a unique ID for a new row.
func getUniqueId(rows [][]interface{}) int64 {
var id int64
if len(rows) > 1 {
lastID, err := strconv.ParseInt(rows[len(rows)-1][0].(string), 10, 64)
if err != nil {
log.Fatalf("unable to parse the last ID from last row as %s <%v>.", rows[len(rows)-1], err)
}
id = lastID + 1
} else {
id = 1
}
return id
}
// This function merges two slices of interface{} into a map[string]interface{}.
// Parameters:
// - keys: a slice of interface{} representing the keys of the map.
// - values: a slice of interface{} representing the values of the map.
// Returns:
// - map[string]interface{}: a map with keys and values from the input slices.
func mergeKV(keys []interface{}, values []interface{}) map[string]interface{} {
mergedMap := make(map[string]interface{})
// Check if the lengths of keys and values arrays are the same
if len(keys) == len(values) {
// Iterate over the keys and values arrays
for i := 0; i < len(keys); i++ {
key := keys[i]
value := values[i]
mergedMap[key.(string)] = value
}
} else {
fmt.Println("error: length of keys and values arrays must be the same.")
}
return mergedMap
}
// This function converts a column index to its corresponding letter in a Google Sheet.
// Parameters:
// - index: an integer representing the column index.
// Returns:
// - string: a string representing the corresponding letter of the column index.
func columnIndexToLetter(index int) string {
var result string
for index >= 0 {
result = string(rune(index%26+65)) + result
index = index/26 - 1
}
return result
}
// This function finds the index of a header in a slice of headers.
// Parameters:
// - headers: a slice of interface{} representing the headers.
// - header: a string representing the header to find.
// Returns:
// - int: the index of the header in the slice, or -1 if not found.
func findHeaderIndex(headers []interface{}, header string) int {
for i, h := range headers {
if strings.TrimSpace(h.(string)) == header {
return i
}
}
return -1
}
// This function sorts values by headers.
// Parameters:
// - headers: a slice of interface{} representing the headers.
// - values: a map[string]interface{} representing the values to sort.
// Returns:
// - []interface{}: a slice of interface{} values sorted by the headers.
func sortValuesByHeaders(headers []interface{}, values map[string]interface{}) []interface{} {
headerMap := make(map[string]int)
for i, header := range headers {
headerMap[header.(string)] = i
}
// Create the new row data in the correct order
sorted := make([]interface{}, len(headers))
for columnName, value := range values {
index, found := headerMap[columnName]
if !found {
log.Fatalf("column '%s' not found in the sheet", columnName)
}
sorted[index] = value
}
return sorted
}
// func getSheetName(c *sheets.Service, spreadsheetId string, sheetId int) string {
// res, err := c.Spreadsheets.Get(spreadsheetId).Fields("sheets(properties(sheetId,title))").Do()
// if err != nil || res.HTTPStatusCode != 200 {
// log.Println(err)
// return ""
// }
// sheetName := ""
// for _, v := range res.Sheets {
// prop := v.Properties
// if prop.SheetId == int64(sheetId) {
// sheetName = prop.Title
// break
// }
// }
// return sheetName
// }