-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
2460 lines (2339 loc) · 84.4 KB
/
main.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package excelize-py is a Python port of Go Excelize library, providing a set
// of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM
// / XLTX files. Supports reading and writing spreadsheet documents generated
// by Microsoft Excel™ 2007 and later. Supports complex components by high
// compatibility, and provided streaming API for generating or reading data from
// a worksheet with huge amounts of data. This library needs Python version 3.9
// or later.
package main
/*
#include "types_c.h"
*/
import "C"
import (
"bytes"
"errors"
"reflect"
"sync"
"time"
"unicode"
"unsafe"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/tiff"
"github.com/xuri/excelize/v2"
)
const (
Nil C.int = 0
Int C.int = 1
String C.int = 2
Float C.int = 3
Boolean C.int = 4
Time C.int = 5
)
var (
files, sw = sync.Map{}, sync.Map{}
emptyString string
errFilePtr = "can not find file pointer"
errStreamWriterPtr = "can not find stream writer pointer"
errArgType = errors.New("invalid argument data type")
// goBaseTypes defines Go's basic data types.
goBaseTypes = map[reflect.Kind]bool{
reflect.Bool: true,
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Uintptr: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.Map: true,
reflect.String: true,
}
// cToBaseGoTypeFuncs defined functions mapping for G to Go basic data types
// convention.
cToBaseGoTypeFuncs = map[reflect.Kind]func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error){
reflect.Bool: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(cVal.Bool()), nil
},
reflect.Uint: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(uint(cVal.Interface().(C.uint))), nil
},
reflect.Uint8: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(uint8(cVal.Interface().(C.uchar))), nil
},
reflect.Uint64: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(uint64(cVal.Interface().(C.uint))), nil
},
reflect.Int: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(int(cVal.Int())), nil
},
reflect.Int64: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(cVal.Int()), nil
},
reflect.Float64: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(float64(cVal.Float())), nil
},
reflect.String: func(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
if cVal.Elem().CanAddr() {
return reflect.ValueOf(C.GoString(cVal.Interface().(*C.char))), nil
}
return reflect.ValueOf(""), nil
},
}
// goBaseValueToCFuncs defined functions mapping for Go basic data types
// value to C convention.
goBaseValueToCFuncs = map[reflect.Kind]func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error){
reflect.Bool: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C._Bool(goVal.Bool())), nil
},
reflect.Uint: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.uint(uint32(goVal.Uint()))), nil
},
reflect.Uint8: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.uchar(int8(goVal.Uint()))), nil
},
reflect.Uint32: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.uint(uint32(goVal.Uint()))), nil
},
reflect.Uint64: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.ulong(goVal.Uint())), nil
},
reflect.Int: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.int(int32(goVal.Int()))), nil
},
reflect.Int32: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.long(int32(goVal.Int()))), nil
},
reflect.Int64: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.longlong(int64(goVal.Int()))), nil
},
reflect.Float64: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.double(goVal.Float())), nil
},
reflect.String: func(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
return reflect.ValueOf(C.CString(goVal.String())), nil
},
}
)
// cToGoBaseType convert JavaScript value to Go basic data type variable.
func cToGoBaseType(cVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
fn, ok := cToBaseGoTypeFuncs[kind]
if !ok {
return reflect.ValueOf(nil), errArgType
}
return fn(cVal, kind)
}
// cToGoArray convert C language array to Go array base on the given Go structure
// types.
func cToGoArray(cArray reflect.Value, cArrayLen int) reflect.Value {
switch cArray.Elem().Type().String() {
case "*main._Ctype_char":
return reflect.ValueOf(append([]*C.char{}, unsafe.Slice(cArray.Interface().(**C.char), cArrayLen)...))
case "*main._Ctype_struct_Options": // []*excelize.Options
val := cArray.Interface().(**C.struct_Options)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_Border":
val := cArray.Interface().(*C.struct_Border)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_ChartSeries":
val := cArray.Interface().(*C.struct_ChartSeries)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_ConditionalFormatOptions":
val := cArray.Interface().(*C.struct_ConditionalFormatOptions)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_PivotTableField":
val := cArray.Interface().(*C.struct_PivotTableField)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_RichTextRun":
val := cArray.Interface().(*C.struct_RichTextRun)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
case "main._Ctype_struct_Selection":
val := cArray.Interface().(*C.struct_Selection)
arr := unsafe.Slice(val, cArrayLen)
return reflect.ValueOf(arr)
}
return cArray
}
// cValueToGo convert C language object to Go variable base on the given Go
// structure types, this function extract each fields of the structure from
// object recursively.
func cValueToGo(cVal reflect.Value, goType reflect.Type) (reflect.Value, error) {
result := reflect.New(goType)
s := result.Elem()
for resultFieldIdx := 0; resultFieldIdx < s.NumField(); resultFieldIdx++ {
field := goType.Field(resultFieldIdx)
if unicode.IsLower(rune(field.Name[0])) {
continue
}
if goBaseTypes[field.Type.Kind()] {
cBaseVal := cVal.FieldByName(field.Name)
goBaseVal, err := cToGoBaseType(cBaseVal, field.Type.Kind())
if err != nil {
return result, err
}
s.Field(resultFieldIdx).Set(goBaseVal.Convert(s.Field(resultFieldIdx).Type()))
continue
}
switch field.Type.Kind() {
case reflect.Ptr:
// Pointer of the Go data type, for example: *excelize.Options or *string
ptrType := field.Type.Elem()
if !goBaseTypes[ptrType.Kind()] {
// Pointer of the Go struct, for example: *excelize.Options
cObjVal := cVal.FieldByName(field.Name)
if cObjVal.Elem().CanAddr() {
v, err := cValueToGo(cObjVal.Elem(), ptrType)
if err != nil {
return result, err
}
s.Field(resultFieldIdx).Set(v)
}
}
if goBaseTypes[ptrType.Kind()] {
// Pointer of the Go basic data type, for example: *string
cBaseVal := cVal.FieldByName(field.Name)
if !cBaseVal.IsNil() {
v, err := cToGoBaseType(cBaseVal.Elem(), ptrType.Kind())
if err != nil {
return result, err
}
x := reflect.New(ptrType)
x.Elem().Set(v)
s.Field(resultFieldIdx).Set(x.Elem().Addr())
}
}
case reflect.Struct:
// The Go struct, for example: excelize.Options, convert sub fields recursively
structType := field.Type
cObjVal := cVal.FieldByName(field.Name)
v, err := cValueToGo(cObjVal, structType)
if err != nil {
return result, err
}
s.Field(resultFieldIdx).Set(v.Elem())
case reflect.Slice:
// The Go data type array, for example:
// []*excelize.Options, []excelize.Options, []string, []*string
ele := field.Type.Elem()
cArray := cVal.FieldByName(field.Name)
if cArray.IsZero() {
continue
}
if ele.Kind() == reflect.Ptr {
// Pointer array of the Go data type, for example: []*excelize.Options or []*string
subEle := ele.Elem()
cArrayLen := int(cVal.FieldByName(field.Name + "Len").Int())
cArray = cToGoArray(cArray, cArrayLen)
for i := 0; i < cArray.Len(); i++ {
if goBaseTypes[subEle.Kind()] {
// Pointer array of the Go basic data type, for example: []*string
v, err := cToGoBaseType(cArray.Index(i), subEle.Kind())
if err != nil {
return result, err
}
x := reflect.New(subEle)
x.Elem().Set(v)
s.Field(resultFieldIdx).Set(reflect.Append(s.Field(resultFieldIdx), x.Elem().Addr()))
} else {
// Pointer array of the Go struct, for example: []*excelize.Options
v, err := cValueToGo(cArray.Index(i).Elem(), subEle)
if err != nil {
return result, err
}
x := reflect.New(subEle)
x.Elem().Set(v.Elem())
s.Field(resultFieldIdx).Set(reflect.Append(s.Field(resultFieldIdx), x.Elem().Addr()))
}
}
} else {
// The Go data type array, for example: []excelize.Options or []string
subEle := ele
cArrayLen := int(cVal.FieldByName(field.Name + "Len").Int())
if subEle.Kind() == reflect.Uint8 { // []byte
buf := C.GoBytes(unsafe.Pointer(cArray.Interface().(*C.uchar)), C.int(cArrayLen))
s.Field(resultFieldIdx).Set(reflect.ValueOf(buf))
continue
}
cArray = cToGoArray(cArray, cArrayLen)
for i := 0; i < cArray.Len(); i++ {
if goBaseTypes[subEle.Kind()] {
// The Go basic data type array, for example: []string
v, err := cToGoBaseType(cArray.Index(i), subEle.Kind())
if err != nil {
return result, err
}
s.Field(resultFieldIdx).Set(reflect.Append(s.Field(resultFieldIdx), v))
} else {
// The Go struct array, for example: []excelize.Options
v, err := cValueToGo(cArray.Index(i), subEle)
if err != nil {
return result, err
}
s.Field(resultFieldIdx).Set(reflect.Append(s.Field(resultFieldIdx), v.Elem()))
}
}
}
}
}
return result, nil
}
// goBaseTypeToC convert Go basic data type value to C variable.
func goBaseTypeToC(goVal reflect.Value, kind reflect.Kind) (reflect.Value, error) {
fn, ok := goBaseValueToCFuncs[kind]
if !ok {
return reflect.ValueOf(nil), errors.New("invalid argument data type" + kind.String())
}
return fn(goVal, kind)
}
// goValueToC convert Go variable to C object base on the given Go structure
// types, this function extract each fields of the structure from structure
// variable recursively.
func goValueToC(goVal, cVal reflect.Value) (reflect.Value, error) {
result := cVal
c := result.Elem()
for i := 0; i < goVal.Type().NumField(); i++ {
cField, _ := c.Type().FieldByName(goVal.Type().Field(i).Name)
field := goVal.Type().Field(i)
if unicode.IsLower(rune(field.Name[0])) {
continue
}
if goBaseTypes[field.Type.Kind()] {
goBaseVal := goVal.FieldByName(field.Name)
cBaseVal, err := goBaseTypeToC(goBaseVal, goBaseVal.Type().Kind())
if err != nil {
return result, err
}
c.FieldByName(field.Name).Set(cBaseVal.Convert(cField.Type))
continue
}
switch goVal.Type().Field(i).Type.Kind() {
case reflect.Ptr:
// Pointer of the Go data type, for example: *excelize.Options or *string
ptrType := field.Type.Elem()
if !goBaseTypes[ptrType.Kind()] {
// Pointer of the Go struct, for example: *excelize.Options
goStructVal := goVal.Field(i)
if !goStructVal.IsNil() {
cPtr := C.malloc(C.size_t(cField.Type.Elem().Size()))
cStructPtr := reflect.NewAt(cField.Type.Elem(), cPtr)
v, err := goValueToC(goStructVal.Elem(), cStructPtr)
if err != nil {
return result, err
}
c.FieldByName(field.Name).Set(v)
}
}
if goBaseTypes[ptrType.Kind()] {
// Pointer of the Go basic data type, for example: *string
goBaseVal := goVal.Field(i)
if !goBaseVal.IsNil() {
v, err := goBaseTypeToC(goBaseVal.Elem(), ptrType.Kind())
if err != nil {
return result, err
}
cValPtr := C.malloc(C.size_t(unsafe.Sizeof(cField.Type.Elem().Size())))
ptrVal := reflect.NewAt(v.Type(), cValPtr).Elem()
ptrVal.Set(v)
c.FieldByName(field.Name).Set(ptrVal.Addr())
}
}
case reflect.Struct:
// The Go struct, for example: excelize.Options, convert sub fields recursively
goStructVal := goVal.Field(i)
v, err := goValueToC(goStructVal, reflect.New(cField.Type))
if err != nil {
return result, err
}
c.FieldByName(field.Name).Set(v.Elem())
case reflect.Slice:
// The Go data type array, for example:
// []*excelize.Options, []excelize.Options, []string, []*string
goSlice := goVal.Field(i)
ele := goSlice.Type().Elem()
l, err := goBaseTypeToC(reflect.ValueOf(goSlice.Len()), reflect.Int)
if err != nil {
return result, err
}
c.FieldByName(field.Name + "Len").Set(l)
cArray := C.malloc(C.size_t(goSlice.Len()) * C.size_t(cField.Type.Elem().Size()))
for j := 0; j < goSlice.Len(); j++ {
if goBaseTypes[ele.Kind()] {
// The Go basic data type array, for example: []string
cBaseVal, err := goBaseTypeToC(goSlice.Index(j), goSlice.Index(j).Type().Kind())
if err != nil {
return result, err
}
elePtr := unsafe.Pointer(uintptr(cArray) + uintptr(j)*cBaseVal.Type().Size())
ele := reflect.NewAt(cBaseVal.Type(), elePtr).Elem()
ele.Set(cBaseVal)
} else {
// The Go struct array, for example: []excelize.Options
cPtr := C.malloc(C.size_t(cField.Type.Elem().Size()))
cStructPtr := reflect.NewAt(cField.Type.Elem(), cPtr)
v, err := goValueToC(goSlice.Index(j), cStructPtr)
if err != nil {
return result, err
}
elePtr := unsafe.Pointer(uintptr(cArray) + uintptr(j)*cField.Type.Elem().Size())
ele := reflect.NewAt(cField.Type.Elem(), elePtr).Elem()
ele.Set(reflect.NewAt(cField.Type.Elem(), unsafe.Pointer(v.Pointer())).Elem())
}
}
c.FieldByName(field.Name).Set(reflect.NewAt(cField.Type.Elem(), cArray))
}
}
return result, nil
}
// cInterfaceToGo convert C interface to Go interface data type value.
func cInterfaceToGo(val C.struct_Interface) interface{} {
switch val.Type {
case Int:
return int(val.Integer)
case String:
return C.GoString(val.String)
case Float:
return float64(val.Float64)
case Boolean:
return bool(val.Boolean)
case Time:
return time.Unix(int64(val.Integer), 0)
default:
return nil
}
}
// AddChart provides the method to add chart in a sheet by given chart format
// set (such as offset, scale, aspect ratio setting and print settings) and
// properties set.
//
//export AddChart
func AddChart(idx int, sheet, cell *C.char, chart *C.struct_Chart, length int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
charts := make([]*excelize.Chart, length)
for i, c := range unsafe.Slice(chart, length) {
goVal, err := cValueToGo(reflect.ValueOf(c), reflect.TypeOf(excelize.Chart{}))
if err != nil {
return C.CString(err.Error())
}
c := goVal.Elem().Interface().(excelize.Chart)
charts[i] = &c
}
if len(charts) > 1 {
if err := f.(*excelize.File).AddChart(C.GoString(sheet), C.GoString(cell), charts[0], charts[1:]...); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
if err := f.(*excelize.File).AddChart(C.GoString(sheet), C.GoString(cell), charts[0]); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddChartSheet provides the method to create a chartsheet by given chart
// format set (such as offset, scale, aspect ratio setting and print settings)
// and properties set. In Excel a chartsheet is a worksheet that only contains
// a chart.
//
//export AddChartSheet
func AddChartSheet(idx int, sheet *C.char, chart *C.struct_Chart, length int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
charts := make([]*excelize.Chart, length)
for i, c := range unsafe.Slice(chart, length) {
goVal, err := cValueToGo(reflect.ValueOf(c), reflect.TypeOf(excelize.Chart{}))
if err != nil {
return C.CString(err.Error())
}
c := goVal.Elem().Interface().(excelize.Chart)
charts[i] = &c
}
if len(charts) > 1 {
if err := f.(*excelize.File).AddChartSheet(C.GoString(sheet), charts[0], charts[1:]...); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
if err := f.(*excelize.File).AddChartSheet(C.GoString(sheet), charts[0]); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddComment provides the method to add comments in a sheet by giving the
// worksheet name, cell reference, and format set (such as author and text).
// Note that the maximum author name length is 255 and the max text length is
// 32512.
//
//export AddComment
func AddComment(idx int, sheet *C.char, opts *C.struct_Comment) *C.char {
var comment excelize.Comment
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.Comment{}))
if err != nil {
return C.CString(err.Error())
}
comment = goVal.Elem().Interface().(excelize.Comment)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddComment(C.GoString(sheet), comment); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddFormControl provides the method to add form control button in a worksheet
// by given worksheet name and form control options. Supported form control
// type: button, check box, group box, label, option button, scroll bar and
// spinner. If set macro for the form control, the workbook extension should be
// XLSM or XLTM. Scroll value must be between 0 and 30000.
//
//export AddFormControl
func AddFormControl(idx int, sheet *C.char, opts *C.struct_FormControl) *C.char {
var options excelize.FormControl
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.FormControl{}))
if err != nil {
return C.CString(err.Error())
}
options = goVal.Elem().Interface().(excelize.FormControl)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddFormControl(C.GoString(sheet), options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// Add picture in a sheet by given picture format set (such as offset, scale,
// aspect ratio setting and print settings) and file path, supported image
// types: BMP, EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
//
//export AddPicture
func AddPicture(idx int, sheet, cell, name *C.char, opts *C.struct_GraphicOptions) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if opts != nil {
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.GraphicOptions{}))
if err != nil {
return C.CString(err.Error())
}
options := goVal.Elem().Interface().(excelize.GraphicOptions)
if err := f.(*excelize.File).AddPicture(C.GoString(sheet), C.GoString(cell), C.GoString(name), &options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
if err := f.(*excelize.File).AddPicture(C.GoString(sheet), C.GoString(cell), C.GoString(name), nil); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddPictureFromBytes provides the method to add picture in a sheet by given
// picture format set (such as offset, scale, aspect ratio setting and print
// settings), file base name, extension name and file bytes, supported image
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. Note that
// this function only supports adding pictures placed over the cells currently,
// and doesn't support adding pictures placed in cells or creating the Kingsoft
// WPS Office embedded image cells
//
//export AddPictureFromBytes
func AddPictureFromBytes(idx int, sheet, cell *C.char, pic *C.struct_Picture) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
goVal, err := cValueToGo(reflect.ValueOf(*pic), reflect.TypeOf(excelize.Picture{}))
if err != nil {
return C.CString(err.Error())
}
options := goVal.Elem().Interface().(excelize.Picture)
if err := f.(*excelize.File).AddPictureFromBytes(C.GoString(sheet), C.GoString(cell), &options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddPivotTable provides the method to add pivot table by given pivot table
// options. Note that the same fields can not in Columns, Rows and Filter
// fields at the same time.
//
//export AddPivotTable
func AddPivotTable(idx int, opts *C.struct_PivotTableOptions) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.PivotTableOptions{}))
if err != nil {
return C.CString(err.Error())
}
options := goVal.Elem().Interface().(excelize.PivotTableOptions)
if err := f.(*excelize.File).AddPivotTable(&options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddShape provides the method to add shape in a sheet by given worksheet
// name and shape format set (such as offset, scale, aspect ratio setting and
// print settings).
//
//export AddShape
func AddShape(idx int, sheet *C.char, opts *C.struct_Shape) *C.char {
var options excelize.Shape
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.Shape{}))
if err != nil {
return C.CString(err.Error())
}
options = goVal.Elem().Interface().(excelize.Shape)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddShape(C.GoString(sheet), &options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddSlicer function inserts a slicer by giving the worksheet name and slicer
// settings.
//
//export AddSlicer
func AddSlicer(idx int, sheet *C.char, opts *C.struct_SlicerOptions) *C.char {
var options excelize.SlicerOptions
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.SlicerOptions{}))
if err != nil {
return C.CString(err.Error())
}
options = goVal.Elem().Interface().(excelize.SlicerOptions)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddSlicer(C.GoString(sheet), &options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddSparkline provides a function to add sparklines to the worksheet by
// given formatting options. Sparklines are small charts that fit in a single
// cell and are used to show trends in data. Sparklines are a feature of Excel
// 2010 and later only. You can write them to workbook that can be read by Excel
// 2007, but they won't be displayed.
//
//export AddSparkline
func AddSparkline(idx int, sheet *C.char, opts *C.struct_SparklineOptions) *C.char {
var options excelize.SparklineOptions
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.SparklineOptions{}))
if err != nil {
return C.CString(err.Error())
}
options = goVal.Elem().Interface().(excelize.SparklineOptions)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddSparkline(C.GoString(sheet), &options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddTable provides the method to add table in a worksheet by given worksheet
// name, range reference and format set.
//
//export AddTable
func AddTable(idx int, sheet *C.char, table *C.struct_Table) *C.char {
var tbl excelize.Table
goVal, err := cValueToGo(reflect.ValueOf(*table), reflect.TypeOf(excelize.Table{}))
if err != nil {
return C.CString(err.Error())
}
tbl = goVal.Elem().Interface().(excelize.Table)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).AddTable(C.GoString(sheet), &tbl); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AddVBAProject provides the method to add vbaProject.bin file which contains
// functions and/or macros. The file extension should be XLSM or XLTM.
//
//export AddVBAProject
func AddVBAProject(idx int, file *C.uchar, fileLen C.int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
buf := C.GoBytes(unsafe.Pointer(file), fileLen)
if err := f.(*excelize.File).AddVBAProject(buf); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// AutoFilter provides the method to add auto filter in a worksheet by given
// worksheet name, range reference and settings. An auto filter in Excel is a
// way of filtering a 2D range of data based on some simple criteria.
//
//export AutoFilter
func AutoFilter(idx int, sheet, rangeRef *C.char, opts *C.struct_AutoFilterOptions, length int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
options := make([]excelize.AutoFilterOptions, length)
for i, val := range unsafe.Slice(opts, length) {
goVal, err := cValueToGo(reflect.ValueOf(val), reflect.TypeOf(excelize.AutoFilterOptions{}))
if err != nil {
return C.CString(err.Error())
}
options[i] = goVal.Elem().Interface().(excelize.AutoFilterOptions)
}
if err := f.(*excelize.File).AutoFilter(C.GoString(sheet), C.GoString(rangeRef), options); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// CalcCellValue provides a function to get calculated cell value. This feature
// is currently in working processing. Iterative calculation, implicit
// intersection, explicit intersection, array formula, table formula and some
// other formulas are not supported currently.
//
//export CalcCellValue
func CalcCellValue(idx int, sheet, cell *C.char, opts *C.struct_Options) C.struct_StringErrorResult {
var options excelize.Options
f, ok := files.Load(idx)
if !ok {
return C.struct_StringErrorResult{val: C.CString(emptyString), err: C.CString(errFilePtr)}
}
if opts != nil {
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.Options{}))
if err != nil {
return C.struct_StringErrorResult{val: C.CString(emptyString), err: C.CString(err.Error())}
}
options = goVal.Elem().Interface().(excelize.Options)
}
val, err := f.(*excelize.File).CalcCellValue(C.GoString(sheet), C.GoString(cell), options)
if err != nil {
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(err.Error())}
}
return C.struct_StringErrorResult{val: C.CString(val), err: C.CString(emptyString)}
}
// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates
// or returns an error.
//
//export CellNameToCoordinates
func CellNameToCoordinates(cell *C.char) C.struct_CellNameToCoordinatesResult {
col, row, err := excelize.CellNameToCoordinates(C.GoString(cell))
if err != nil {
return C.struct_CellNameToCoordinatesResult{col: C.int(col), row: C.int(row), err: C.CString(err.Error())}
}
return C.struct_CellNameToCoordinatesResult{col: C.int(col), row: C.int(row), err: C.CString(emptyString)}
}
// ColumnNameToNumber provides a function to convert Excel sheet column name
// (case-insensitive) to int. The function returns an error if column name
// incorrect.
//
//export ColumnNameToNumber
func ColumnNameToNumber(name *C.char) C.struct_IntErrorResult {
col, err := excelize.ColumnNameToNumber(C.GoString(name))
if err != nil {
return C.struct_IntErrorResult{val: C.int(col), err: C.CString(err.Error())}
}
return C.struct_IntErrorResult{val: C.int(col), err: C.CString(emptyString)}
}
// ColumnNumberToName provides a function to convert the integer to Excel
// sheet column title.
//
//export ColumnNumberToName
func ColumnNumberToName(num int) C.struct_StringErrorResult {
col, err := excelize.ColumnNumberToName(num)
if err != nil {
return C.struct_StringErrorResult{val: C.CString(col), err: C.CString(err.Error())}
}
return C.struct_StringErrorResult{val: C.CString(col), err: C.CString(emptyString)}
}
// CoordinatesToCellName converts [X, Y] coordinates to alpha-numeric cell name
// or returns an error.
//
//export CoordinatesToCellName
func CoordinatesToCellName(col, row int, abs bool) C.struct_StringErrorResult {
cell, err := excelize.CoordinatesToCellName(col, row, abs)
if err != nil {
return C.struct_StringErrorResult{val: C.CString(cell), err: C.CString(err.Error())}
}
return C.struct_StringErrorResult{val: C.CString(cell), err: C.CString(emptyString)}
}
// Close closes and cleanup the open temporary file for the spreadsheet.
//
//export Close
func Close(idx int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
defer files.Delete(idx)
if err := f.(*excelize.File).Close(); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// CopySheet provides a function to duplicate a worksheet by gave source and
// target worksheet index. Note that currently doesn't support duplicate
// workbooks that contain tables, charts or pictures.
//
//export CopySheet
func CopySheet(idx, from, to int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).CopySheet(from, to); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeleteChart provides a function to delete chart in spreadsheet by given
// worksheet name and cell reference.
//
//export DeleteChart
func DeleteChart(idx int, sheet, cell *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteChart(C.GoString(sheet), C.GoString(cell)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeleteComment provides the method to delete comment in a sheet by given
// worksheet name.
//
//export DeleteComment
func DeleteComment(idx int, sheet, cell *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteComment(C.GoString(sheet), C.GoString(cell)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeleteDefinedName provides a function to delete the defined names of the
// workbook or worksheet. If not specified scope, the default scope is
// workbook.
//
//export DeleteDefinedName
func DeleteDefinedName(idx int, definedName *C.struct_DefinedName) *C.char {
var df excelize.DefinedName
goVal, err := cValueToGo(reflect.ValueOf(*definedName), reflect.TypeOf(excelize.DefinedName{}))
if err != nil {
return C.CString(err.Error())
}
df = goVal.Elem().Interface().(excelize.DefinedName)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteDefinedName(&df); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeletePicture provides a function to delete charts in spreadsheet by given
// worksheet name and cell reference. Note that the image file won't be
// deleted from the document currently.
//
//export DeletePicture
func DeletePicture(idx int, sheet, cell *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeletePicture(C.GoString(sheet), C.GoString(cell)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeleteSheet provides a function to delete worksheet in a workbook by given
// worksheet name. Use this method with caution, which will affect changes in
// references such as formulas, charts, and so on. If there is any referenced
// value of the deleted worksheet, it will cause a file error when you open
// it. This function will be invalid when only one worksheet is left.
//
//export DeleteSheet
func DeleteSheet(idx int, sheet *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteSheet(C.GoString(sheet)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DeleteSlicer provides the method to delete a slicer by a given slicer name.
//
//export DeleteSlicer
func DeleteSlicer(idx int, name *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteSlicer(C.GoString(name)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DuplicateRow inserts a copy of specified row (by its Excel row number)
// below. Use this method with caution, which will affect changes in
// references such as formulas, charts, and so on. If there is any referenced
// value of the worksheet, it will cause a file error when you open it. The
// excelize only partially updates these references currently.
//
//export DuplicateRow
func DuplicateRow(idx int, sheet *C.char, row int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DuplicateRow(C.GoString(sheet), row); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// DuplicateRowTo inserts a copy of specified row by it Excel number to
// specified row position moving down exists rows after target position. Use
// this method with caution, which will affect changes in references such as
// formulas, charts, and so on. If there is any referenced value of the
// worksheet, it will cause a file error when you open it. The excelize only
// partially updates these references currently.
//
//export DuplicateRowTo
func DuplicateRowTo(idx int, sheet *C.char, row, row2 int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DuplicateRowTo(C.GoString(sheet), row, row2); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}
// GetActiveSheetIndex provides a function to get active sheet index of the
// spreadsheet. If not found the active sheet will be return integer 0.
//
//export GetActiveSheetIndex
func GetActiveSheetIndex(idx int) int {
f, ok := files.Load(idx)
if !ok {
return 0