-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_range.go
225 lines (195 loc) · 5.55 KB
/
sheet_range.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package docs
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strconv"
)
// Range reference https://open.feishu.cn/document/ukTMukTMukTM/uczNzUjL3czM14yN3MTN#bae19f77
type SheetRange struct {
Err error
leftTop *cellName
rightBottom *cellName
sheet *Sheet
}
/*
// Prepend insert data before range block.
func (s *SheetRange) Prepend() {}
// Append insert data after range block.
func (s *SheetRange) Append() {}
// Read data from the range block.
func (s *SheetRange) Read() {}
// Write data to the range block.
func (s *SheetRange) Write() {}
// SetBold
func (s *SheetRange) SetBold(bold bool) {}
// SetItalic
func (s *SheetRange) SetItalic(set bool) {}
// SetFontSize
func (s *SheetRange) SetFontSize(set bool) {}
// SetTextDecoration
func (s *SheetRange) SetTextDecoration(set bool) {}
// SetHorizontalAlign
func (s *SheetRange) SetHorizontalAlign(set bool) {}
// SetVerticalAlign
func (s *SheetRange) SetVerticalAlign(set bool) {}
// SetFontColor
func (s *SheetRange) SetFontColor(set bool) {}
// SetBackgroudColor
func (s *SheetRange) SetBackgroudColor(set bool) {}
// SetBorder
func (s *SheetRange) SetBorder(borderType string, borderColor string) {}
// Clean all the style
func (s *SheetRange) Clean() {}
// Merge
func (s *SheetRange) Merge(mergeType string) {}
// Unmerge
func (s *SheetRange) Unmerge() {}
func (s *SheetRange) Find(keyword string, matchCase, matchEntireCell, regex, includeFormulas bool) {
}
func (s *SheetRange) Replace() {}
*/
// SetDropdown
// Parameter:
// colors: set highlight color of values, could be nil. values like #1FB6C1
// Reference:
// https://open.feishu.cn/document/ukTMukTMukTM/uATMzUjLwEzM14CMxMTN/datavalidation/set-dropdown
func (s *SheetRange) SetDropdown(values []string, multiple bool, colors []string) error {
body, _ := json.Marshal(map[string]interface{}{
"range": s.genRangeStr(),
"dataValidationType": "list",
"dataValidation": map[string]interface{}{
"conditionValues": values,
"options": map[string]interface{}{
"multipleValues": multiple,
"highlightValidData": len(colors) > 0,
"colors": colors,
},
},
})
_url := s.sheet.ssClient.baseClient.urlJoin("open-apis/sheets/v2/spreadsheets/" + s.sheet.ssClient.GetToken() + "/dataValidation")
req, _ := http.NewRequest(http.MethodPost, _url, bytes.NewReader(body))
_, err := s.sheet.ssClient.baseClient.CommonReq(req, nil)
return err
}
// Rows ...
func (s *SheetRange) Rows() ([]SheetRow, error) {
if s.Err != nil {
return nil, s.Err
}
content, err := s.sheet.getContentByRange(s.genRangeStr())
if err != nil {
return nil, err
}
return content.ToRows(), nil
}
// RowsParseMerge
// parse cell from merged cell.
// if a cell is merged, like below, we only get value at the first cell, others are nil.
// so we should fill a to every cell of the merged cell.
// | a | | |
// | | | |
// | | | |
func (s *SheetRange) RowsParseMerge() ([]SheetRow, error) {
meta, err := s.sheet.getMeta()
if err != nil {
return nil, err
}
cells, err := s.Rows()
if err != nil {
return nil, err
}
for _, v := range meta.Merges {
startCol := v.StartColumnIndex
endCol := v.StartColumnIndex + v.ColumnCount - 1
startRow := v.StartRowIndex
endRow := v.StartRowIndex + v.RowCount - 1
for i := startRow; i <= endRow; i++ {
for j := startCol; j <= endCol; j++ {
cells[i][j] = cells[startRow][startCol]
}
}
}
return cells, nil
}
func (s *SheetRange) Scan(ptr interface{}) error {
rows, err := s.RowsParseMerge()
if err != nil {
return err
}
return s.scan(rows, ptr)
}
func (s *SheetRange) scan(rows []SheetRow, ptr interface{}) error {
// check it args is a pointer
rv := reflect.ValueOf(ptr)
if rv.Kind() != reflect.Ptr {
return fmt.Errorf("ptr is must a slice pointer")
}
if rv.CanSet() {
return fmt.Errorf("can not get slice address")
}
// dereference the pointer to get the slice
rv = rv.Elem()
if rv.Kind() != reflect.Slice {
return fmt.Errorf("ptr element is must a slice")
}
// slice element
elemt := rv.Type().Elem()
if elemt.Kind() != reflect.Ptr {
return fmt.Errorf("slice element must be a struct pointer, it is not pointer")
}
elemt = elemt.Elem()
if elemt.Kind() != reflect.Struct {
return fmt.Errorf("slice element must be a struct pointer, it it not struct")
}
fieldPosition := map[int]int{}
for i := 0; i < elemt.NumField(); i++ {
str := elemt.Field(i).Tag.Get("docs")
if str != "" {
v, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return fmt.Errorf("struct tag wrong, it is not a number, field index, %d, tag: %s", i, str)
}
fieldPosition[i] = int(v)
} else {
fieldPosition[i] = i
}
}
for _, row := range rows {
// new a slice element
valP := reflect.New(elemt)
// dereference pointer
val := valP.Elem()
for j := 0; j < val.NumField(); j++ {
name := elemt.Field(j).Name
position := fieldPosition[j]
f := val.Field(j)
if !f.CanAddr() {
fmt.Printf(": can not set, %s\n", name)
}
switch f.Kind() {
case reflect.Int64, reflect.Int:
to, err := row[position].ToInt64()
if err != nil {
return fmt.Errorf("scan faild, index: %d, %w", j, err)
}
f.SetInt(to)
case reflect.Float64, reflect.Float32:
to, err := row[position].ToFloat()
if err != nil {
return fmt.Errorf("scan faild, index: %d, %w", j, err)
}
f.SetFloat(to)
default:
f.SetString(row[position].ToString())
}
}
rv.Set(reflect.Append(rv, valP))
}
return nil
}
func (s *SheetRange) genRangeStr() string {
return s.sheet.id + "!" + s.leftTop.String() + ":" + s.rightBottom.String()
}