From 4b35f7c2c3edc5c2fc8a9198dca3cd4031ecc2ed Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sat, 25 Jul 2020 01:35:31 +0300 Subject: Initial --- exporter.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 exporter.go (limited to 'exporter.go') diff --git a/exporter.go b/exporter.go new file mode 100644 index 0000000..620cae2 --- /dev/null +++ b/exporter.go @@ -0,0 +1,72 @@ +/* +Copyright © 2020 Alexander Kiryukhin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package main + +import ( + "encoding/csv" + "fmt" + "os" + "strconv" + "time" +) + +const months = 48 + +func export(name string, documents []*Document) error { + var sheet [][]string + header := []string{"Облигации", "Тикер"} + now := time.Now() + for i := 0; i <= months; i++ { + year, month, _ := now.Date() + cellID := fmt.Sprintf("%02d.%d", month, year) + header = append(header, cellID) + now = now.AddDate(0, 1, 0) + } + sheet = append(sheet, header) + for _, document := range documents { + row := []string{document.Name, document.ID} + now = time.Now() + for i := 0; i <= months; i++ { + if sum, ok := document.GetByDate(now); ok { + row = append(row, strconv.FormatFloat(sum, 'f', 2, 64)) + } else { + row = append(row, "") + } + now = now.AddDate(0, 1, 0) + } + sheet = append(sheet, row) + } + f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777) + if err != nil { + return err + } + defer f.Close() + w := csv.NewWriter(f) + if err := w.WriteAll(sheet); err != nil { + return err + } + + if err := w.Error(); err != nil { + return err + } + return nil +} -- cgit v1.2.3