-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyfastexcel.go
175 lines (161 loc) · 3.84 KB
/
pyfastexcel.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
package main
// #include <stdlib.h>
import (
"C"
)
import (
"fmt"
"unsafe"
"encoding/base64"
"testing"
"github.com/Zncl2222/pyfastexcel/pyfastexcel/core"
)
// Export takes a C char pointer containing JSON data for an Excel file and returns a base64 encoded string of the generated Excel file.
//
// Args:
//
// data (*C.char): A C char pointer containing JSON data for the Excel file.
//
// Returns:
//
// *C.char: A C char pointer containing the base64 encoded string of the generated Excel file.
//
// Notes:
// - This function does not directly interact with C code.
// - Remember to free the memory allocated for the returned pointer using `C.free`.
//
//export Export
func Export(data *C.char, useCatchPanic int64) *C.char {
if useCatchPanic != 0 {
defer catchPanic()
}
goStringData := C.GoString(data)
result := core.WriteExcel(goStringData)
encodedRes := C.CString(result)
return encodedRes
}
func catchPanic() {
if r := recover(); r != nil {
fmt.Printf("Recovered from panic: %v\n", r)
}
}
// FreeCPointer frees the memory allocated for a C char pointer.
//
// Args:
//
// cptr (*C.char): The C char pointer to be freed.
// printMsg (int64): The flag to print message.
//
//export FreeCPointer
func FreeCPointer(cptr *C.char, printMsg int64) {
C.free(unsafe.Pointer(cptr))
if printMsg == 1 {
fmt.Println("C Pointer Free Successfully !")
}
}
// testExport is a trick method to test cgo in golang standard test module
func testExport(t *testing.T) {
// Mock input data
inputData := `{
"style": {
"style1": {
"Font": {
"Bold": true
},
"Fill": {
"Type": "pattern",
"Color": "#FFFFFF",
"Pattern": 1,
"Shading": 100
},
"Border": {
"left": {
"Color": "FF0000",
"Style": 1
},
"top": {
"Color": "00FF00",
"Style": 2
}
},
"Alignment": {
"Horizontal": "center",
"Vertical": "middle",
"Indent": 0,
"JustifyLastLine": false,
"ReadingOrder": 0,
"RelativeIndent": 0,
"ShrinkToFit": false,
"TextRotation": 0,
"WrapText": false
},
"Protection": {
"Hidden": true,
"Locked": false
},
"CustomNumFmt": "0.00"
}
},
"protection": {},
"file_props": {
"Title": "Test Excel File",
"Creator": "Test User",
"Category": "Test Category",
"ContentStatus": "Draft",
"Description": "Test Description",
"Keywords": "Test Keywords",
"Language": "en-US",
"LastModifiedBy": "Test User",
"Revision": "1",
"Subject": "Test Subject",
"Version": "1.0",
"Identifier": "",
"Created": "",
"Modified": ""
},
"sheet_order": ["Sheet1"],
"content": {
"Sheet1": {
"Header": [
["Column1", "Column2", "Column3"]
],
"Data": [
[["Data1", "style1"], ["Data2", "style1"], ["Data3", "style1"], []],
[["Data4", "style1"], ["Data5", "style1"], ["Data6", "style1"], []]
],
"Height": {"3": 252},
"Width": {"1": 25, "2": 26, "3": 6},
"MergeCells": [["A1", "A2"], ["B2","C3"]],
"AutoFilter": [],
"Panes": {},
"DataValidation": [],
"Comment": [],
"NoStyle": false,
"Table": [],
"Chart": [],
"PivotTable": [],
"SheetVisible": true,
"WriterEngine": "StreamWriter"
}
}
}`
// Convert input data to *C.char
cInputData := C.CString(inputData)
// Call the Export function
encodedExcel := Export(cInputData, 1)
// Free the allocated memory for cInputData
FreeCPointer(cInputData, 1)
// Convert the result back to a Go string
goEncodedExcel := C.GoString(encodedExcel)
// Decode the encoded Excel data
decodedExcel, err := base64.StdEncoding.DecodeString(goEncodedExcel)
if err != nil {
t.Fatalf("Failed to decode encoded Excel data: %v", err)
}
// Assert the expected result
if len(decodedExcel) == 0 {
t.Error("Encoded Excel data is empty")
}
}
func main() {
}