-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable.go
More file actions
316 lines (294 loc) · 7.83 KB
/
table.go
File metadata and controls
316 lines (294 loc) · 7.83 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package werkbook
import (
"fmt"
"strconv"
"strings"
"unicode"
"github.com/jpoz/werkbook/formula"
"github.com/jpoz/werkbook/ooxml"
)
// Table models a worksheet table (ListObject).
type Table struct {
SheetName string
Name string
DisplayName string
Ref string
Columns []string
HeaderRowCount int
TotalsRowCount int
AutoFilter bool
Style *TableStyle
}
// TableStyle models <tableStyleInfo>.
type TableStyle struct {
Name string
ShowFirstColumn bool
ShowLastColumn bool
ShowRowStripes bool
ShowColumnStripes bool
}
// Tables returns the workbook's tables in workbook order.
func (f *File) Tables() []Table {
out := make([]Table, len(f.tableDefs))
for i, td := range f.tableDefs {
out[i] = td.clone()
}
return out
}
// Tables returns the tables defined on this sheet.
func (s *Sheet) Tables() []Table {
var out []Table
for _, td := range s.file.tableDefs {
if td.SheetName == s.name {
out = append(out, td.clone())
}
}
return out
}
// AddTable registers a table on the sheet and writes it as a proper OOXML table part.
func (s *Sheet) AddTable(td Table) error {
norm, info, err := s.normalizeTable(td)
if err != nil {
return err
}
s.file.tableDefs = append(s.file.tableDefs, norm)
s.file.tables = append(s.file.tables, info)
s.file.rebuildFormulaState()
return nil
}
func (s *Sheet) normalizeTable(td Table) (Table, formula.TableInfo, error) {
if td.SheetName != "" && td.SheetName != s.name {
return Table{}, formula.TableInfo{}, fmt.Errorf("table sheet %q does not match sheet %q", td.SheetName, s.name)
}
col1, row1, col2, row2, err := RangeToCoordinates(td.Ref)
if err != nil {
return Table{}, formula.TableInfo{}, fmt.Errorf("invalid table ref %q: %w", td.Ref, err)
}
width := col2 - col1 + 1
height := row2 - row1 + 1
headerRows := td.HeaderRowCount
if headerRows == 0 {
headerRows = 1
}
if headerRows < 0 || td.TotalsRowCount < 0 {
return Table{}, formula.TableInfo{}, fmt.Errorf("table row counts must be non-negative")
}
if headerRows+td.TotalsRowCount > height {
return Table{}, formula.TableInfo{}, fmt.Errorf("table ref %q is too small for header/totals rows", td.Ref)
}
name := strings.TrimSpace(td.Name)
displayName := strings.TrimSpace(td.DisplayName)
if name == "" && displayName == "" {
name = s.file.nextTableName()
displayName = name
} else if name == "" {
name = displayName
} else if displayName == "" {
displayName = name
}
if !isValidTableName(name) {
return Table{}, formula.TableInfo{}, fmt.Errorf("invalid table name %q", name)
}
if !isValidTableName(displayName) {
return Table{}, formula.TableInfo{}, fmt.Errorf("invalid table display name %q", displayName)
}
for _, existing := range s.file.tableDefs {
if strings.EqualFold(existing.Name, name) || strings.EqualFold(existing.DisplayName, displayName) {
return Table{}, formula.TableInfo{}, fmt.Errorf("table name %q already exists", displayName)
}
if existing.SheetName == s.name && refsOverlap(existing.Ref, td.Ref) {
return Table{}, formula.TableInfo{}, fmt.Errorf("table ref %q overlaps existing table %q", td.Ref, existing.DisplayName)
}
}
cols, err := s.normalizeTableColumns(td.Columns, col1, col2, row1, headerRows)
if err != nil {
return Table{}, formula.TableInfo{}, err
}
if len(cols) != width {
return Table{}, formula.TableInfo{}, fmt.Errorf("table ref %q spans %d columns, but %d columns were provided", td.Ref, width, len(cols))
}
norm := Table{
SheetName: s.name,
Name: name,
DisplayName: displayName,
Ref: td.Ref,
Columns: cols,
HeaderRowCount: headerRows,
TotalsRowCount: td.TotalsRowCount,
AutoFilter: td.AutoFilter || headerRows > 0,
Style: copyTableStyle(td.Style),
}
info := formula.TableInfo{
Name: displayName,
SheetName: s.name,
Columns: append([]string(nil), cols...),
FirstCol: col1,
FirstRow: row1,
LastCol: col2,
LastRow: row2,
HeaderRows: headerRows,
TotalRows: td.TotalsRowCount,
}
return norm, info, nil
}
func (s *Sheet) normalizeTableColumns(cols []string, firstCol, lastCol, headerRow, headerRows int) ([]string, error) {
if len(cols) == 0 {
cols = make([]string, lastCol-firstCol+1)
if headerRows > 0 {
for col := firstCol; col <= lastCol; col++ {
cols[col-firstCol] = s.tableHeaderValue(col, headerRow)
}
}
}
out := make([]string, len(cols))
seen := make(map[string]int)
for i, col := range cols {
name := strings.TrimSpace(col)
if name == "" {
name = "Column" + strconv.Itoa(i+1)
}
base := name
lower := strings.ToLower(base)
for seen[lower] > 0 {
name = fmt.Sprintf("%s_%d", base, seen[lower]+1)
lower = strings.ToLower(name)
}
seen[strings.ToLower(name)]++
out[i] = name
}
return out, nil
}
func (s *Sheet) tableHeaderValue(col, row int) string {
r, ok := s.rows[row]
if !ok {
return ""
}
c, ok := r.cells[col]
if !ok {
return ""
}
s.resolveCell(c, col, row)
switch c.value.Type {
case TypeString:
return c.value.String
case TypeNumber:
return strconv.FormatFloat(c.value.Number, 'f', -1, 64)
case TypeBool:
if c.value.Bool {
return "TRUE"
}
return "FALSE"
case TypeError:
return c.value.String
default:
return ""
}
}
func refsOverlap(a, b string) bool {
a1, ar1, a2, ar2, err := RangeToCoordinates(a)
if err != nil {
return false
}
b1, br1, b2, br2, err := RangeToCoordinates(b)
if err != nil {
return false
}
return a1 <= b2 && a2 >= b1 && ar1 <= br2 && ar2 >= br1
}
func isValidTableName(name string) bool {
if name == "" {
return false
}
if _, _, err := CellNameToCoordinates(name); err == nil {
return false
}
for i, r := range name {
if i == 0 {
if !unicode.IsLetter(r) && r != '_' {
return false
}
continue
}
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' {
return false
}
}
return true
}
func (f *File) nextTableName() string {
for i := 1; ; i++ {
name := "Table" + strconv.Itoa(i)
used := false
for _, td := range f.tableDefs {
if strings.EqualFold(td.Name, name) || strings.EqualFold(td.DisplayName, name) {
used = true
break
}
}
if !used {
return name
}
}
}
func (t Table) clone() Table {
out := t
out.Columns = append([]string(nil), t.Columns...)
out.Style = copyTableStyle(t.Style)
return out
}
func copyTableStyle(style *TableStyle) *TableStyle {
if style == nil {
return nil
}
cp := *style
return &cp
}
func tableFromData(td ooxml.TableDef, sheetName string) Table {
return Table{
SheetName: sheetName,
Name: td.Name,
DisplayName: td.DisplayName,
Ref: td.Ref,
Columns: append([]string(nil), td.Columns...),
HeaderRowCount: td.HeaderRowCount,
TotalsRowCount: td.TotalsRowCount,
AutoFilter: td.HasAutoFilter,
Style: tableStyleFromData(td.Style),
}
}
func tableStyleFromData(style *ooxml.TableStyleData) *TableStyle {
if style == nil {
return nil
}
return &TableStyle{
Name: style.Name,
ShowFirstColumn: style.ShowFirstColumn,
ShowLastColumn: style.ShowLastColumn,
ShowRowStripes: style.ShowRowStripes,
ShowColumnStripes: style.ShowColumnStripes,
}
}
func (t Table) toData(sheetIndex int) ooxml.TableDef {
var style *ooxml.TableStyleData
if t.Style != nil {
style = &ooxml.TableStyleData{
Name: t.Style.Name,
ShowFirstColumn: t.Style.ShowFirstColumn,
ShowLastColumn: t.Style.ShowLastColumn,
ShowRowStripes: t.Style.ShowRowStripes,
ShowColumnStripes: t.Style.ShowColumnStripes,
}
}
return ooxml.TableDef{
Name: t.Name,
DisplayName: t.DisplayName,
Ref: t.Ref,
SheetIndex: sheetIndex,
Columns: append([]string(nil), t.Columns...),
HeaderRowCount: t.HeaderRowCount,
TotalsRowCount: t.TotalsRowCount,
HasAutoFilter: t.AutoFilter,
HasActiveFilter: false,
Style: style,
}
}