-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagrid.rb
More file actions
executable file
·65 lines (53 loc) · 1.32 KB
/
datagrid.rb
File metadata and controls
executable file
·65 lines (53 loc) · 1.32 KB
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
64
65
#!/usr/bin/env ruby
require 'sinatra'
require 'csv'
require 'spreadsheet'
require 'pry'
# folders
set :public_folder, File.dirname(__FILE__) + '/public'
set :files_folder, File.dirname(__FILE__) + '/files'
Spreadsheet.client_encoding = 'UTF-8'
get '/' do
'Welcome to the reports'
end
get '/files/:year/:month/:report.:ext' do |year,month,report,ext|
path = File.join( settings.files_folder, year, month, report ) + "." + ext
@report = report
case ext.downcase
when 'html'
@csv = CSV.read( path.sub( /html$/, 'csv' ), :encoding => 'windows-1251:utf-8' )
return erb :grid
when 'xls'
if ! File.exists? path
render_xls path
end
end
send_file path
end
private
def render_xls( path )
csv = CSV.read( path.sub( /xls$/, 'csv' ), :encoding => 'windows-1251:utf-8' )
save_path = path
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
sheet.name = @report
#write header
sheet.row(0).concat csv.shift
#write data
y=1
csv.each do |row|
x=0
row.each do |cell|
sheet[y,x] = cell
x+=1
end
y+=1
end
#formatting
sheet.row(0).height = 18
format = Spreadsheet::Format.new :color => :blue,
:weight => :bold,
:size => 18
sheet.row(0).default_format = format
book.write save_path
end