ss2json 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +3 -0
- data/lib/ss2json/cli.rb +24 -4
- data/lib/ss2json/options.rb +52 -14
- data/lib/ss2json/version.rb +2 -2
- data/ss2json.gemspec +1 -1
- metadata +3 -3
data/Changelog
CHANGED
@@ -4,6 +4,9 @@
|
|
4
4
|
right now is cleaner and showing the list of available spreadsheets.
|
5
5
|
* row_converter.rb (sanitize_key) : [bug] sanitify_key fail if the key is
|
6
6
|
just one letter. Now it is fixed.
|
7
|
+
* cli.rb (process_vertical) : New mode for processing in vertical was added.
|
8
|
+
* cli.rb (open) : Added support for old excel files (not so much tested).
|
9
|
+
* options.rb (initialize) : Added --version support.
|
7
10
|
|
8
11
|
2012-05-23 Guillermo Alvarez <guillermo@cientifico.net>
|
9
12
|
|
data/lib/ss2json/cli.rb
CHANGED
@@ -24,6 +24,9 @@ class Ss2Json
|
|
24
24
|
# * **:sheet** Name of the sheet to use.
|
25
25
|
# * **:first_row** Where the title of the columns is.
|
26
26
|
# * **:check_column** Output only the results with a value present on the specific field.
|
27
|
+
# * **:orientation** The data orientation (:horizontal/:vertical).
|
28
|
+
# * **:key_column** Column of the keys for vertical mode.
|
29
|
+
# * **:value_column** Column of the values.
|
27
30
|
# * **:action** Could
|
28
31
|
# * *:convert* Do the normal conversion.
|
29
32
|
# * *:list* Will list the sheets.
|
@@ -34,7 +37,13 @@ class Ss2Json
|
|
34
37
|
if options[:action] == :list
|
35
38
|
@doc.sheets.join("\n")
|
36
39
|
else
|
37
|
-
|
40
|
+
if options[:orientation] == :horizontal
|
41
|
+
process_horizontal
|
42
|
+
elsif options[:orientation] == :vertical
|
43
|
+
process_vertical
|
44
|
+
else
|
45
|
+
raise "Orientation #{options[:orientation]} not recognized"
|
46
|
+
end
|
38
47
|
end
|
39
48
|
end
|
40
49
|
|
@@ -51,7 +60,7 @@ class Ss2Json
|
|
51
60
|
@doc.header_line = @options[:first_row] if @options[:first_row]
|
52
61
|
end
|
53
62
|
|
54
|
-
def
|
63
|
+
def process_horizontal
|
55
64
|
@content = []
|
56
65
|
(@options[:first_row]+1).upto(@doc.last_row).each do |row_n|
|
57
66
|
row = @doc.find(row_n)[0]
|
@@ -63,11 +72,22 @@ class Ss2Json
|
|
63
72
|
end
|
64
73
|
end
|
65
74
|
|
75
|
+
def process_vertical
|
76
|
+
hash = {}
|
77
|
+
@options[:first_row].upto(@doc.last_row) do |row|
|
78
|
+
key = @doc.cell(row, @options[:key_column])
|
79
|
+
value = @doc.cell(row, @options[:value_column])
|
80
|
+
hash[key] = value
|
81
|
+
end
|
82
|
+
@content = RowConverter.new(hash, @options[:converter])
|
83
|
+
end
|
84
|
+
|
66
85
|
|
67
86
|
def open
|
68
87
|
kclass = case @options[:file][/\.(.*)$/,1]
|
69
|
-
when /xlsx
|
70
|
-
when /
|
88
|
+
when /xlsx$/i then Excelx
|
89
|
+
when /xls$/i then Excel
|
90
|
+
when /ods$/i then Openoffice
|
71
91
|
else
|
72
92
|
raise "Unknown format"
|
73
93
|
end
|
data/lib/ss2json/options.rb
CHANGED
@@ -10,6 +10,9 @@ class Ss2Json
|
|
10
10
|
:file => nil,
|
11
11
|
:check_column => nil,
|
12
12
|
:action => :convert,
|
13
|
+
:orientation => :horizontal,
|
14
|
+
:key_column => 1,
|
15
|
+
:value_column => 2,
|
13
16
|
:converter => {
|
14
17
|
:show_null => false,
|
15
18
|
:dont_convert => false,
|
@@ -30,21 +33,53 @@ class Ss2Json
|
|
30
33
|
@help = opts
|
31
34
|
|
32
35
|
opts.banner = "Usage: #{File.basename $0} -f FILENAME [options]"
|
33
|
-
|
36
|
+
|
37
|
+
opts.separator "\nMode options:"
|
38
|
+
|
39
|
+
opts.on("-h", "--horizontal", "Normal processing mode (DEFAULT). Can be ommited") do
|
40
|
+
@options[:orientation] = :horizontal
|
41
|
+
end
|
42
|
+
|
43
|
+
opts.on("-v", "--vertical", "Process the spreadhsheet on vertical") do
|
44
|
+
@options[:orientation] = :vertical
|
45
|
+
end
|
46
|
+
|
47
|
+
opts.on("-l", "--list-sheets", "Return the list of sheets") do |file|
|
48
|
+
@options[:action] = :list
|
49
|
+
end
|
50
|
+
|
51
|
+
opts.separator "Common options:"
|
52
|
+
|
53
|
+
opts.on("-f", "--file FILENAME", "Use the filename") do |file|
|
54
|
+
File.file?(file) or die("Can't find the file #{file}.")
|
55
|
+
@options[:file] = File.expand_path(file)
|
56
|
+
end
|
57
|
+
|
34
58
|
opts.on("-s", "--sheet SHEET_NAME", "Use other that the first table") do |sheet|
|
35
59
|
@options[:sheet] = sheet
|
36
60
|
end
|
37
61
|
|
38
|
-
opts.
|
62
|
+
opts.separator "\nHorizontal and Vertical mode options:"
|
63
|
+
|
64
|
+
opts.on("-r", "--first-row ROW_NUMBER", "Set the first row") do |row|
|
39
65
|
die("Can't understand the row #{row}. Use a number") unless row =~ /\A\d*\z/
|
40
|
-
|
66
|
+
@options[:first_row] = row.to_i
|
41
67
|
end
|
42
68
|
|
43
|
-
opts.
|
44
|
-
|
45
|
-
|
69
|
+
opts.separator "\nVertical mode options:"
|
70
|
+
|
71
|
+
opts.on("-k", "--key-column", "Column where the keys are (Default to 1)") do |column|
|
72
|
+
die("Can't understand the column #{column}. Use a number") unless column =~ /\A\d*\z/
|
73
|
+
@options[:key_column] = column.to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
opts.on("-a", "--value-column", "Column where the values are (Default to 2)") do |column|
|
77
|
+
die("Can't understand the column #{column}. Use a number") unless column =~ /\A\d*\z/
|
78
|
+
@options[:value_column] = column.to_i
|
46
79
|
end
|
47
80
|
|
81
|
+
opts.separator "\nData options:"
|
82
|
+
|
48
83
|
opts.on("-i", "--ignore-value VALUE", "Ignore the fields with that value. Could be use several times") do |t|
|
49
84
|
@options[:converter][:ignored_values] << t
|
50
85
|
end
|
@@ -53,10 +88,6 @@ class Ss2Json
|
|
53
88
|
@options[:converter][:show_null] = true
|
54
89
|
end
|
55
90
|
|
56
|
-
opts.on("-c", "--check-column NAME", "Only output objects wich his property NAME is not in IGNORED VALUES") do |t|
|
57
|
-
@options[:check_column] = t
|
58
|
-
end
|
59
|
-
|
60
91
|
opts.on("-d", "--disable-conversion", "Disable the conversion from floats to integers") do
|
61
92
|
@options[:converter][:dont_convert] = true
|
62
93
|
end
|
@@ -65,18 +96,25 @@ class Ss2Json
|
|
65
96
|
@options[:converter][:downcase_first_letter] = false
|
66
97
|
end
|
67
98
|
|
68
|
-
opts.separator ""
|
99
|
+
opts.separator "\nFilter options:"
|
69
100
|
|
70
|
-
opts.on("-
|
71
|
-
@options[:
|
101
|
+
opts.on("-c", "--check-column NAME", "Only output objects wich his property NAME is not in IGNORED VALUES") do |t|
|
102
|
+
@options[:check_column] = t
|
72
103
|
end
|
73
104
|
|
74
|
-
|
105
|
+
|
106
|
+
opts.separator "\n"
|
75
107
|
|
76
108
|
opts.on_tail("-h","--help", "Show this help") do
|
77
109
|
puts opts
|
78
110
|
exit 0
|
79
111
|
end
|
112
|
+
|
113
|
+
opts.on_tail("--version", "Show the version") do
|
114
|
+
puts "#{File.basename($0)} Version: #{Ss2Json::VERSION}"
|
115
|
+
exit 0
|
116
|
+
end
|
117
|
+
|
80
118
|
end.parse!
|
81
119
|
|
82
120
|
die("You need to at least specify a file") if @options[:file].nil?
|
data/lib/ss2json/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
class
|
2
|
-
VERSION = "0.0
|
1
|
+
class Ss2Json
|
2
|
+
VERSION = "0.1.0"
|
3
3
|
end
|
data/ss2json.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ss2json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.3
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Guillermo A\xCC\x81lvarez"
|