This repository has been archived by the owner on Jan 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
59 lines (50 loc) · 1.62 KB
/
template.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
package main
import (
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/Toshik1978/cart_to_qwintry/cartparsers"
"github.com/pkg/errors"
)
const (
templateSheet = "Лист1"
templateCell = "%s%d"
cartXlsx = "./cart.xlsx"
)
// SaveTemplate saves template with cart items
func SaveTemplate(templatePath string, cart []cartparsers.CartItem) error {
if len(cart) == 0 {
return errors.New("empty cart detected")
}
xlsx, err := excelize.OpenFile(templatePath)
if err != nil {
return errors.Wrap(err, "failed to open template")
}
for i, cartItem := range cart {
writeCartItem(xlsx, i+2, cartItem)
}
if err := xlsx.SaveAs(cartXlsx); err != nil {
return errors.Wrap(err, "failed to save XLSX")
}
return nil
}
// writeCartItem save cart's item in XLSX
func writeCartItem(xlsx *excelize.File, index int, cartItem cartparsers.CartItem) {
xlsx.SetCellValue(templateSheet, fmt.Sprintf(templateCell, "A", index), getProductDesc(cartItem))
xlsx.SetCellValue(templateSheet, fmt.Sprintf(templateCell, "C", index), cartItem.ProductQty)
xlsx.SetCellValue(templateSheet, fmt.Sprintf(templateCell, "D", index), cartItem.ProductPrice)
xlsx.SetCellValue(templateSheet, fmt.Sprintf(templateCell, "F", index), cartItem.ProductUrl)
}
// getProductDesc retrieve description for template
func getProductDesc(cartItem cartparsers.CartItem) string {
desc := cartItem.ProductName
if len(cartItem.ProductStyle) > 0 {
desc += ", " + cartItem.ProductStyle
}
if len(cartItem.ProductSize) > 0 {
desc += ", " + cartItem.ProductSize
}
if len(cartItem.ProductColor) > 0 {
desc += ", " + cartItem.ProductColor
}
return desc
}