easy_sheet_io 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/easy_sheet_io/version.rb +1 -1
- data/lib/easy_sheet_io.rb +66 -22
- 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
@@ -4,9 +4,8 @@ require "roo-xls"
|
|
4
4
|
require "spreadsheet"
|
5
5
|
require "rover"
|
6
6
|
require "daru"
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
require_relative "./to_csv"
|
8
|
+
require_relative "./longest_line"
|
10
9
|
require_relative "easy_sheet_io/version"
|
11
10
|
|
12
11
|
module EasySheetIo
|
@@ -18,29 +17,66 @@ module EasySheetIo
|
|
18
17
|
return /csv$/ === path ? read_csv(path, **opt) : read_excel(path, **opt)
|
19
18
|
end
|
20
19
|
|
21
|
-
# ##Generate
|
20
|
+
# ##Generate Array from CSV File, and convert it to Hash or DataFrame.
|
22
21
|
# **opt candidate= line_from: 1, header: 0
|
23
|
-
def read_csv(path, format: :
|
24
|
-
|
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
|
+
|
32
|
+
return csv if format.nil?
|
33
|
+
|
25
34
|
ans = to_hash(csv, **opt)
|
26
35
|
return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
|
27
36
|
end
|
28
37
|
|
29
|
-
# ##Generate
|
38
|
+
# ##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
|
30
39
|
# **opt candidate= line_from: 1, header: 0)
|
31
|
-
def read_excel(path, sheet_i: 0, format: :
|
32
|
-
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
|
42
|
+
return a2d if format.nil?
|
43
|
+
|
33
44
|
ans = to_hash(a2d, **opt)
|
34
45
|
return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
|
35
46
|
end
|
36
47
|
|
37
48
|
# Convert 2d Array to Hash
|
38
49
|
# ##header: nil -> Default Headers(:column1, column2,...) are generated.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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)
|
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)
|
43
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)
|
44
80
|
return hd.each_with_object({}).with_index {|(hdr, hash), i| hash[hdr]=output_transpose[i]}
|
45
81
|
end
|
46
82
|
|
@@ -54,18 +90,26 @@ module EasySheetIo
|
|
54
90
|
end
|
55
91
|
|
56
92
|
# ##Genarate Hash from excel file
|
57
|
-
def open_excel(path, sheet_i)
|
58
|
-
|
59
|
-
|
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)
|
60
98
|
s = book.sheet(sheet_i)
|
61
99
|
|
62
|
-
## bottole neck
|
100
|
+
## bottole neck
|
63
101
|
return s.to_a
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
69
113
|
|
70
114
|
a2d = []
|
71
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.
|