-
Notifications
You must be signed in to change notification settings - Fork 5
/
spreadsheet.go
90 lines (76 loc) · 2.71 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
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
package sheets
// SheetType represents the type of a Sheet.
type SheetType string
const (
// Grid is a Sheet type.
Grid SheetType = "GRID"
)
type (
// Spreadsheet holds a spreadsheet's fields.
Spreadsheet struct {
ID string `json:"spreadsheetId"`
Properties SpreadsheetProperties `json:"properties"`
Sheets []Sheet `json:"sheets"`
NamedRanges []NamedRange `json:"namedRanges"`
URL string `json:"spreadsheetUrl"`
}
// SpreadsheetProperties holds the properties of a spreadsheet.
SpreadsheetProperties struct {
Title string `json:"title"`
Locale string `json:"locale"`
Timezone string `json:"timeZone"`
}
// Sheet holds the sheet fields.
Sheet struct {
Properties SheetProperties `json:"properties"`
}
// SheetProperties holds the properties of a sheet.
SheetProperties struct {
ID string `json:"sheetId"`
Title string `json:"title"`
Index int `json:"index"`
SheetType SheetType `json:"sheetType"`
Grid SheetGrid `json:"gridProperties,omitempty"`
}
// SheetGrid represents the `Grid` field of `SheetProperties`.
SheetGrid struct {
RowCount int `json:"rowCount"`
ColumnCount int `json:"columnCount"`
FrozenRowCount int `json:"frozenRowCount"`
}
// NamedRange represents the namedRange of a request.
NamedRange struct {
ID string `json:"namedRangeId"`
Name string `json:"name"`
Range Range `json:"range"`
}
// Range holds the range request and response values.
Range struct {
SheetID string `json:"sheetId"`
StartRowIndex int `json:"startRowIndex"`
EndRowIndex int `json:"endRowIndex"`
StartColumnIndex int `json:"startColumnIndex"`
EndColumnIndex int `json:"endColumnIndex"`
}
// BatchUpdateResponse is the response when a batch update request is fired on a spreadsheet.
BatchUpdateResponse struct {
// SpreadsheetID is the spreadsheet the updates were applied to.
SpreadsheetID string `json:"spreadsheetId,omitempty"`
// UpdatedSpreadsheet: The spreadsheet after updates were applied.
// UpdatedSpreadsheet *Spreadsheet `json:"updatedSpreadsheet,omitempty"`
}
)
// RangeAll returns a data range text which can be used to fetch all rows of a sheet.
func (s *Sheet) RangeAll() string {
// To return all values we use the sheet's title as the range, so we return that one here.
return "'" + s.Properties.Title + "'"
}
// GetSheet finds and returns a sheet based on its "title" inside the "sd" Spreadsheet value.
func (sd *Spreadsheet) GetSheet(title string) (Sheet, bool) {
for _, s := range sd.Sheets {
if s.Properties.Title == title || s.Properties.ID == title {
return s, true
}
}
return Sheet{}, false
}