-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.go
167 lines (135 loc) · 3.87 KB
/
excel.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
package formats
import (
"fmt"
"github.com/dazfuller/azcosts/internal/model"
"github.com/xuri/excelize/v2"
"log"
)
const firstCol = "A"
type ExcelFormatter struct {
outputPath string
}
func MakeExcelFormatter(outputPath string) (ExcelFormatter, error) {
if err := validateOptions(false, outputPath); err != nil {
return ExcelFormatter{}, err
}
return ExcelFormatter{outputPath: outputPath}, nil
}
func (ef ExcelFormatter) Generate(costs []model.ResourceGroupSummary) error {
f := excelize.NewFile()
defer func(f *excelize.File) {
err := f.Close()
if err != nil {
log.Printf("Unable to close Excel workbook")
}
}(f)
sheetName := "Costs"
err := f.SetSheetName("Sheet1", sheetName)
if err != nil {
return err
}
err = ef.addHeaders(f, sheetName, costs[0])
if err != nil {
return err
}
err = ef.addCostDataToSheet(f, sheetName, costs)
if err != nil {
return err
}
cols, _ := f.GetCols(sheetName)
lastCol, _ := excelize.ColumnNumberToName(len(cols))
err = ef.setSheetFormats(f, sheetName, cols)
if err != nil {
return err
}
err2 := ef.addTable(f, sheetName, costs, lastCol)
if err2 != nil {
return err2
}
if err := f.SaveAs(ef.outputPath); err != nil {
return fmt.Errorf("an error occured saving the workbook: %s", err.Error())
}
return nil
}
func (ef ExcelFormatter) addHeaders(f *excelize.File, sheetName string, costEntry model.ResourceGroupSummary) error {
firstCell, _ := excelize.JoinCellName("A", 1)
headers := []string{
"Resource Group",
"Subscription",
}
for _, bp := range costEntry.Costs {
headers = append(headers, bp.Period)
}
headers = append(headers, "Total Cost")
err := f.SetSheetRow(sheetName, firstCell, &headers)
if err != nil {
return fmt.Errorf("unable to set header row in costs sheet: %v", err)
}
return nil
}
func (ef ExcelFormatter) addCostDataToSheet(f *excelize.File, sheetName string, costs []model.ResourceGroupSummary) error {
for i, entry := range costs {
rowStart, _ := excelize.JoinCellName("A", i+2)
row := []interface{}{
entry.Name,
entry.SubscriptionName,
}
for _, cost := range entry.Costs {
row = append(row, cost.Total)
}
row = append(row, entry.TotalCost)
err := f.SetSheetRow(sheetName, rowStart, &row)
if err != nil {
return fmt.Errorf("unable to add data row to costs worksheet: %v", err)
}
}
return nil
}
func (ef ExcelFormatter) setSheetFormats(f *excelize.File, sheetName string, cols [][]string) error {
customNumFmt := "#,##0.00;(#,##0.00);-"
billingStyle, _ := f.NewStyle(&excelize.Style{
CustomNumFmt: &customNumFmt, Alignment: &excelize.Alignment{
Horizontal: "right",
},
})
for i := range cols {
colName, _ := excelize.ColumnNumberToName(i + 1)
if i < 2 {
maxLength := 0
for _, v := range cols[i] {
if len(v) > maxLength {
maxLength = len(v)
}
}
err := f.SetColWidth(sheetName, colName, colName, float64(maxLength)*0.9)
if err != nil {
return fmt.Errorf("unable to set column width for column %s: %v", colName, err)
}
continue
}
err := f.SetColStyle(sheetName, colName, billingStyle)
if err != nil {
return fmt.Errorf("unable to set column style for column %s: %v", colName, err)
}
err = f.SetColWidth(sheetName, colName, colName, 15)
if err != nil {
return fmt.Errorf("unable to set column width for column %s: %v", colName, err)
}
}
return nil
}
func (ef ExcelFormatter) addTable(f *excelize.File, sheetName string, costs []model.ResourceGroupSummary, lastCol string) error {
showHeaderRow := true
showRowStripes := true
err := f.AddTable(sheetName, &excelize.Table{
Range: fmt.Sprintf("%s%d:%s%d", firstCol, 1, lastCol, len(costs)+1),
Name: "CostSummary",
StyleName: "TableStyleMedium9",
ShowHeaderRow: &showHeaderRow,
ShowRowStripes: &showRowStripes,
})
if err != nil {
return fmt.Errorf("unable to add table to costs sheet: %v", err)
}
return nil
}