Skip to content

Commit

Permalink
feat: add function convert json array to multiple sheets
Browse files Browse the repository at this point in the history
songsit.pantayang committed Oct 26, 2024
1 parent fa639fa commit 5c3a4b0
Showing 4 changed files with 70 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.idea
example
example
output.xlsx
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ type Person struct {
}
```

## Using for Convert
## Using for Convert single sheet

```go
m := []MyStruct{
@@ -34,6 +34,21 @@ m := []MyStruct{
file, err := excelx.Convert[MyStruct](m)
```

## Using for Convert multiple sheets

```go
m := []MyStruct{
{"John Doe", 25, "New York", "555"},
{"Jane Doe", 30, "San Francisco", "555"},
{"Bob Smith", 22, "Chicago", "555"},
}
sheets := []Sheet[MyStruct]{
{Name: "Sheet1", Data: m},
{Name: "Sheet2", Data: m},
}
file, err := excelx.Converts[MyStruct](sheets)
```

## Save the Excel file to the response writer

```go
31 changes: 29 additions & 2 deletions excelx.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@ type model[T any] struct {
Data T
}

type Sheet[T any] struct {
Name string
Data []T
}

type Options struct {
Options *excelize.Options
SheetName string
@@ -407,6 +412,30 @@ func Convert[T any](data []T, sheetName ...string) (*excelize.File, error) {
if len(sheetName) > 0 {
sheet = sheetName[0]
}

NewSheet(file, sheet, data)

return file, nil
}

// Converts array struct to excel format
func Converts[T any](sheets []Sheet[T]) (*excelize.File, error) {
if len(sheets) == 0 {
return nil, errors.New("sheets is empty")
}

// Create a new Excel file
file := excelize.NewFile()

// Create a new sheet
for _, sheet := range sheets {
NewSheet(file, sheet.Name, sheet.Data)
}

return file, nil
}

func NewSheet[T any](file *excelize.File, sheet string, data []T) {
_, _ = file.NewSheet(sheet)

// Use reflection to get struct field names and sort them by the "no" tag
@@ -439,8 +468,6 @@ func Convert[T any](data []T, sheetName ...string) (*excelize.File, error) {
_ = file.SetCellValue(sheet, cell, v.FieldByName(field.Name).Interface())
}
}

return file, nil
}

func ResponseWriter(file *excelize.File, w http.ResponseWriter, filename string) error {
23 changes: 23 additions & 0 deletions excelx_test.go
Original file line number Diff line number Diff line change
@@ -77,6 +77,29 @@ func TestConvertIsNotEmpty(t *testing.T) {
}
}

func TestConvertsIsNotEmpty(t *testing.T) {
// Given
persons := []Person{
{"John Doe", 25, "New York", "555"},
{"Jane Doe", 30, "San Francisco", "555"},
{"Bob Smith", 22, "Chicago", "555"},
}
sheets := []excelx.Sheet[Person]{
{Name: "Sheet1", Data: persons},
{Name: "Sheet2", Data: persons},
}

// When
file, err := excelx.Converts[Person](sheets)

// Then
if err != nil {
t.Error("Error", err)
} else {
_ = file.SaveAs("output.xlsx")
}
}

func TestConvertIsEmpty(t *testing.T) {
// Given
persons := []Person{}

0 comments on commit 5c3a4b0

Please sign in to comment.