-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
example_test.go
659 lines (555 loc) · 15.3 KB
/
example_test.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx_test
import (
"fmt"
"github.com/plandem/xlsx"
"github.com/plandem/xlsx/format/styles"
colOptions "github.com/plandem/xlsx/types/options/column"
rowOptions "github.com/plandem/xlsx/types/options/row"
sheetOptions "github.com/plandem/xlsx/types/options/sheet"
"log"
"os"
"strings"
"time"
)
// Demonstrates how to create/open/save XLSX files
func Example_files() {
// Create a new XLSX file
xl := xlsx.New()
// Open the XLSX file using file name
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Open the XLSX file using file handler
zipFile, err := os.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
xl, err = xlsx.Open(zipFile)
if err != nil {
log.Fatal(err)
}
// Update the existing XLSX file
err = xl.Save()
if err != nil {
log.Fatal(err)
}
// Save the XLSX file under different name
err = xl.SaveAs("new_file.xlsx")
if err != nil {
log.Fatal(err)
}
}
// Demonstrates how to access differ information
func Example_access() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Get sheet by 0-based index
sheet := xl.Sheet(0)
// Get cell by 0-based indexes
cell := sheet.Cell(13, 27)
fmt.Println(cell.Value())
// Get cell by reference
cell = sheet.CellByRef("N28")
fmt.Println(cell.Value())
// Get row by 0-based index
row := sheet.Row(9)
fmt.Println(strings.Join(row.Values(), ","))
// Get cell of row at 0-based col index
cell = row.Cell(0)
fmt.Println(cell.Value())
// Get col by 0-based index
col := sheet.Col(3)
fmt.Println(strings.Join(col.Values(), ","))
// Get cell of col at 0-based row index
cell = col.Cell(0)
fmt.Println(cell.Value())
// Get range by references
area := sheet.RangeByRef("D10:H13")
fmt.Println(strings.Join(area.Values(), ","))
//Output:
// last cell
// last cell
// ,,,1,2,3,4,5,,,,,,
//
// ,,,,,,,,,1,6,11,16,,,,,,,,,,,,,,,
//
// 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
}
// Demonstrates how to iterate
func Example_iterate() {
xl, err := xlsx.Open("./test_files/example_iteration.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Get sheet by 0-based index
sheet := xl.Sheet(0)
// Iterate by indexes
totalCols, totalRows := sheet.Dimension()
for rIdx := 0; rIdx < totalRows; rIdx++ {
for cIdx := 0; cIdx < totalCols; cIdx++ {
fmt.Println(sheet.Cell(cIdx, rIdx).Value())
}
}
// Iterate rows via iterator
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
for cells := row.Cells(); cells.HasNext(); {
_, _, cell := cells.Next()
fmt.Println(cell.Value())
}
}
// Iterate cols via iterator
for cols := sheet.Cols(); cols.HasNext(); {
_, col := cols.Next()
for cells := col.Cells(); cells.HasNext(); {
_, _, cell := cells.Next()
fmt.Println(cell.Value())
}
}
// Iterate range's cells via iterator
r := sheet.RangeByRef("A1:B3")
for cells := r.Cells(); cells.HasNext(); {
_, _, cell := cells.Next()
fmt.Println(cell.Value())
}
// Iterate sheets via iterator
for sheets := xl.Sheets(); sheets.HasNext(); {
_, sheet := sheets.Next()
fmt.Println(sheet.Name())
}
//Output:
//Header 1
//Header 2
//Value 1-1
//Value 2-1
//Value 1-2
//Value 2-2
//Header 1
//Header 2
//Value 1-1
//Value 2-1
//Value 1-2
//Value 2-2
//Header 1
//Value 1-1
//Value 1-2
//Header 2
//Value 2-1
//Value 2-2
//Header 1
//Header 2
//Value 1-1
//Value 2-1
//Value 1-2
//Value 2-2
//First Sheet
//Second Sheet
//Last Sheet
}
// Demonstrate walk cells using callback
func Example_walk() {
xl, err := xlsx.Open("./test_files/example_iteration.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Get sheet by 0-based index
sheet := xl.Sheet(0)
// Walk through the cells of row
row := sheet.Row(0)
row.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
fmt.Println(c.Value())
})
// Walk through the cells of col
col := sheet.Col(0)
col.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
fmt.Println(c.Value())
})
// Walk through the cells of range
area := sheet.RangeByRef("A1:B3")
area.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
fmt.Println(c.Value())
})
//Output:
//Header 1
//Header 2
//Header 1
//Value 1-1
//Value 1-2
//Header 1
//Header 2
//Value 1-1
//Value 2-1
//Value 1-2
//Value 2-2
}
// Demonstrates how to update information
func Example_update() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
// Update value of cell
cell := sheet.Cell(13, 27)
fmt.Println(cell.Value())
cell.SetValue("new value")
fmt.Println(cell.Value())
// Update value of cells in row
row := sheet.Row(9)
fmt.Println(strings.Join(row.Values(), ","))
row.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
c.SetValue(idx)
})
fmt.Println(strings.Join(row.Values(), ","))
// Update value of cells in col
col := sheet.Col(3)
fmt.Println(strings.Join(col.Values(), ","))
col.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
c.SetValue(idx)
})
fmt.Println(strings.Join(col.Values(), ","))
// Update value of cells in range
area := sheet.RangeByRef("D10:H13")
fmt.Println(strings.Join(area.Values(), ","))
area.Walk(func(idx, cIdx, rIdx int, c *xlsx.Cell) {
c.SetValue(idx)
})
fmt.Println(strings.Join(area.Values(), ","))
//Output:
// last cell
// new value
// ,,,1,2,3,4,5,,,,,,
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13
// ,,,,,,,,,3,6,11,16,,,,,,,,,,,,,,,
// 0,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
// 9,4,5,6,7,10,7,8,9,10,11,12,13,14,15,12,17,18,19,20
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
}
// Demonstrates how to add style formatting
func Example_formatting() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Create a new format for a bold font with red color and yellow solid background
redBold := styles.New(
styles.Font.Bold,
styles.Font.Color("#ff0000"),
styles.Fill.Background("#ffff00"),
styles.Fill.Type(styles.PatternTypeSolid),
)
// Add formatting to xlsx
styleID := xl.AddStyles(redBold)
sheet := xl.Sheet(0)
// Set formatting for cell
sheet.CellByRef("N28").SetStyles(styleID)
// Set DEFAULT formatting for row. Affects cells not yet allocated in the row.
// In other words, this style applies to new cells.
sheet.Row(9).SetStyles(styleID)
// Set DEFAULT formatting for col. Affects cells not yet allocated in the col.
// In other words, this style applies to new cells.
sheet.Col(3).SetStyles(styleID)
//set formatting for all cells in range
sheet.RangeByRef("D10:H13").SetStyles(styleID)
}
// Demonstrates how to set options of rows/cols/sheets
func Example_options() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
// set options for row
ro := rowOptions.New(
rowOptions.Hidden(true),
rowOptions.Height(10.0),
rowOptions.Collapsed(true),
rowOptions.Styles(styles.New(
styles.Alignment.VAlign(styles.VAlignJustify),
)),
)
sheet.Row(9).SetOptions(ro)
// set options for col
co := colOptions.New(
colOptions.Hidden(true),
colOptions.Width(10.0),
colOptions.Collapsed(true),
colOptions.Styles(styles.New(
styles.Alignment.HAlign(styles.HAlignJustify),
)),
)
sheet.Col(3).SetOptions(co)
// set options for sheet
so := sheetOptions.New(
sheetOptions.Visibility(sheetOptions.VisibilityVeryHidden),
)
sheet.SetOptions(so)
}
// Demonstrates how to append cols/rows/sheets.
func Example_append() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
// To append a new col/row, simple request it - sheet will be auto expanded.
// E.g.: we have 14 cols, 28 rows.
fmt.Println(sheet.Dimension())
// Append 72 rows
sheet.Row(99)
fmt.Println(sheet.Dimension())
// Append 36 cols
sheet.Col(49)
fmt.Println(sheet.Dimension())
// Append 3 sheet
fmt.Println(strings.Join(xl.SheetNames(), ","))
xl.AddSheet("new sheet")
xl.AddSheet("new sheet")
xl.AddSheet("new sheet")
fmt.Println(strings.Join(xl.SheetNames(), ","))
//Output:
// 14 28
// 14 100
// 50 100
// Sheet1
// Sheet1,new sheet,new sheet1,new sheet2
}
// Demonstrates how to insert cols/rows
func Example_insert() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
fmt.Println(sheet.Dimension())
fmt.Println(strings.Join(sheet.Col(3).Values(), ","))
// Insert a new col
sheet.InsertCol(3)
fmt.Println(sheet.Dimension())
fmt.Println(strings.Join(sheet.Col(3).Values(), ","))
fmt.Println(strings.Join(sheet.Col(4).Values(), ","))
// Insert a new row
fmt.Println(strings.Join(sheet.Row(9).Values(), ","))
sheet.InsertRow(3)
fmt.Println(sheet.Dimension())
fmt.Println(strings.Join(sheet.Row(9).Values(), ","))
fmt.Println(strings.Join(sheet.Row(10).Values(), ","))
//Output:
// 14 28
// ,,,,,,,,,1,6,11,16,,,,,,,,,,,,,,,
// 15 28
// ,,,,,,,,,,,,,,,,,,,,,,,,,,,
// ,,,,,,,,,1,6,11,16,,,,,,,,,,,,,,,
// ,,,,1,2,3,4,5,,,,,,
// 15 29
// ,,,,,,,,,,,,,,
// ,,,,1,2,3,4,5,,,,,,
}
// Demonstrates how to delete information
func Example_delete() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
fmt.Println(sheet.Dimension())
// Delete col
fmt.Println(strings.Join(sheet.Col(3).Values(), ","))
sheet.DeleteCol(3)
fmt.Println(sheet.Dimension())
fmt.Println(strings.Join(sheet.Col(3).Values(), ","))
// Delete row
fmt.Println(strings.Join(sheet.Row(3).Values(), ","))
sheet.DeleteRow(3)
fmt.Println(sheet.Dimension())
fmt.Println(strings.Join(sheet.Row(3).Values(), ","))
// Delete sheet
fmt.Println(strings.Join(xl.SheetNames(), ","))
xl.DeleteSheet(0)
fmt.Println(strings.Join(xl.SheetNames(), ","))
//Output:
// 14 28
// ,,,,,,,,,1,6,11,16,,,,,,,,,,,,,,,
// 13 28
// ,merged cols,,merged rows+cols,,,,,,2,7,12,17,,,,,,,,,,,,,,,
// ,,merged rows,merged rows+cols,,,,,,,,,
// 13 27
// with trailing space ,,merged rows,,,,,,,,,,
// Sheet1
//
}
// Demonstrates how to open sheet in streaming mode
func Example_streams() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
// Open sheet in stream reading mode with single phase.
// Some meta information is NOT available (e.g. merged cells).
sheet := xl.Sheet(0, xlsx.SheetModeStream)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
sheet.Close()
// Open sheet in stream reading mode with multi phases.
// Meta information is available.
sheet = xl.Sheet(0, xlsx.SheetModeStream, xlsx.SheetModeMultiPhase)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
sheet.Close()
}
// Demonstrates how to copy information in sheet
func Example_copy() {
xl, err := xlsx.Open("./test_files/example_iteration.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
// Copy row to another row with index
row := sheet.Row(0)
row.CopyTo(4, false)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
// Copy col to another col with index
col := sheet.Col(0)
col.CopyTo(3, false)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
// Copy range to another range that started at indexes
r := sheet.RangeByRef("A1:B3")
r.CopyTo(3, 0)
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
// Copy range to another range that started at ref
r.CopyToRef("I4")
for rows := sheet.Rows(); rows.HasNext(); {
_, row := rows.Next()
fmt.Println(strings.Join(row.Values(), ","))
}
//Output:
//Header 1,Header 2
//Value 1-1,Value 2-1
//Value 1-2,Value 2-2
//Header 1,Header 2
//Value 1-1,Value 2-1
//Value 1-2,Value 2-2
//,
//Header 1,Header 2
//Header 1,Header 2,,Header 1
//Value 1-1,Value 2-1,,Value 1-1
//Value 1-2,Value 2-2,,Value 1-2
//,,,
//Header 1,Header 2,,Header 1
//Header 1,Header 2,,Header 1,Header 2
//Value 1-1,Value 2-1,,Value 1-1,Value 2-1
//Value 1-2,Value 2-2,,Value 1-2,Value 2-2
//,,,,
//Header 1,Header 2,,Header 1,
//Header 1,Header 2,,Header 1,Header 2,,,,,
//Value 1-1,Value 2-1,,Value 1-1,Value 2-1,,,,,
//Value 1-2,Value 2-2,,Value 1-2,Value 2-2,,,,,
//,,,,,,,,Header 1,Header 2
//Header 1,Header 2,,Header 1,,,,,Value 1-1,Value 2-1
//,,,,,,,,Value 1-2,Value 2-2
}
// Demonstrates how to get/set value for cell
func Example_gettersAndSetters() {
xl := xlsx.New()
defer xl.Close()
sheet := xl.AddSheet("test sheet")
now, _ := time.Parse("02 Jan 06 15:04 MST", time.RFC822)
//set values by typed method
sheet.CellByRef("A1").SetText("string")
sheet.CellByRef("B1").SetInlineText("inline string")
sheet.CellByRef("C1").SetBool(true)
sheet.CellByRef("D1").SetInt(12345)
sheet.CellByRef("E1").SetFloat(123.123)
sheet.CellByRef("F1").SetDateTime(now)
sheet.CellByRef("G1").SetDate(now)
sheet.CellByRef("H1").SetTime(now)
sheet.CellByRef("I1").SetDeltaTime(now)
sheet.CellByRef("K1").SetValueWithFormat(-1234, "")
//set values by unified method
sheet.CellByRef("A2").SetValue("string")
sheet.CellByRef("B2").SetValue(true)
sheet.CellByRef("C2").SetValue(12345)
sheet.CellByRef("D2").SetValue(123.123)
sheet.CellByRef("E2").SetValue(now)
//get raw values that were set via typed setter
fmt.Println(sheet.CellByRef("A1").Value())
fmt.Println(sheet.CellByRef("B1").Value())
fmt.Println(sheet.CellByRef("C1").Value())
fmt.Println(sheet.CellByRef("D1").Value())
fmt.Println(sheet.CellByRef("E1").Value())
fmt.Println(sheet.CellByRef("F1").Value())
fmt.Println(sheet.CellByRef("G1").Value())
fmt.Println(sheet.CellByRef("H1").Value())
fmt.Println(sheet.CellByRef("I1").Value())
fmt.Println(sheet.CellByRef("K1").Value())
//get raw values that were set that via general setter
fmt.Println(sheet.CellByRef("A2").Value())
fmt.Println(sheet.CellByRef("B2").Value())
fmt.Println(sheet.CellByRef("C2").Value())
fmt.Println(sheet.CellByRef("D2").Value())
fmt.Println(sheet.CellByRef("E2").Value())
//get typed values and error if invalid type (values were set via typed setter)
_ = sheet.CellByRef("A1").String()
_ = sheet.CellByRef("B1").String()
_, _ = sheet.CellByRef("C1").Bool()
_, _ = sheet.CellByRef("D1").Int()
_, _ = sheet.CellByRef("E1").Float()
_, _ = sheet.CellByRef("F1").Date()
//get typed values and error if invalid type (values were set via general setter)
_ = sheet.CellByRef("A2").String()
_, _ = sheet.CellByRef("B2").Bool()
_, _ = sheet.CellByRef("C2").Int()
_, _ = sheet.CellByRef("D2").Float()
_, _ = sheet.CellByRef("E2").Date()
//Output:
//string
//inline string
//1
//12345
//123.123
//2006-01-02T15:04:00
//2006-01-02T15:04:00
//2006-01-02T15:04:00
//2006-01-02T15:04:00
//-1234
//string
//1
//12345
//123.123
//2006-01-02T15:04:00
}