easy_sheet_io 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cf3f5ae0805f655b09de03abc6465131b4d9f4eba1b00bed205ef023cd0d811
4
- data.tar.gz: 8862de3262a47dcce8ed470f3e03ddcd1059a452b11556977dcb0109fd184dce
3
+ metadata.gz: fbf69dee727f658d602e0343e142c5a09ddcbd5b810980d1bdb068591e5a1088
4
+ data.tar.gz: 239442681c1e6cc6a7a492e48ba8d07032f3bfcfe2cb59009ab1d0ca8604aa6d
5
5
  SHA512:
6
- metadata.gz: b420128080ca86db35e08fdc9a72227016c16673118e3c73508388f9c223686066dc585ff6676dcde4633e0f4b13a0e0a194079c50dc7d57a780a7262773923f
7
- data.tar.gz: 5950173a0b12a60272f5db537577f2ccc14c5f01d9b7921f38752d0089582245fcfafada97ca13d5ad95dc73d5ff1753a3f7a83a96218a40b9b95f528a64f417
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.0)
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.1"
4
+ VERSION = "0.2.0"
5
5
  end
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
- require "./to_csv"
8
- require "./longest_line"
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 DF from CSV File
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: :hash, **opt)
24
- 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
+
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 DF from Excel File
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: :hash, **opt)
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
- def to_hash(array2d, line_from: 1, header: 0)
40
- output = array2d[line_from..]
41
- hd = header.nil? ? [*0...(output.longest_line)].map{"column#{_1}"} : array2d[header]
42
- 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)
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
- begin
59
- 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)
60
98
  s = book.sheet(sheet_i)
61
99
 
62
- ## bottole neck===
100
+ ## bottole neck
63
101
  return s.to_a
64
-
65
- rescue Encoding::InvalidByteSequenceError
66
-
67
- Spreadsheet.client_encoding="Windows-31J"
68
- 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
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.1.1
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-17 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.