company facts simple read out of xls works

This commit is contained in:
Andreas Schröpfer
2021-02-28 08:33:00 +01:00
parent e0faa1fc7c
commit 90af0db862
11 changed files with 197 additions and 170 deletions

View File

@@ -1,10 +1,28 @@
package loader
import (
"bytes"
_ "embed"
"encoding/json"
"io"
"log"
)
//go:embed conf/default.json
var defaultConf []byte
// DefaultConf loads the defaultConf of the package. The base
// is the default.json inside the package definition.
func DefaultConf() *Conf {
buf := bytes.NewBuffer(defaultConf)
conf := &Conf{}
err := conf.DecodeJSON(buf)
if err != nil {
log.Println("cannot decode default conf: ", err)
}
return conf
}
// Conf configures the mapping from the excel version
// to the eCalc structure
type Conf struct {
@@ -15,6 +33,20 @@ type Conf struct {
Rating Rating `json:"rating"`
}
// EncodeJSON writes the JSON of the conf into the Writer
func (c Conf) EncodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(c)
}
// DecodeJSON writes the configuration from the reader into
// the pointer of the conf
func (c *Conf) DecodeJSON(r io.Reader) error {
dec := json.NewDecoder(r)
return dec.Decode(c)
}
// Value defines a single value inside the excel workbook
type Value struct {
Sheet int `json:"sheet"`
@@ -61,17 +93,3 @@ type Rating struct {
WeightCol string `json:"weight_col"`
SelectedByUserCol string `json:"selected_by_user_col"`
}
// EncodeJSON writes the JSON of the conf into the Writer
func (c Conf) EncodeJSON(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(c)
}
// DecodeJSON writes the configuration from the reader into
// the pointer of the conf
func (c *Conf) DecodeJSON(r io.Reader) error {
dec := json.NewDecoder(r)
return dec.Decode(c)
}

View File

@@ -0,0 +1,198 @@
{
"title": "Good Balance Calculator",
"version": "5.04",
"values": [
{
"sheet": 2,
"cell": "C7",
"type": "int",
"struct": "CompanyFacts",
"field": "TotalPurchaseFromSuppliers",
"default": ""
},
{
"sheet": 2,
"cell": "C27",
"type": "int",
"struct": "CompanyFacts",
"field": "TotalStaffCosts",
"default": ""
},
{
"sheet": 2,
"cell": "C18",
"type": "int",
"struct": "CompanyFacts",
"field": "Profit",
"default": ""
},
{
"sheet": 2,
"cell": "C19",
"type": "int",
"struct": "CompanyFacts",
"field": "FinancialCosts",
"default": ""
},
{
"sheet": 2,
"cell": "C20",
"type": "int",
"struct": "CompanyFacts",
"field": "IncomeFromFinancialInvestments",
"default": ""
},
{
"sheet": 2,
"cell": "C22",
"type": "int",
"struct": "CompanyFacts",
"field": "AdditionsToFixedAssets",
"default": ""
},
{
"sheet": 2,
"cell": "C37",
"type": "int",
"struct": "CompanyFacts",
"field": "Turnover",
"default": ""
},
{
"sheet": 2,
"cell": "C21",
"type": "int",
"struct": "CompanyFacts",
"field": "TotalAssets",
"default": ""
},
{
"sheet": 2,
"cell": "C23",
"type": "int",
"struct": "CompanyFacts",
"field": "FinancialAssetsAndCashBalance",
"default": ""
},
{
"sheet": 2,
"cell": "C26",
"type": "int",
"struct": "CompanyFacts",
"field": "NumberOfEmployees",
"default": ""
},
{
"sheet": 2,
"cell": "C24",
"type": "bool",
"struct": "CompanyFacts",
"field": "HasCanteen",
"default": ""
},
{
"sheet": 2,
"cell": "C38",
"type": "bool",
"struct": "CompanyFacts",
"field": "IsB2B",
"default": ""
},
{
"sheet": 2,
"cell": "C33",
"type": "int",
"struct": "CompanyFacts",
"field": "AverageJourneyToWorkForStaffInKm",
"default": ""
}
],
"areas": [
{
"sheet": 2,
"start_row": 10,
"end_row": 14,
"struct": "SupplyFraction",
"cols": [
{
"col": "B",
"field": "IndustryCode",
"type": "string",
"default": ""
},
{
"col": "D",
"field": "CountryCode",
"type": "string",
"default": ""
},
{
"col": "E",
"field": "Costs",
"type": "string",
"default": ""
}
]
},
{
"sheet": 2,
"start_row": 30,
"end_row": 32,
"struct": "EmployeesFraction",
"cols": [
{
"col": "B",
"field": "CountryCode",
"type": "string",
"default": ""
},
{
"col": "D",
"field": "Percentage",
"type": "float64",
"default": ""
}
]
},
{
"sheet": 2,
"start_row": 41,
"end_row": 43,
"struct": "IndustrySector",
"cols": [
{
"col": "B",
"field": "IndustryCode",
"type": "string",
"default": ""
},
{
"col": "C",
"field": "Description",
"type": "string",
"default": ""
},
{
"col": "D",
"field": "AmountOfTotalTurnover",
"type": "int",
"default": ""
}
]
}
],
"rating": {
"sheet": 3,
"start_row": 9,
"end_row": 93,
"points_col": "I",
"max_points_col": "J",
"id_col": "",
"short_name_col": "B",
"name_col": "C",
"estimations_col": "H",
"weight_col": "D",
"selected_by_user_col": "N"
}
}

99
pkg/loader/loader.go Normal file
View File

@@ -0,0 +1,99 @@
package loader
import (
"fmt"
"io"
"git.ecogood.org/andreas.schroepfer/excelConverter/pkg/ecalc"
"git.ecogood.org/andreas.schroepfer/excelConverter/pkg/set"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)
// XLSX reads the data out of the reader. If conf is nil
// the default configuration is used.
func XLSX(r io.Reader, conf *Conf) (*ecalc.Ecalc, error) {
eBalance := &ecalc.Ecalc{}
var errs []error
if conf == nil {
conf = DefaultConf()
}
xFile, err := excelize.OpenReader(r)
if err != nil {
return nil, fmt.Errorf("XLSX.OpenReader: %w", err)
}
for _, v := range conf.Values {
cellValue, err := xFile.GetCellValue(
xFile.GetSheetName(v.Sheet),
v.Cell,
)
if err != nil {
errs = append(errs, err)
}
switch v.Struct {
case "CompanyFacts":
set.Field(&eBalance.CompanyFacts, v.Field, cellValue)
}
}
for _, a := range conf.Areas {
switch a.Struct {
case "SupplyFraction":
for r := a.StartRow; r <= a.EndRow; r++ {
sf := ecalc.SupplyFraction{}
for _, c := range a.Cols {
cellValue, err := xFile.GetCellValue(
xFile.GetSheetName(a.Sheet),
fmt.Sprintf("%s%d", c.Col, r),
)
if err != nil {
errs = append(errs, err)
}
set.Field(&sf, c.Field, cellValue)
}
eBalance.CompanyFacts.SupplyFractions =
append(
eBalance.CompanyFacts.SupplyFractions,
sf)
}
case "EmployeesFraction":
for r := a.StartRow; r <= a.EndRow; r++ {
ef := ecalc.EmployeesFraction{}
for _, c := range a.Cols {
cellValue, err := xFile.GetCellValue(
xFile.GetSheetName(a.Sheet),
fmt.Sprintf("%s%d", c.Col, r),
)
if err != nil {
errs = append(errs, err)
}
set.Field(&ef, c.Field, cellValue)
}
eBalance.CompanyFacts.EmployeesFractions =
append(
eBalance.CompanyFacts.EmployeesFractions,
ef)
}
case "IndustrySector":
for r := a.StartRow; r <= a.EndRow; r++ {
is := ecalc.IndustrySector{}
for _, c := range a.Cols {
cellValue, err := xFile.GetCellValue(
xFile.GetSheetName(a.Sheet),
fmt.Sprintf("%s%d", c.Col, r),
)
if err != nil {
errs = append(errs, err)
}
set.Field(&is, c.Field, cellValue)
}
eBalance.CompanyFacts.IndustrySectors =
append(
eBalance.CompanyFacts.IndustrySectors,
is)
}
}
}
// TODO: error handling of errs
return eBalance, nil
}