easy_sheet_io 0.2.4 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88df6087923b6ed328df091fa6f6fdf2b833e5a7d96af9a386db127b13b2e3cd
4
- data.tar.gz: c841d5ed1f39467ee30a3904cb546951c23178343b3f019aa2c8fd9a8872d7a0
3
+ metadata.gz: ec3308eccf2e355a8c32cf0a3496425522f793633d84203fb6378b460c99b6e0
4
+ data.tar.gz: 0e00581c782acbbe52883dc0b7e298158854ac4fe992e2fc825c0c766b1f0c67
5
5
  SHA512:
6
- metadata.gz: 2eb46d668a0e4465e1f93c6617fd0a2212f41b9b0e90b4d0e566d0f25129779914860f970cdf8491ca7fdbcdc5df598d0c5a496097ae3ee1cf62afed95daef2c
7
- data.tar.gz: e0ec1d00e0852e70cc4be1ef90d981353803fa796fc9e763631687a4980421b650db1ac6e3c119460e7eabec4313374eee788b0b15768b8c12857f95f5d3a1e2
6
+ metadata.gz: 54a4b942475d211a2a5d6e9cd3621ed94366f6f4ea313c6eb338b9e10ee426f5bee961de91bab5d31e1b5f3cd3b84bd8636cd002af22122f5140c8303741605f
7
+ data.tar.gz: 76caa06c1a889fb1e70c6b6b2b5a8e8a483811d24380708443fc527305da8931b5c2f42728d4d82df0e426f7a48e8a992d52896ae77f9af4e7559b3fb7115b77
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_sheet_io (0.2.3)
4
+ easy_sheet_io (0.2.4)
5
5
  daru (>= 0.3)
6
6
  rake (~> 13.0)
7
7
  roo-xls (>= 1.2.0)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasySheetIo
4
- VERSION = "0.2.4"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/easy_sheet_io.rb CHANGED
@@ -51,7 +51,7 @@ module EasySheetIo
51
51
  ## Option line_ignored is not implemented yet.
52
52
  def to_hash(array2d, line_from: 1, line_until: nil, line_ignored: nil,
53
53
  header: 0, symbol_header: false,
54
- replace_to_nil: [], analyze_type: false)
54
+ replace_to_nil: [], analyze_type: true)
55
55
 
56
56
  # Define Read Range------------
57
57
  lfrom, luntil = line_from, line_until
@@ -66,8 +66,8 @@ module EasySheetIo
66
66
 
67
67
  # Define Data Array------------
68
68
  output = array2d[lfrom...luntil]
69
- output = fix_array(output, replace_to_nil, analyze_type)
70
69
  output_transpose = output[0].zip(*output[1..])
70
+ output_transpose = fix_array(output_transpose, replace_to_nil, analyze_type)
71
71
  # -----------------------------
72
72
 
73
73
  # Define Header----------------
@@ -129,19 +129,49 @@ module EasySheetIo
129
129
  def fix_array(array2d, replace_to_nil, analyze_type)
130
130
  ans = array2d
131
131
 
132
- if replace_to_nil.length > 0
133
- ans = ans.map { _1.map {|cell| replace_to_nil.include?(cell) ? nil : cell } }
132
+ ## Replace Blank or User-Selected Value
133
+ ans = ans.map do |column|
134
+ column.map { |cell| replace_to_nil.include?(cell) || /^\s*$/ === cell ? nil : cell }
134
135
  end
135
-
136
- # if analyze_type
137
- # ans = ans.map do |column|
136
+
137
+ ## Replace Number Values to Integer or Float
138
+ if analyze_type
139
+ ans = ans.map.with_index do |column, i|
140
+ type_of_column = :any
141
+ column.each { |cell| type_of_column = recognize_type(cell, type_of_column) }
138
142
 
139
- # end
140
- # end
143
+ # p type_of_column
144
+ case type_of_column
145
+ when :int
146
+ column.map { _1.nil? ? nil : _1.to_i }
147
+ when :float
148
+ column.map { _1.nil? ? nil : _1.to_f }
149
+ else
150
+ column
151
+ end
152
+ end
153
+ end
141
154
 
142
155
  return ans
143
156
  end
144
157
 
158
+ def recognize_type(str, expected)
159
+ return expected if str.nil?
160
+
161
+ order = {:any => 0, :int => 1, :float => 2, :string => 3}
162
+ if /^\s*(-|\+)?\d+\s*$/ === str
163
+ type_of_str = :int
164
+ elsif /^\s*(-|\+)?\d*\.\d*\s*$/ === str || /^\s*(-|\+)?(\d*\.\d+|\d+)(e|E)(-|\+)?\d+\s*$/ === str
165
+ type_of_str = :float
166
+ else
167
+ type_of_str = :string
168
+ end
169
+
170
+ # p "#{type_of_str}, #{str}" if order[type_of_str] > order[expected]
171
+
172
+ return order[type_of_str] > order[expected] ? type_of_str : expected
173
+ end
174
+
145
175
  # Fix blank or duplicated header
146
176
  def check_header(header_array)
147
177
  # Check Blank
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_sheet_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - show-o-atakun