easy_sheet_io 0.1.2 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b4655b4dbf3e5a8bbb152f70018068103bc335f4f2d855b82bf0ecb27eb045c
4
- data.tar.gz: a07f5bb08b09eb724b8a38d67dc7fe6e571ed040d558e76cff2a164850e28e09
3
+ metadata.gz: 3fe769a46f695624ce4e05905ed33313b749c5a77546ac545f723ff63950f430
4
+ data.tar.gz: abc63c91428ae99b41c80ed9846ae82970a1f43bc314a3b3d381f32cbf850a58
5
5
  SHA512:
6
- metadata.gz: 883e6250a702be56a3a072129494bd8650fcb09db21ba2251fc9668dc078d0f3b7747889441702104a5772b2452ec85d00bd580c2f982db66bd33a496739f8db
7
- data.tar.gz: 73d9b766d85d9489bfb5806fd0bb2e1ebba7333cb17511358ff0a0bf1541d664707193debf4097c394b35e126789fb1ec07ef37aefc4229c7cddb946cf36332f
6
+ metadata.gz: 8d75d2aa249909cd5310d2cd7ba360daced800dbcba7a57d3e53050d60a2318720289532c7cfe74f5ccf29fbe8b137debce92ec20d565105c436395b909fb0a3
7
+ data.tar.gz: 3ebddcd9ad90b22a43ab6a039029b79b91a897f6f8b4c3a6bbc547f5c878af6dc0e7e5287c379f055d3e0b0accbbffd460bb164b779b7a750f91148c1603c7cf
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_sheet_io (0.1.0)
4
+ easy_sheet_io (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["shun_yamaguchi_tc@live.jp"]
10
10
 
11
11
  spec.summary = "A simple way to Open .csv, .xls, .xlsx files."
12
- spec.description = "A simple way to Open .csv, .xls, .xlsx files. You can convert it to 2D Array, Hash, Dataframe."
12
+ spec.description = "A simple way to Open .csv, .xls, .xlsx files. You can convert it to 2D array, hash, data frame."
13
13
  spec.homepage = "https://github.com/show-o-atakun/easy_sheet_io"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasySheetIo
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/easy_sheet_io.rb CHANGED
@@ -17,29 +17,66 @@ module EasySheetIo
17
17
  return /csv$/ === path ? read_csv(path, **opt) : read_excel(path, **opt)
18
18
  end
19
19
 
20
- # ##Generate DF from CSV File
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: :hash, **opt)
23
- csv = CSV.parse(File.open path, &:read) # Get 2D Array
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
+
24
34
  ans = to_hash(csv, **opt)
25
35
  return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
26
36
  end
27
37
 
28
- # ##Generate DF from Excel File
38
+ # ##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
29
39
  # **opt candidate= line_from: 1, header: 0)
30
- def read_excel(path, sheet_i: 0, format: :hash, **opt)
31
- 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
+
32
44
  ans = to_hash(a2d, **opt)
33
45
  return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
34
46
  end
35
47
 
36
48
  # Convert 2d Array to Hash
37
49
  # ##header: nil -> Default Headers(:column1, column2,...) are generated.
38
- def to_hash(array2d, line_from: 1, header: 0)
39
- output = array2d[line_from..]
40
- hd = header.nil? ? [*0...(output.longest_line)].map{"column#{_1}"} : array2d[header]
41
- 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)
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)
42
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)
43
80
  return hd.each_with_object({}).with_index {|(hdr, hash), i| hash[hdr]=output_transpose[i]}
44
81
  end
45
82
 
@@ -53,18 +90,26 @@ module EasySheetIo
53
90
  end
54
91
 
55
92
  # ##Genarate Hash from excel file
56
- def open_excel(path, sheet_i)
57
- begin
58
- book = /xlsx$/ === path ? Roo::Excelx.new(path) : Roo::Excel.new(path)
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)
59
98
  s = book.sheet(sheet_i)
60
99
 
61
- ## bottole neck===
100
+ ## bottole neck
62
101
  return s.to_a
63
-
64
- rescue Encoding::InvalidByteSequenceError
65
-
66
- Spreadsheet.client_encoding="Windows-31J"
67
- ss = Spreadsheet.open(path)
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
68
113
 
69
114
  a2d = []
70
115
  ss.worksheets[sheet_i].rows.each do |row|
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_sheet_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
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-18 00:00:00.000000000 Z
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
- Array, Hash, Dataframe.
14
+ array, hash, data frame.
15
15
  email:
16
16
  - shun_yamaguchi_tc@live.jp
17
17
  executables: []