easy_sheet_io 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 791e3662406be26d6afb90f394f0d585b7c9e956d4cdc200db484be820af4eb8
4
- data.tar.gz: 7796eacac61646ec19c3d452b9ff738de37546b2a7360374a9f9d5fd9fabda8a
3
+ metadata.gz: fbf69dee727f658d602e0343e142c5a09ddcbd5b810980d1bdb068591e5a1088
4
+ data.tar.gz: 239442681c1e6cc6a7a492e48ba8d07032f3bfcfe2cb59009ab1d0ca8604aa6d
5
5
  SHA512:
6
- metadata.gz: 991506127b66bfdd211f99ec00554dcca3ad521274d3c712389d3f346cf35f4d0b1c5e0478817f9ead1a915aa89c34a88abf49502ee054d30a41a02f19068510
7
- data.tar.gz: 6eb892fa842a1a522e939c432e66c0aef9384b37b4bae3b454bb9a82628a2dbd3b3c1cb3d13192b71cf915a7b886858fbb31331b5fa6f03a148baacefd5a611b
6
+ metadata.gz: d88d861cc3982416bb1066e24d497cc22d9f3ee4498d35853745d4bdbc7e38d8f3aeb9a267aa6669d62b26d3a266a2598dcaec0c9a58921b5a9b3e3f9bde0b4e
7
+ data.tar.gz: 45ed813fd422636c8ef9c1e1b5323397430a785680502956762e2fd28055361b61bdf5540171401e1684bae606e32439bd745caa870976182ebed69daafdc9e5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_sheet_io (0.1.3)
4
+ easy_sheet_io (0.1.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasySheetIo
4
- VERSION = "0.1.4"
4
+ VERSION = "0.2.0"
5
5
  end
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 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: nil, encoding: "utf-8", **opt)
23
- csv = CSV.parse(File.open path, encoding: encoding, &: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
+
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 DF from Excel File
38
+ # ##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
31
39
  # **opt candidate= line_from: 1, header: 0)
32
- # !encoding parameter is not allowed yet
33
- # !(Finally, I want to make it automatically recognize encoding of file).
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
- def to_hash(array2d, line_from: 1, line_until: -1, header: 0)
45
- output = array2d[line_from..line_until]
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
- begin
64
- 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)
65
98
  s = book.sheet(sheet_i)
66
99
 
67
- ## bottole neck===
100
+ ## bottole neck
68
101
  return s.to_a
69
-
70
- rescue Encoding::InvalidByteSequenceError
71
-
72
- Spreadsheet.client_encoding="Windows-31J"
73
- 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
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.1.4
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-22 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
14
  Array, Hash, Dataframe.