From 9bf0b05ec66f07b487e6f4c72666e907e482f459 Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Wed, 12 Apr 2023 11:11:29 +0000 Subject: [PATCH] feat: adds overview sheet with chart --- internal/dirtoexcel/dirtoexcel.go | 43 +++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/internal/dirtoexcel/dirtoexcel.go b/internal/dirtoexcel/dirtoexcel.go index 3c6a89a..f8d29bf 100644 --- a/internal/dirtoexcel/dirtoexcel.go +++ b/internal/dirtoexcel/dirtoexcel.go @@ -119,13 +119,15 @@ func Create(out, dir string) error { } } - for cat, item := range c { - if err := f.DeleteSheet("Sheet1"); err != nil { - return fmt.Errorf("failed to remove default sheet: %w", err) - } + if _, err := f.NewSheet("Overview"); err != nil { + return fmt.Errorf("failed to add overview sheet: %w", err) + } - _, err := f.NewSheet(cat) - if err != nil { + catLength := 0 + + for cat, item := range c { + + if _, err := f.NewSheet(cat); err != nil { return fmt.Errorf("failed to create new sheet: %w", err) } @@ -134,24 +136,43 @@ func Create(out, dir string) error { m[i.Company] += i.Value } - length := 0 + valueLength := 0 for k, v := range m { - if err := f.SetCellStr(cat, fmt.Sprintf("A%d", length+1), k); err != nil { + if err := f.SetCellStr(cat, fmt.Sprintf("A%d", valueLength+1), k); err != nil { return fmt.Errorf("failed to set cell: %w", err) } - if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", length+1), v, 2, 64); err != nil { + if err := f.SetCellFloat(cat, fmt.Sprintf("B%d", valueLength+1), v, 2, 64); err != nil { return fmt.Errorf("failed to set cell: %w", err) } - length += 1 + valueLength += 1 } - if err := createCategoryChart(cat, length, f); err != nil { + if err := createCategoryChart(cat, valueLength, f); err != nil { return fmt.Errorf("failed to create category chart: %w", err) } + // Add to overview. + if err := f.SetCellStr("Overview", fmt.Sprintf("A%d", catLength+1), cat); err != nil { + return fmt.Errorf("failed to set category to overview: %w", err) + } + + if err := f.SetCellFormula("Overview", fmt.Sprintf("B%d", catLength+1), fmt.Sprintf("=SUM(%s!B1:B%d)", cat, valueLength)); err != nil { + return fmt.Errorf("failed to set sum to overview: %w", err) + } + + if err := createCategoryChart("Overview", catLength+1, f); err != nil { + return fmt.Errorf("failed to create overview chat: %w", err) + } + + catLength += 1 + + } + + if err := f.DeleteSheet("Sheet1"); err != nil { + return fmt.Errorf("failed to remove default sheet: %w", err) } if err := f.SaveAs(out); err != nil {