Repository navigation
Expand file tree
/
Copy pathsheet.go
More file actions
142 lines (126 loc) · 3.79 KB
/
Copy pathsheet.go
File metadata and controls
142 lines (126 loc) · 3.79 KB
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
package spreadsheet
import "encoding/json"
// Sheet is a sheet in a spreadsheet.
type Sheet struct {
Properties SheetProperties `json:"properties"`
Data SheetData `json:"data"`
// Merges []*GridRange `json:"merges"`
// ConditionalFormats []*ConditionalFormatRule `json:"conditionalFormats"`
// FilterViews []*FilterView `json:"filterViews"`
// ProtectedRanges []*ProtectedRange `json:"protectedRanges"`
// BasicFilter *BasicFilter `json:"basicFilter"`
// Charts []*EmbeddedChart `json:"charts"`
// BandedRanges []*BandedRange `json:"bandedRanges"`
Spreadsheet *Spreadsheet `json:"-"`
Rows [][]Cell `json:"-"`
Columns [][]Cell `json:"-"`
modifiedCells []*Cell
newMaxRow uint
newMaxColumn uint
}
// UnmarshalJSON embeds rows and columns to the sheet.
func (sheet *Sheet) UnmarshalJSON(data []byte) error {
type Alias Sheet
a := (*Alias)(sheet)
if err := json.Unmarshal(data, a); err != nil {
return err
}
var maxRow, maxColumn int
cells := []Cell{}
for _, gridData := range sheet.Data.GridData {
for rowNum, row := range gridData.RowData {
for columnNum, cellData := range row.Values {
r := gridData.StartRow + uint(rowNum)
if int(r) > maxRow {
maxRow = int(r)
}
c := gridData.StartColumn + uint(columnNum)
if int(c) > maxColumn {
maxColumn = int(c)
}
cell := Cell{
Row: r,
Column: c,
Value: cellData.FormattedValue,
}
cells = append(cells, cell)
}
}
}
sheet.Rows, sheet.Columns = newCells(uint(maxRow), uint(maxColumn))
for _, cell := range cells {
sheet.Rows[cell.Row][cell.Column] = cell
sheet.Columns[cell.Column][cell.Row] = cell
}
sheet.modifiedCells = []*Cell{}
sheet.newMaxRow = sheet.Properties.GridProperties.RowCount
sheet.newMaxColumn = sheet.Properties.GridProperties.ColumnCount
return nil
}
// Update updates cell changes
func (sheet *Sheet) Update(row, column int, val string) {
if uint(row)+1 > sheet.newMaxRow {
sheet.newMaxRow = uint(row) + 1
}
if uint(column)+1 > sheet.newMaxColumn {
sheet.newMaxColumn = uint(column) + 1
}
if uint(len(sheet.Rows)) < sheet.newMaxRow+1 ||
uint(len(sheet.Columns)) < sheet.newMaxColumn+1 {
newRows, newColumns := newCells(sheet.newMaxRow, sheet.newMaxColumn)
for i := range sheet.Rows {
copy(newRows[i], sheet.Rows[i])
}
for i := range sheet.Columns {
copy(newColumns[i], sheet.Columns[i])
}
sheet.Rows = newRows
sheet.Columns = newColumns
}
cell := Cell{
Row: uint(row),
Column: uint(column),
Value: val,
}
sheet.Rows[row][column] = cell
sheet.Columns[column][row] = cell
for _, cell := range sheet.modifiedCells {
if cell.Row == uint(row) && cell.Column == uint(column) {
cell.Value = val
return
}
}
sheet.modifiedCells = append(sheet.modifiedCells, &cell)
}
// DeleteRows deletes rows from the sheet
func (sheet *Sheet) DeleteRows(start, end int) (err error) {
err = sheet.Spreadsheet.service.DeleteRows(sheet, start, end)
return
}
// DeleteColumns deletes columns from the sheet
func (sheet *Sheet) DeleteColumns(start, end int) (err error) {
err = sheet.Spreadsheet.service.DeleteColumns(sheet, start, end)
return
}
// Synchronize reflects the changes of the sheet.
func (sheet *Sheet) Synchronize() (err error) {
err = sheet.Spreadsheet.service.SyncSheet(sheet)
return
}
func newCells(maxRow, maxColumn uint) (rows, columns [][]Cell) {
rows = make([][]Cell, maxRow+1)
for i := uint(0); i < maxRow+1; i++ {
rows[i] = make([]Cell, 0, maxColumn+1)
for t := uint(0); t < maxColumn+1; t++ {
rows[i] = append(rows[i], Cell{Row: i, Column: t})
}
}
columns = make([][]Cell, maxColumn+1)
for i := uint(0); i < maxColumn+1; i++ {
columns[i] = make([]Cell, 0, maxRow+1)
for t := uint(0); t < maxRow+1; t++ {
columns[i] = append(columns[i], Cell{Row: t, Column: i})
}
}
return
}