-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkbook.go
More file actions
292 lines (267 loc) · 7.64 KB
/
workbook.go
File metadata and controls
292 lines (267 loc) · 7.64 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
package gospreadsheet
import (
"errors"
"fmt"
)
// DocumentProperties holds metadata about the spreadsheet.
type DocumentProperties struct {
Creator string
LastModifiedBy string
Title string
Subject string
Description string
Keywords string
Category string
}
// Workbook represents a spreadsheet workbook containing one or more worksheets.
type Workbook struct {
worksheets []*Worksheet
activeSheet int
Properties DocumentProperties
namedRanges map[string]string // name -> "Sheet1!A1:B2"
workbookProtection *WorkbookProtection
}
// New creates a new workbook with a single default worksheet.
func New() *Workbook {
wb := &Workbook{
worksheets: make([]*Worksheet, 0),
namedRanges: make(map[string]string),
}
ws := NewWorksheet("Sheet1")
ws.parent = wb
ws.index = 0
wb.worksheets = append(wb.worksheets, ws)
return wb
}
// NewEmpty creates a new workbook with no worksheets.
func NewEmpty() *Workbook {
return &Workbook{
worksheets: make([]*Worksheet, 0),
namedRanges: make(map[string]string),
}
}
// AddSheet adds a new worksheet with the given title.
func (wb *Workbook) AddSheet(title string) (*Worksheet, error) {
for _, ws := range wb.worksheets {
if ws.title == title {
return nil, fmt.Errorf("worksheet with title %q already exists", title)
}
}
ws := NewWorksheet(title)
ws.parent = wb
ws.index = len(wb.worksheets)
wb.worksheets = append(wb.worksheets, ws)
return ws, nil
}
// GetSheet returns the worksheet at the given index (0-based).
func (wb *Workbook) GetSheet(index int) (*Worksheet, error) {
if index < 0 || index >= len(wb.worksheets) {
return nil, fmt.Errorf("sheet index %d out of range (0-%d)", index, len(wb.worksheets)-1)
}
return wb.worksheets[index], nil
}
// GetSheetByName returns the worksheet with the given title.
func (wb *Workbook) GetSheetByName(title string) (*Worksheet, error) {
for _, ws := range wb.worksheets {
if ws.title == title {
return ws, nil
}
}
return nil, fmt.Errorf("worksheet %q not found", title)
}
// SheetCount returns the number of worksheets.
func (wb *Workbook) SheetCount() int {
return len(wb.worksheets)
}
// GetSheetNames returns the titles of all worksheets.
func (wb *Workbook) GetSheetNames() []string {
names := make([]string, len(wb.worksheets))
for i, ws := range wb.worksheets {
names[i] = ws.title
}
return names
}
// RemoveSheet removes the worksheet at the given index.
func (wb *Workbook) RemoveSheet(index int) error {
if index < 0 || index >= len(wb.worksheets) {
return fmt.Errorf("sheet index %d out of range", index)
}
if len(wb.worksheets) <= 1 {
return errors.New("cannot remove the last worksheet")
}
wb.worksheets = append(wb.worksheets[:index], wb.worksheets[index+1:]...)
// Re-index
for i, ws := range wb.worksheets {
ws.index = i
}
if wb.activeSheet >= len(wb.worksheets) {
wb.activeSheet = len(wb.worksheets) - 1
}
return nil
}
// GetActiveSheet returns the active worksheet.
func (wb *Workbook) GetActiveSheet() *Worksheet {
if len(wb.worksheets) == 0 {
return nil
}
return wb.worksheets[wb.activeSheet]
}
// SetActiveSheet sets the active worksheet by index.
func (wb *Workbook) SetActiveSheet(index int) error {
if index < 0 || index >= len(wb.worksheets) {
return fmt.Errorf("sheet index %d out of range", index)
}
wb.activeSheet = index
return nil
}
// AddNamedRange adds a named range to the workbook.
func (wb *Workbook) AddNamedRange(name, reference string) {
wb.namedRanges[name] = reference
}
// GetNamedRange returns the reference for a named range.
func (wb *Workbook) GetNamedRange(name string) (string, error) {
ref, ok := wb.namedRanges[name]
if !ok {
return "", fmt.Errorf("named range %q not found", name)
}
return ref, nil
}
// GetNamedRanges returns all named ranges.
func (wb *Workbook) GetNamedRanges() map[string]string {
result := make(map[string]string, len(wb.namedRanges))
for k, v := range wb.namedRanges {
result[k] = v
}
return result
}
// RemoveNamedRange removes a named range by name.
func (wb *Workbook) RemoveNamedRange(name string) error {
if _, ok := wb.namedRanges[name]; !ok {
return fmt.Errorf("named range %q not found", name)
}
delete(wb.namedRanges, name)
return nil
}
// Sheets returns all worksheets.
func (wb *Workbook) Sheets() []*Worksheet {
result := make([]*Worksheet, len(wb.worksheets))
copy(result, wb.worksheets)
return result
}
// SetWorkbookProtection sets the workbook protection.
func (wb *Workbook) SetWorkbookProtection(wp *WorkbookProtection) {
wb.workbookProtection = wp
}
// GetWorkbookProtection returns the workbook protection, or nil if not set.
func (wb *Workbook) GetWorkbookProtection() *WorkbookProtection {
return wb.workbookProtection
}
// ClearProtection removes all workbook protection.
func (wb *Workbook) ClearProtection() {
wb.workbookProtection = nil
}
// Validate checks the workbook for common issues.
func (wb *Workbook) Validate() error {
if len(wb.worksheets) == 0 {
return errors.New("workbook must contain at least one worksheet")
}
// Check for duplicate sheet names
names := make(map[string]bool)
for _, ws := range wb.worksheets {
if names[ws.title] {
return fmt.Errorf("duplicate worksheet name: %q", ws.title)
}
names[ws.title] = true
if err := ws.Validate(); err != nil {
return fmt.Errorf("worksheet %q: %w", ws.title, err)
}
}
return nil
}
// Close is a no-op for in-memory workbooks but provided for API compatibility.
// It can be used to signal that the workbook is no longer needed.
func (wb *Workbook) Close() error {
return nil
}
// CopySheet copies a worksheet to a new sheet with the given name.
func (wb *Workbook) CopySheet(srcIndex int, newTitle string) (*Worksheet, error) {
src, err := wb.GetSheet(srcIndex)
if err != nil {
return nil, err
}
dst, err := wb.AddSheet(newTitle)
if err != nil {
return nil, err
}
// Copy cells
for key, cell := range src.cells {
newCell := &Cell{
Value: cell.Value,
Type: cell.Type,
Formula: cell.Formula,
row: cell.row,
col: cell.col,
}
if cell.Style != nil {
styleCopy := *cell.Style
newCell.Style = &styleCopy
}
if cell.Hyperlink != nil {
hlCopy := *cell.Hyperlink
newCell.Hyperlink = &hlCopy
}
if cell.Comment != nil {
cmCopy := *cell.Comment
newCell.Comment = &cmCopy
}
dst.cells[key] = newCell
}
// Copy merge cells
dst.mergeCells = make([]MergeCell, len(src.mergeCells))
copy(dst.mergeCells, src.mergeCells)
// Copy dimensions
for k, v := range src.colWidths {
dst.colWidths[k] = v
}
for k, v := range src.rowHeights {
dst.rowHeights[k] = v
}
for k, v := range src.hiddenRows {
dst.hiddenRows[k] = v
}
for k, v := range src.hiddenCols {
dst.hiddenCols[k] = v
}
// Copy frozen pane
if src.frozen != nil {
frozenCopy := *src.frozen
dst.frozen = &frozenCopy
}
dst.tabColor = src.tabColor
return dst, nil
}
// MoveSheet moves a worksheet from one index to another.
func (wb *Workbook) MoveSheet(fromIndex, toIndex int) error {
if fromIndex < 0 || fromIndex >= len(wb.worksheets) {
return fmt.Errorf("source index %d out of range", fromIndex)
}
if toIndex < 0 || toIndex >= len(wb.worksheets) {
return fmt.Errorf("destination index %d out of range", toIndex)
}
if fromIndex == toIndex {
return nil
}
ws := wb.worksheets[fromIndex]
// Remove from old position
wb.worksheets = append(wb.worksheets[:fromIndex], wb.worksheets[fromIndex+1:]...)
// Insert at new position
rear := make([]*Worksheet, len(wb.worksheets[toIndex:]))
copy(rear, wb.worksheets[toIndex:])
wb.worksheets = append(wb.worksheets[:toIndex], ws)
wb.worksheets = append(wb.worksheets, rear...)
// Re-index
for i, w := range wb.worksheets {
w.index = i
}
return nil
}