-
Notifications
You must be signed in to change notification settings - Fork 7
/
excel2json.go
105 lines (98 loc) · 3.14 KB
/
excel2json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package excel2json
import (
"fmt"
"github.com/patrickmn/go-cache"
"strings"
"time"
)
// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 10 minutes
var localCache = cache.New(5*time.Minute, 10*time.Minute)
// GetExcelFileUrl to read all data excel
// url is string url file name
// sheetName is sheet name in excel file
// Headers is array string that is want to select specific data
func GetExcelFileUrl(url, sheetName string, headers []string) ([]*map[string]interface{}, error) {
var (
result []*map[string]interface{}
err error
byteFile []byte
keyName = hashKeyString(fmt.Sprintf(`%s||%s||%s`, url, sheetName, strings.Join(headers, "||")))
)
if byteFile, err = getFileUrl(url); err != nil {
return nil, err
}
if result, err = parseExcelFileData(byteFile, keyName, sheetName); err != nil {
return nil, err
}
if len(headers) > 0 {
result = filterDataHeaders(mapHeaders(headers), result)
}
return result, nil
}
// GetExcelFilePath to read all data excel
// path is string path file name
// sheetName is sheet name in excel file
// Headers is array string that is want to select specific data
func GetExcelFilePath(path, sheetName string, headers []string) ([]*map[string]interface{}, error) {
var (
result []*map[string]interface{}
err error
byteFile []byte
keyName = hashKeyString(fmt.Sprintf(`%s||%s||%s`, path, sheetName, strings.Join(headers, "||")))
)
if byteFile, err = getFilePath(path); err != nil {
return nil, err
}
if result, err = parseExcelFileData(byteFile, keyName, sheetName); err != nil {
return nil, err
}
if len(headers) > 0 {
result = filterDataHeaders(mapHeaders(headers), result)
}
return result, nil
}
// GetCsvFileUrl to read all data excel
// url is string url file name
// sheetName is sheet name in excel file
// Headers is array string that is want to select specific data
func GetCsvFileUrl(url, delimiter string, headers []string) ([]*map[string]interface{}, error) {
var (
result []*map[string]interface{}
err error
byteFile []byte
keyName = hashKeyString(fmt.Sprintf(`%s||%s||%s`, url, delimiter, strings.Join(headers, "||")))
)
if byteFile, err = getFileUrl(url); err != nil {
return nil, err
}
if result, err = parseCsvFileData(byteFile, delimiter, keyName); err != nil {
return nil, err
}
if len(headers) > 0 {
result = filterDataHeaders(mapHeaders(headers), result)
}
return result, nil
}
// GetCsvFilePath to read all data excel
// path is string path file name
// sheetName is sheet name in excel file
// Headers is array string that is want to select specific data
func GetCsvFilePath(path, delimiter string, headers []string) ([]*map[string]interface{}, error) {
var (
result []*map[string]interface{}
err error
byteFile []byte
keyName = hashKeyString(fmt.Sprintf(`%s||%s||%s`, path, delimiter, strings.Join(headers, "||")))
)
if byteFile, err = getFilePath(path); err != nil {
return nil, err
}
if result, err = parseCsvFileData(byteFile, delimiter, keyName); err != nil {
return nil, err
}
if len(headers) > 0 {
result = filterDataHeaders(mapHeaders(headers), result)
}
return result, nil
}