excel2csv 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,12 +34,12 @@ Excel2CSV.foreach("path/to/file.xls") {|r| puts r}
34
34
  Excel2CSV.foreach("path/to/file.xlsx") {|r| puts r}
35
35
 
36
36
  # Read non first worksheet
37
- Excel2CSV.read "path/to/file.xls", index:1 #reads second sheet
38
- Excel2CSV.read "path/to/file.xlsx", index:2 #reads third sheet
37
+ Excel2CSV.read "path/to/file.xls", sheet:1 #reads second sheet
38
+ Excel2CSV.read "path/to/file.xlsx", sheet:2 #reads third sheet
39
39
 
40
40
 
41
41
  # Preview first N rows in big files
42
- Excel2CSV.read "path/to/file.xls", rows_limit: 2, preview: true
43
- Excel2CSV.read "path/to/file.xlsx", rows_limit: 2, preview: true
44
- Excel2CSV.read "path/to/file.csv", rows_limit: 2, preview: true
42
+ Excel2CSV.read "path/to/file.xls", rows: 2, preview: true
43
+ Excel2CSV.read "path/to/file.xlsx", rows: 2, preview: true
44
+ Excel2CSV.read "path/to/file.csv", rows: 2, preview: true
45
45
  ```
@@ -0,0 +1,42 @@
1
+ require "fileutils"
2
+
3
+ module Excel2CSV
4
+ class Info
5
+ attr_accessor :sheets
6
+ attr_accessor :previews
7
+ attr_accessor :working_folder
8
+
9
+ def self.read working_folder
10
+ info = Info.new working_folder
11
+ info.read
12
+ info
13
+ end
14
+
15
+ def read
16
+ Dir["#{@working_folder}/*.csv"].map do |file|
17
+ name = File.basename(file)
18
+ m = /(?<sheet>\d+)-(?<rows>\d+)(-of-(?<total_rows>\d+))?/.match(name)
19
+ next if !m
20
+ total_rows = (m[:total_rows] || m[:rows]).to_i
21
+ preview_rows = m[:rows].to_i
22
+ if name =~ /preview/
23
+ @previews << {path: file, total_rows:total_rows, rows:preview_rows}
24
+ else
25
+ @sheets << {path: file, total_rows:total_rows, rows:total_rows}
26
+ end
27
+ end
28
+ end
29
+
30
+ def clean
31
+ FileUtils.remove_entry_secure(@working_folder, true) if @working_folder
32
+ end
33
+
34
+ private
35
+
36
+ def initialize working_folder
37
+ @working_folder = working_folder
38
+ @sheets = []
39
+ @previews = []
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module Excel2CSV
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/excel2csv.rb CHANGED
@@ -1,51 +1,10 @@
1
1
  require "excel2csv/version"
2
+ require "excel2csv/info"
3
+
2
4
  require "csv"
3
5
  require "tmpdir"
4
- require "fileutils"
5
6
 
6
7
  module Excel2CSV
7
-
8
- class Info
9
- attr_accessor :sheets
10
- attr_accessor :previews
11
- attr_accessor :tmp_dir
12
- attr_accessor :working_dir
13
-
14
- def self.read dir, tmp_dir
15
- info = Info.new dir, tmp_dir
16
- info.read
17
- info
18
- end
19
-
20
- def read
21
- Dir["#{@working_dir}/*.csv"].map do |file|
22
- name = File.basename(file)
23
- m = /(?<sheet>\d+)-(?<rows>\d+)(-of-(?<total_rows>\d+))?/.match(name)
24
- next if !m
25
- total_rows = (m[:total_rows] || m[:rows]).to_i
26
- preview_rows = m[:rows].to_i
27
- if name =~ /preview/
28
- @previews << {path: file, total_rows:total_rows, rows:preview_rows}
29
- else
30
- @sheets << {path: file, total_rows:total_rows, rows:total_rows}
31
- end
32
- end
33
- end
34
-
35
- def clean
36
- FileUtils.remove_entry_secure(@tmp_dir, true) if @tmp_dir
37
- end
38
-
39
- private
40
-
41
- def initialize working_dir, tmp_dir
42
- @working_dir = working_dir
43
- @tmp_dir = tmp_dir
44
- @sheets = []
45
- @previews = []
46
- end
47
-
48
- end
49
8
 
50
9
  def foreach(path, options = {}, &block)
51
10
  convert(path, options) do |info|
@@ -65,7 +24,7 @@ module Excel2CSV
65
24
 
66
25
  def convert(path, options = {})
67
26
  info = options[:info]
68
- if info && Dir.exists?(info.working_dir)
27
+ if info && Dir.exists?(info.working_folder)
69
28
  return block_given? ? yield(info) : info
70
29
  end
71
30
  begin
@@ -89,7 +48,7 @@ module Excel2CSV
89
48
  else
90
49
  collection = info.sheets
91
50
  end
92
- index = (idx = options[:index]) ? idx : 0
51
+ index = (idx = options[:sheet]) ? idx : 0
93
52
  collection[index][:path]
94
53
  end
95
54
 
@@ -97,8 +56,8 @@ module Excel2CSV
97
56
 
98
57
  def create_cvs_files(path, options)
99
58
  tmp_dir = Dir.mktmpdir
100
- dest_folder = options[:dest_folder] || tmp_dir
101
- limit = options[:rows_limit]
59
+ working_folder = options[:working_folder] || tmp_dir
60
+ limit = options[:rows]
102
61
  if path =~ /\.csv$/
103
62
  total_rows = 0
104
63
  preview_rows = []
@@ -106,7 +65,7 @@ module Excel2CSV
106
65
 
107
66
  # Transcode file to utf-8, count total and gen preview
108
67
 
109
- CSV.open("#{dest_folder}/1-#{total_rows}.csv", "wb") do |csv|
68
+ CSV.open("#{working_folder}/1-#{total_rows}.csv", "wb") do |csv|
110
69
  CSV.foreach(path, opts) do |row|
111
70
  if limit && total_rows <= limit
112
71
  preview_rows << row
@@ -117,7 +76,7 @@ module Excel2CSV
117
76
  end
118
77
 
119
78
  if limit
120
- CSV.open("#{dest_folder}/1-#{limit}-of-#{total_rows}-preview.csv", "wb") do |csv|
79
+ CSV.open("#{working_folder}/1-#{limit}-of-#{total_rows}-preview.csv", "wb") do |csv|
121
80
  preview_rows.each {|row| csv << row}
122
81
  end
123
82
  end
@@ -125,17 +84,17 @@ module Excel2CSV
125
84
  java_options = options[:java_options] || "-Dfile.encoding=utf8 -Xms512m -Xmx512m -XX:MaxPermSize=256m"
126
85
  rows_limit = limit ? "-r #{limit}" : ""
127
86
  jar_path = File.join(File.dirname(__FILE__), "excel2csv.jar")
128
- `java #{java_options} -jar #{jar_path} #{rows_limit} #{path} #{dest_folder}`
87
+ `java #{java_options} -jar #{jar_path} #{rows_limit} #{path} #{working_folder}`
129
88
  end
