xlstocsv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/bin/xlstocsv +86 -0
  4. metadata +74 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54d69aaeadc33c85f5f30a67dc07863ace92900a
4
+ data.tar.gz: 233329de489d5652f348bb70881aee7cd16720de
5
+ SHA512:
6
+ metadata.gz: 628b16985aead048989e71fca0eb1ae7400908b7bad2d98ba69da09631e43eb56dae5ef21b83b71f8e28bdc1c28c8b347ca9731c75d2cd8554db17ac8636c92f
7
+ data.tar.gz: c8c5a76d8b098cdab50ed7783309c26a164e360c2ba85726f94af24475a16a8f70f3c0fba89e9c83ff590eafa0e6e60f1076eb3d2c553b686e5f24da51526564
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Barzahlen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/bin/xlstocsv ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv'
4
+ require 'optparse'
5
+ require 'spreadsheet'
6
+
7
+ options = {
8
+ col_sep: ",",
9
+ encoding: "UTF-8",
10
+ quote_char: "",
11
+ remove_empty_rows: false,
12
+ }
13
+ optionparser = OptionParser.new do |opts|
14
+ opts.banner = "Usage: ruby #{File.basename(__FILE__)} -f <inputfile> [options]\n" +
15
+ "Reads an XLS file and converts every worksheet to a csv file\n\n"
16
+
17
+ opts.on('-f filename', "Path to the XLS file") do |file|
18
+ options[:filename] = file
19
+ end
20
+
21
+ opts.on('-c col_sep', "Column separator. Defaults to ','") do |col_sep|
22
+ options[:col_sep] = col_sep
23
+ end
24
+
25
+ opts.on('-e encoding', "Output file encoding. Defaults to UTF-8") do |encoding|
26
+ options[:encoding] = encoding
27
+ end
28
+
29
+ opts.on('-q quote_char', "Quote char. Defaults to an empty string") do |quote_char|
30
+ options[:quote_char] = quote_char
31
+ end
32
+
33
+ opts.on('-R', '--remove-empty-rows', 'Removes empty rows') do |remove|
34
+ options[:remove_empty_rows] = remove
35
+ end
36
+
37
+ opts.on('-N count', '--remove-first-n-rows', 'Removes the first N rows. Empty rows are included here') do |count|
38
+ options[:remove_first_n_rows] = count.to_i
39
+ end
40
+
41
+ opts.on('-W sheet_index1,sheet_index2', '--only-specific-worksheets',
42
+ 'Only transforms the worksheets with the specific indexes starting at 0') do |worksheets|
43
+ options[:only_specific_worksheets] = worksheets.split(',').map{ |val| val.to_i }
44
+ end
45
+
46
+ opts.on_tail("-h", "--help", "Show this message") do
47
+ puts opts
48
+ exit(false)
49
+ end
50
+ end
51
+ optionparser.parse!
52
+
53
+ if options[:filename].nil?
54
+ puts optionparser
55
+ exit
56
+ end
57
+
58
+ csv_params = {
59
+ col_sep: options[:col_sep],
60
+ encoding: options[:encoding],
61
+ quote_char: options[:quote_char]
62
+ }
63
+
64
+ csv_params.delete(:quote_char) if csv_params[:quote_char].to_s.length < 1
65
+
66
+ report = Spreadsheet.open(options[:filename])
67
+
68
+ report.worksheets.each_with_index do |sheet, sheet_index|
69
+ next if !options[:only_specific_worksheets].nil? && !options[:only_specific_worksheets].include?(sheet_index)
70
+
71
+ filename = File.basename(options[:filename], ".*")
72
+ filepath = File.join( File.dirname(options[:filename]), "#{filename}_#{sheet_index}.csv" )
73
+
74
+ csv_data = CSV.generate(csv_params) do |csv|
75
+ sheet.rows.each_with_index do |row, row_index|
76
+ next if !options[:remove_first_n_rows].nil? && row_index < options[:remove_first_n_rows]
77
+ next if row.to_a.empty? && options[:remove_empty_rows]
78
+
79
+
80
+ csv << row.to_a
81
+ end
82
+ end
83
+
84
+ puts "Writing #{filepath}"
85
+ File.open(filepath, "w:#{options[:encoding]}") { |f| f.write(csv_data) }
86
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xlstocsv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cash Payment Solutions GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spreadsheet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 10.4.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 10.4.1
41
+ description:
42
+ email: tobias.schoknecht@barzahlen.de
43
+ executables:
44
+ - xlstocsv
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE
49
+ - bin/xlstocsv
50
+ homepage: https://github.com/Barzahlen/xlstocsv
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.4.8
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Convert XLS files to CSV files
74
+ test_files: []