forked from hirenj/mascp-jstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetojson.rb
executable file
·64 lines (49 loc) · 1.32 KB
/
filetojson.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/ruby
require 'rubygems'
require 'getoptlong'
require 'fastercsv'
require 'spreadsheet'
require 'cgi'
require 'base64'
require 'tempfile'
require 'roo'
require 'json'
ENV['ROO_TMP'] = Dir.tmpdir()
cgi = CGI.new
params = cgi.params
infile = nil
data_file = Tempfile.new(['converter','.xlsx'])
filename = ''
puts cgi.header('text/plain')
data = []
if params && params.has_key?("file")
file = params["file"].first
filename = file.original_filename
data_file << file.read
end
if ENV['HTTP_UP_FILENAME']
filename = ENV['HTTP_UP_FILENAME']
data_file << ((ENV['HTTP_NO_BASE64'] == "true") ? params.keys[0] : Base64.decode64(params.keys[0].gsub(/\s/, '+')+'='))
end
data_file.close()
if filename =~ /csv$/
data = FasterCSV.read(data_file.path)
elsif filename =~ /xls$/
workbook = Spreadsheet.open(data_file.path)
a_sheet = workbook.worksheet(0)
(a_sheet.dimensions[0]..(a_sheet.dimensions[1] - 1)).each { |row_num|
data.push(a_sheet.row(row_num))
}
elsif filename =~ /xlsx$/
workbook = Excelx.new(data_file.path)
workbook.default_sheet = workbook.sheets.first
(workbook.first_row..workbook.last_row).each { |row_num|
data.push(workbook.row(row_num))
}
elsif filename =~ /txt$/
data_file.open().each { |line|
data.push(line.split(/[,\t\s]/))
}
end
puts data.to_json
data_file.unlink()