130
89
 
131
- Info.read(dest_folder, tmp_dir)
90
+ Info.read(working_folder)
132
91
  end
133
92
 
134
93
  module_function :create_cvs_files
135
94
 
136
95
  def clean_options options
137
96
  options.dup.delete_if do |key, value|
138
- [:dest_folder, :java_options, :preview, :index, :path, :rows_limit, :info].include?(key)
97
+ [:working_folder, :java_options, :preview, :sheet, :path, :rows, :info].include?(key)
139
98
  end
140
99
  end
141
100
 
@@ -35,31 +35,31 @@ describe Excel2CSV do
35
35
  count.should == 3
36
36
  end
37
37
 
38
- it "removes tmp dir after work" do
39
- tmp_dir = nil
38
+ it "removes tmp folder after work" do
39
+ working_folder = nil
40
40
  excel.convert xlsx_basic_types do |info|
41
41
  # puts IO.read(info.sheets.first[:path])
42
- tmp_dir = info.tmp_dir
42
+ working_folder = info.working_folder
43
43
  end
44
- tmp_dir.should_not be_nil
45
- Dir.exists?(tmp_dir).should == false
44
+ working_folder.should_not be_nil
45
+ Dir.exists?(working_folder).should == false
46
46
  end
47
47
 
48
48
  it "converts once if info is passed" do
49
49
  info = excel.convert xlsx_basic_types
50
50
  info.sheets.length.should == 1
51
51
  info.previews.length.should == 0
52
- info.should == excel.convert("spec/fixtures/basic_types.xlsx", info:info)
52
+ info.should == excel.convert(xlsx_basic_types, info:info)
53
53
  end
54
54
 
55
- it "regenerate csv files if working_dir is removed" do
55
+ it "regenerate csv files if working_folder is removed" do
56
56
  info = excel.convert xlsx_basic_types
57
57
  info.clean
58
58
  info.should_not == excel.convert(xlsx_basic_types, info:info)
59
59
  end
60
60
 
61
61
  it "generates preview csv files with rows limit" do
62
- info = excel.convert xls_basic_types, rows_limit:1
62
+ info = excel.convert xls_basic_types, rows:1
63
63
  info.sheets.length.should == 1
64
64
  info.previews.length.should == 1
65
65
 
@@ -71,7 +71,7 @@ describe Excel2CSV do
71
71
  end
72
72
 
73
73
  it "reads previews" do
74
- data = excel.read(xls_basic_types, rows_limit:1, preview:true, index:0)
74
+ data = excel.read(xls_basic_types, rows:1, preview:true, sheet:0)
75
75
  data.length.should == 1
76
76
  data[0].should == ["1.00", date_24, "Hello"]
77
77
  end
@@ -85,9 +85,9 @@ describe Excel2CSV do
85
85
 
86
86
  it "reads csv files with preview" do
87
87
  data = excel.read(csv_basic_types,
88
- encoding: 'windows-1251:utf-8',
89
- rows_limit: 2,
90
- preview: true
88
+ encoding: 'windows-1251:utf-8',
89
+ rows: 2,
90
+ preview: true
91
91
  )
92
92
  data[0].should == ["1.00","12/24/11 12:00 AM","Hello"]
93
93
  data[1].should == ["2.00","12/25/11 12:00 AM","Привет"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excel2csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70102599300280 !ruby/object:Gem::Requirement
16
+ requirement: &70115224571500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - <=
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70102599300280
24
+ version_requirements: *70115224571500
25
25
  description: gem for converting Excel files to csv
26
26
  email:
27
27
  - yury.korolev@gmail.com
@@ -36,6 +36,7 @@ files:
36
36
  - excel2csv.gemspec
37
37
  - lib/excel2csv.jar
38
38
  - lib/excel2csv.rb
39
+ - lib/excel2csv/info.rb
39
40
  - lib/excel2csv/version.rb
40
41
  - spec/excel2csv_spec.rb
41
42
  - spec/fixtures/basic_types.csv