-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathspreadsheet.go
66 lines (59 loc) · 1.58 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
package spreadsheet
import (
"encoding/json"
"errors"
)
// Spreadsheet represents a spreadsheet.
type Spreadsheet struct {
ID string `json:"spreadsheetId"`
Properties Properties `json:"properties"`
Sheets []Sheet `json:"sheets"`
// NamedRanges []*NamedRange `json:"namedRanges"`
service *Service
cached bool
}
// UnmarshalJSON embeds spreadsheet to sheets.
func (spreadsheet *Spreadsheet) UnmarshalJSON(data []byte) error {
type Alias Spreadsheet
a := (*Alias)(spreadsheet)
if err := json.Unmarshal(data, a); err != nil {
return err
}
for i := range spreadsheet.Sheets {
spreadsheet.Sheets[i].Spreadsheet = spreadsheet
}
return nil
}
// SheetByIndex gets a sheet by the given index.
func (spreadsheet *Spreadsheet) SheetByIndex(index uint) (sheet *Sheet, err error) {
for i, s := range spreadsheet.Sheets {
if s.Properties.Index == index {
sheet = &spreadsheet.Sheets[i]
return
}
}
err = errors.New("sheet not found by the index")
return
}
// SheetByID gets a sheet by the given ID.
func (spreadsheet *Spreadsheet) SheetByID(id uint) (sheet *Sheet, err error) {
for i, s := range spreadsheet.Sheets {
if s.Properties.ID == id {
sheet = &spreadsheet.Sheets[i]
return
}
}
err = errors.New("sheet not found by the id")
return
}
// SheetByTitle gets a sheet by the given title.
func (spreadsheet *Spreadsheet) SheetByTitle(title string) (sheet *Sheet, err error) {
for i, s := range spreadsheet.Sheets {
if s.Properties.Title == title {
sheet = &spreadsheet.Sheets[i]
return
}
}
err = errors.New("sheet not found by the title")
return
}