From 7931226b762ddd6222312226e99234f1f25f1047 Mon Sep 17 00:00:00 2001 From: Precious Nyasulu Date: Fri, 12 Apr 2024 08:46:38 +0000 Subject: [PATCH 1/5] Feat(reader): optional function to read xlfile by sheetname --- reader/reader.go | 54 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/reader/reader.go b/reader/reader.go index 76b6823..f7672be 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -9,15 +9,18 @@ import ( "time" ) -func ReadFile(filePath string, sheetStruct interface{}, skipHeader bool, sheetNumber ...int) (interface{}, error) { +var sheet *xlsx.Sheet +var sheetStruct interface{} +var _skipHeader bool +func ReadFileDefault(filePath string, customStruct interface{}, skipHeader bool, sheetNumber ...int) (interface{}, error) { + sheetStruct = customStruct + _skipHeader = skipHeader xlFile, err := xlsx.OpenFile(filePath) if err != nil { return nil, err } - var sheet *xlsx.Sheet - if len(sheetNumber) > 0 { sheet, err = getSheetByNumber(xlFile, sheetNumber[0]) if err != nil { @@ -27,6 +30,36 @@ func ReadFile(filePath string, sheetStruct interface{}, skipHeader bool, sheetNu sheet = xlFile.Sheets[0] } + data, err := read(sheet) + if err != nil { + return nil, err + } + + return data, nil +} + +func ReadFileWithSheetName(filePath string, customStruct interface{}, skipHeader bool, sheetName string) (interface{}, error) { + sheetStruct = customStruct + _skipHeader = skipHeader + xlFile, err := xlsx.OpenFile(filePath) + if err != nil { + return nil, err + } + + sheet, err = getSheetByName(xlFile, sheetName) + if err != nil { + return nil, err + } + + data, err := read(sheet) + if err != nil { + return nil, err + } + + return data, nil +} + +func read(sheet *xlsx.Sheet) (interface{}, error) { // Get the type of struct templateType := reflect.TypeOf(sheetStruct) @@ -37,7 +70,7 @@ func ReadFile(filePath string, sheetStruct interface{}, skipHeader bool, sheetNu // Iterate through rows for r_i, row := range sheet.Rows { // Skip header if - if skipHeader && r_i == 0 { + if _skipHeader && r_i == 0 { continue } @@ -48,7 +81,7 @@ func ReadFile(filePath string, sheetStruct interface{}, skipHeader bool, sheetNu // Get the field corresponding to the current column index field := newStruct.FieldByIndex([]int{i}) if field.IsValid() { - err = handleFieldKind(field, cell) + err := handleFieldKind(field, cell) if err != nil { return nil, err } @@ -69,7 +102,14 @@ func getSheetByNumber(xlFile *xlsx.File, sheetNumber int) (*xlsx.Sheet, error) { if isValidSheetNumber(sheetNumber, len(xlFile.Sheets)) { return xlFile.Sheets[sheetNumber], nil } - return nil, fmt.Errorf("Invalid sheet number: %v", sheetNumber) + return nil, fmt.Errorf("invalid sheet number: %v", sheetNumber) +} + +func getSheetByName(xlFile *xlsx.File, sheetName string) (*xlsx.Sheet, error) { + if xlFile.Sheet[sheetName] != nil { + return xlFile.Sheet[sheetName], nil + } + return nil, fmt.Errorf("invalid sheet name: %s", sheetName) } // handleFieldKind: Convert cell value based on the field type @@ -103,7 +143,7 @@ func handleFieldKind(field reflect.Value, cell *xlsx.Cell) error { } } } else { - fmt.Errorf("Unsupported type for pointer: %v", field.Type().Elem().Kind()) + return fmt.Errorf("unsupported type for pointer: %v", field.Type().Elem().Kind()) } case reflect.Struct: From 078f57c2a26b2d383fff61d4e6720b55c7874a8d Mon Sep 17 00:00:00 2001 From: Precious Nyasulu Date: Fri, 12 Apr 2024 08:57:22 +0000 Subject: [PATCH 2/5] ref: renamed func to something more meaningful --- reader/reader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reader/reader.go b/reader/reader.go index f7672be..282bf76 100644 --- a/reader/reader.go +++ b/reader/reader.go @@ -38,7 +38,7 @@ func ReadFileDefault(filePath string, customStruct interface{}, skipHeader bool, return data, nil } -func ReadFileWithSheetName(filePath string, customStruct interface{}, skipHeader bool, sheetName string) (interface{}, error) { +func ReadFileBySheetName(filePath string, customStruct interface{}, skipHeader bool, sheetName string) (interface{}, error) { sheetStruct = customStruct _skipHeader = skipHeader xlFile, err := xlsx.OpenFile(filePath) From 305721824e9c6ce2686759db7a53feed4e2e2956 Mon Sep 17 00:00:00 2001 From: Precious Nyasulu Date: Fri, 12 Apr 2024 09:03:45 +0000 Subject: [PATCH 3/5] ref: Updated readme file, adopting new code changes --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 283dee6..2b96242 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,20 @@ import ( ) func main() { - data, err := structurama.ReadFile("./example.xlsx", Person{}, true) + data, err := structurama.ReadFileDefault("./example.xlsx", Person{}, true) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(data) +} +``` + +Here is how you can read file by Sheetname: + +```go +func main() { + data, err := structurama.ReadFileBySheetName("./example.xlsx", Person{}, true,"Sheet1") if err != nil { fmt.Println(err) return @@ -53,7 +66,7 @@ func main() { ```go func main() { - data, err := structurama.ReadFile("./example.xlsx", Person{}, true) + data, err := structurama.ReadFileDefault("./example.xlsx", Person{}, true) if err != nil { fmt.Println(err) return @@ -73,6 +86,9 @@ func main() { Contributions are welcome! Feel free to open issues or submit pull requests. +## Inspiration +[tealeg/xlsx](github.com/tealeg/xlsx) - Go library for reading and writing XLSX files. + ## License This package is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. From a08b2021b4b1ec70bf7975aa1213f22305c6c9d7 Mon Sep 17 00:00:00 2001 From: Precious Nyasulu Date: Fri, 12 Apr 2024 09:06:03 +0000 Subject: [PATCH 4/5] ref: updated example code, adopting new code changes --- example/main.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/example/main.go b/example/main.go index 6db7683..1d2dc36 100644 --- a/example/main.go +++ b/example/main.go @@ -5,8 +5,15 @@ import ( "github.com/PreciousNyasulu/structurama/reader" ) +type Person struct { + Id string `xlsx:"0"` + FirstName string `xlsx:"1"` + LastName string `xlsx:"2"` + Age string `xlsx:"3"` +} + func main() { - data, err := structurama.ReadFile("./example.xlsx", Person{}, true) + data, err := structurama.ReadFileDefault("./example.xlsx", Person{}, true) if err != nil { fmt.Println(err) return @@ -19,11 +26,4 @@ func main() { fmt.Println(item.Id, item.FirstName, item.LastName, item.Age) } } -} - -type Person struct { - Id string `xlsx:"0"` - FirstName string `xlsx:"1"` - LastName string `xlsx:"2"` - Age string `xlsx:"3"` -} +} \ No newline at end of file From 3dee7b3ffff91d0d31c3efa5cf6e4af9bbf217f1 Mon Sep 17 00:00:00 2001 From: Precious Nyasulu Date: Fri, 12 Apr 2024 09:11:42 +0000 Subject: [PATCH 5/5] ref(README): fixed broken link, added absolute library link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b96242..f294d01 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ func main() { Contributions are welcome! Feel free to open issues or submit pull requests. ## Inspiration -[tealeg/xlsx](github.com/tealeg/xlsx) - Go library for reading and writing XLSX files. +[tealeg/xlsx](https://github.com/tealeg/xlsx) - Go library for reading and writing XLSX files. ## License