-
Notifications
You must be signed in to change notification settings - Fork 0
/
gong_serialize.go
142 lines (116 loc) · 3.55 KB
/
gong_serialize.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
// generated code - do not edit
package models
import (
"fmt"
"log"
"unicode/utf8"
"github.com/xuri/excelize/v2"
)
func SerializeStage(stage *StageStruct, filename string) {
f := excelize.NewFile()
{
// insertion point
SerializeExcelize[Astruct](stage, f)
SerializeExcelize[AstructBstruct2Use](stage, f)
SerializeExcelize[AstructBstructUse](stage, f)
SerializeExcelize[Bstruct](stage, f)
SerializeExcelize[Dstruct](stage, f)
SerializeExcelize[Fstruct](stage, f)
}
var tab ExcelizeTabulator
tab.SetExcelizeFile(f)
{
f.DeleteSheet("Sheet1")
if err := f.SaveAs(filename); err != nil {
fmt.Println("cannot write xl file : ", err)
}
}
}
// Tabulator is an interface for writing to a table strings
type Tabulator interface {
AddSheet(sheetName string)
AddRow(sheetName string) int
AddCell(sheetName string, rowId, columnIndex int, value string)
}
func Serialize[Type Gongstruct](stage *StageStruct, tab Tabulator) {
sheetName := GetGongstructName[Type]()
// Create a new sheet.
tab.AddSheet(sheetName)
headerRowIndex := tab.AddRow(sheetName)
for colIndex, fieldName := range GetFields[Type]() {
tab.AddCell(sheetName, headerRowIndex, colIndex, fieldName)
// f.SetCellStr(sheetName, fmt.Sprintf("%s%d", IntToLetters(int32(index+1)), line), fieldName)
}
set := *GetGongstructInstancesSet[Type](stage)
for instance := range set {
line := tab.AddRow(sheetName)
for index, fieldName := range GetFields[Type]() {
tab.AddCell(sheetName, line, index, GetFieldStringValue(
any(*instance).(Type), fieldName))
// f.SetCellStr(sheetName, fmt.Sprintf("%s%d", IntToLetters(int32(index+1)), line), GetFieldStringValue(
// any(*instance).(Type), fieldName))
}
}
}
type ExcelizeTabulator struct {
f *excelize.File
}
func (tab *ExcelizeTabulator) SetExcelizeFile(f *excelize.File) {
tab.f = f
}
func (tab *ExcelizeTabulator) AddSheet(sheetName string) {
}
func (tab *ExcelizeTabulator) AddRow(sheetName string) (rowId int) {
return
}
func (tab *ExcelizeTabulator) AddCell(sheetName string, rowId, columnIndex int, value string) {
}
func SerializeExcelize[Type Gongstruct](stage *StageStruct, f *excelize.File) {
sheetName := GetGongstructName[Type]()
// Create a new sheet.
f.NewSheet(sheetName)
set := *GetGongstructInstancesSet[Type](stage)
line := 1
for index, fieldName := range GetFields[Type]() {
f.SetCellStr(sheetName, fmt.Sprintf("%s%d", IntToLetters(int32(index+1)), line), fieldName)
}
f.AutoFilter(sheetName,
fmt.Sprintf("%s%d", IntToLetters(int32(1)), line),
[]excelize.AutoFilterOptions{})
for instance := range set {
line = line + 1
for index, fieldName := range GetFields[Type]() {
f.SetCellStr(sheetName, fmt.Sprintf("%s%d", IntToLetters(int32(index+1)), line), GetFieldStringValue(
any(*instance).(Type), fieldName))
}
}
// Autofit all columns according to their text content
cols, err := f.GetCols(sheetName)
if err != nil {
log.Panicln("SerializeExcelize")
}
for idx, col := range cols {
largestWidth := 0
for _, rowCell := range col {
cellWidth := utf8.RuneCountInString(rowCell) + 2 // + 2 for margin
if cellWidth > largestWidth {
largestWidth = cellWidth
}
}
name, err := excelize.ColumnNumberToName(idx + 1)
if err != nil {
log.Panicln("SerializeExcelize")
}
f.SetColWidth(sheetName, name, name, float64(largestWidth))
}
}
func IntToLetters(number int32) (letters string) {
number--
if firstLetter := number / 26; firstLetter > 0 {
letters += IntToLetters(firstLetter)
letters += string('A' + number%26)
} else {
letters += string('A' + number)
}
return
}