sirius/main.go

69 lines
1.5 KiB
Go
Raw Normal View History

2023-04-14 09:01:18 +03:00
package main
import (
2023-04-14 17:51:35 +03:00
"bytes"
2023-04-14 09:01:18 +03:00
"encoding/json"
"fmt"
"io/ioutil"
2023-04-14 17:51:35 +03:00
"os"
"strconv"
2023-04-14 09:01:18 +03:00
2023-04-14 17:51:35 +03:00
excelize "github.com/xuri/excelize/v2"
2023-04-14 09:01:18 +03:00
"gitstore.ru/tolikproh/sirius/model"
)
func main() {
var sirius model.Sirius
2023-04-14 17:51:35 +03:00
siriusJson, err := ioutil.ReadFile("sirius.json")
2023-04-14 09:01:18 +03:00
if err != nil {
2023-04-14 17:51:35 +03:00
fmt.Println("No read file: sirius.json")
os.Exit(0)
2023-04-14 09:01:18 +03:00
}
2023-04-14 17:51:35 +03:00
siriusJson = bytes.TrimPrefix(siriusJson, []byte("\xef\xbb\xbf"))
if !json.Valid(siriusJson) {
fmt.Println("Formated file not supported: sirius.json")
os.Exit(0)
}
fmt.Println("Start converted ....")
2023-04-14 09:01:18 +03:00
err = json.Unmarshal(siriusJson, &sirius)
if err != nil {
2023-04-14 17:51:35 +03:00
fmt.Println("Error unmarshal file: sirius.json")
os.Exit(0)
}
if err := SaveToExel("sirius.xlsx", sirius.NewBolid().ZoneInfo()); err != nil {
fmt.Println("Error create file: sirius.xlsx")
os.Exit(0)
2023-04-14 09:01:18 +03:00
}
2023-04-14 17:51:35 +03:00
fmt.Println("Converted finalise. Create file: sirius.xlsx")
}
2023-04-14 09:01:18 +03:00
2023-04-14 17:51:35 +03:00
func SaveToExel(filename string, data []model.ZoneInfo) error {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
return
}
}()
// Create a new sheet.
f.SetCellValue("Sheet1", "A2", "№ зоны")
f.SetCellValue("Sheet1", "B2", "Описание зоны")
f.SetCellValue("Sheet1", "C2", "Адреса в зоне")
i := 3
for _, d := range data {
f.SetCellValue("Sheet1", "A"+strconv.Itoa(i), d.ZoneNum)
f.SetCellValue("Sheet1", "B"+strconv.Itoa(i), d.ZoneName)
f.SetCellValue("Sheet1", "C"+strconv.Itoa(i), d.InputString())
i++
}
// Save spreadsheet by the given path.
if err := f.SaveAs(filename); err != nil {
return err
}
return nil
2023-04-14 09:01:18 +03:00
}