easy_sheet_io 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/easy_sheet_io/version.rb +1 -1
- data/lib/easy_sheet_io.rb +60 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbf69dee727f658d602e0343e142c5a09ddcbd5b810980d1bdb068591e5a1088
|
4
|
+
data.tar.gz: 239442681c1e6cc6a7a492e48ba8d07032f3bfcfe2cb59009ab1d0ca8604aa6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d88d861cc3982416bb1066e24d497cc22d9f3ee4498d35853745d4bdbc7e38d8f3aeb9a267aa6669d62b26d3a266a2598dcaec0c9a58921b5a9b3e3f9bde0b4e
|
7
|
+
data.tar.gz: 45ed813fd422636c8ef9c1e1b5323397430a785680502956762e2fd28055361b61bdf5540171401e1684bae606e32439bd745caa870976182ebed69daafdc9e5
|
data/Gemfile.lock
CHANGED
data/lib/easy_sheet_io.rb
CHANGED
@@ -17,22 +17,28 @@ module EasySheetIo
|
|
17
17
|
return /csv$/ === path ? read_csv(path, **opt) : read_excel(path, **opt)
|
18
18
|
end
|
19
19
|
|
20
|
-
# ##Generate
|
20
|
+
# ##Generate Array from CSV File, and convert it to Hash or DataFrame.
|
21
21
|
# **opt candidate= line_from: 1, header: 0
|
22
|
-
def read_csv(path, format: nil, encoding: "utf-8", **opt)
|
23
|
-
|
22
|
+
def read_csv(path, format: nil, encoding: "utf-8", col_sep: ",", **opt)
|
23
|
+
# Get 2D Array
|
24
|
+
begin
|
25
|
+
csv = CSV.parse(File.open(path, encoding: encoding, &:read), col_sep: col_sep)
|
26
|
+
rescue Encoding::InvalidByteSequenceError
|
27
|
+
# Try Another Encoding
|
28
|
+
puts "Fail Encoding #{encoding}. Trying cp932..."
|
29
|
+
csv = CSV.parse(File.open(path, encoding: "cp932", &:read), col_sep: col_sep)
|
30
|
+
end
|
31
|
+
|
24
32
|
return csv if format.nil?
|
25
33
|
|
26
34
|
ans = to_hash(csv, **opt)
|
27
35
|
return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
|
28
36
|
end
|
29
37
|
|
30
|
-
# ##Generate
|
38
|
+
# ##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
|
31
39
|
# **opt candidate= line_from: 1, header: 0)
|
32
|
-
|
33
|
-
|
34
|
-
def read_excel(path, sheet_i: 0, format: nil, **opt)
|
35
|
-
a2d = open_excel(path, sheet_i) # Get 2D Array
|
40
|
+
def read_excel(path, sheet_i: 0, format: nil, encoding: "utf-8", **opt)
|
41
|
+
a2d = open_excel(path, sheet_i, encoding: encoding) # Get 2D Array
|
36
42
|
return a2d if format.nil?
|
37
43
|
|
38
44
|
ans = to_hash(a2d, **opt)
|
@@ -41,11 +47,36 @@ module EasySheetIo
|
|
41
47
|
|
42
48
|
# Convert 2d Array to Hash
|
43
49
|
# ##header: nil -> Default Headers(:column1, column2,...) are generated.
|
44
|
-
|
45
|
-
|
46
|
-
hd = header.nil? ? [*0...(output.longest_line)].map{"column#{_1}"} : array2d[header]
|
47
|
-
output_transpose = output[0].zip(*output[1..])
|
50
|
+
# line_until=nil means the data are picked up until the end line.
|
51
|
+
def to_hash(array2d, line_from: 1, line_until: nil, header: 0)
|
48
52
|
|
53
|
+
# Define Read Range------------
|
54
|
+
lfrom, luntil = line_from, line_until
|
55
|
+
lf_reg, lu_reg = line_from.kind_of?(Regexp), line_until.kind_of?(Regexp)
|
56
|
+
|
57
|
+
if lf_reg || lu_reg
|
58
|
+
lines_ary = array2d.map{ _1.join "," }
|
59
|
+
lfrom = lines_ary.find_index{ line_from === _1 } if lf_reg
|
60
|
+
luntil = (lines_ary.length-1) - lines_ary.reverse.find_index{ line_until === _1 } if lu_reg
|
61
|
+
end
|
62
|
+
# -----------------------------
|
63
|
+
|
64
|
+
# Define Data Array------------
|
65
|
+
output = array2d[lfrom...luntil]
|
66
|
+
output_transpose = output[0].zip(*output[1..])
|
67
|
+
# -----------------------------
|
68
|
+
|
69
|
+
# Define Header----------------
|
70
|
+
if header.nil? || header=="string" || header==:string
|
71
|
+
hd = [*0...(output.longest_line)].map{"column#{_1}"}
|
72
|
+
elsif header=="symbol" || header==:symbol
|
73
|
+
hd = [*0...(output.longest_line)].map{"column#{_1}".intern}
|
74
|
+
else
|
75
|
+
hd = array2d[header]
|
76
|
+
end
|
77
|
+
# -----------------------------
|
78
|
+
|
79
|
+
# Make Hash(Header => Data Array)
|
49
80
|
return hd.each_with_object({}).with_index {|(hdr, hash), i| hash[hdr]=output_transpose[i]}
|
50
81
|
end
|
51
82
|
|
@@ -59,18 +90,26 @@ module EasySheetIo
|
|
59
90
|
end
|
60
91
|
|
61
92
|
# ##Genarate Hash from excel file
|
62
|
-
def open_excel(path, sheet_i)
|
63
|
-
|
64
|
-
|
93
|
+
def open_excel(path, sheet_i, encoding: "utf-8")
|
94
|
+
if /xlsx$/ === path
|
95
|
+
puts "Sorry, encoding option is not supported yet for xlsx file." if encoding != "utf-8"
|
96
|
+
|
97
|
+
book = Roo::Excelx.new(path)
|
65
98
|
s = book.sheet(sheet_i)
|
66
99
|
|
67
|
-
## bottole neck
|
100
|
+
## bottole neck
|
68
101
|
return s.to_a
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
102
|
+
|
103
|
+
# xls
|
104
|
+
else
|
105
|
+
begin
|
106
|
+
Spreadsheet.client_encoding = encoding
|
107
|
+
ss = Spreadsheet.open(path)
|
108
|
+
rescue Encoding::InvalidByteSequenceError
|
109
|
+
puts "Fail Encoding #{encoding}. Trying Windows-31J..."
|
110
|
+
Spreadsheet.client_encoding = "Windows-31J"
|
111
|
+
ss = Spreadsheet.open(path)
|
112
|
+
end
|
74
113
|
|
75
114
|
a2d = []
|
76
115
|
ss.worksheets[sheet_i].rows.each do |row|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_sheet_io
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- show-o-atakun
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A simple way to Open .csv, .xls, .xlsx files. You can convert it to 2D
|
14
14
|
Array, Hash, Dataframe.
|