forked from wvulibraries/aspace_reporting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
full_db.rb
72 lines (55 loc) · 1.81 KB
/
full_db.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
64
65
66
67
68
69
70
#!/usr/bin/env ruby
# gems
require 'mysql2'
require 'rubyXL'
# require lib folder
Dir['./lib/*.rb'].each {|file| require file }
# set the root as a constant global
root = File.dirname(__FILE__)
# grabs the sql file you want to get form the database to put into the excel file
tables_file = "#{root}/sql/table_names.txt"
tables_list = File.open(tables_file, 'r') { |f| f.read }
tables_list = tables_list.split(',')
tables_list.map! { |table| table.strip! }
# connect to db
@db = Database.new
# create a base report to work in
report = RubyXL::Workbook.new
report.write "#{root}/exports/all_data.xlsx"
page_number = 0
total_pages = tables_list.size
tables_list.each do |table|
# create worksheets
if page_number.zero?
worksheet = report[0]
worksheet.sheet_name = table
else
worksheet = report.add_worksheet(table)
end
# sql calls
sql = "SELECT * FROM `#{table}`;"
sql_data = @db.query(sql).to_a
next if sql_data.empty?
# write the data to the worksheet
sql_data.each_with_index do |row_data, row_number|
worksheet.change_row_height(row_number, 30)
worksheet.change_row_fill(row_number, 'eeeeee') if row_number.even?
row_data.values.each_with_index do |cell_data, cell_number|
worksheet.insert_cell(row_number, cell_number, cell_data)
end
end
# creat the headers
worksheet.insert_row(0)
worksheet.change_row_height(0, 30)
# headers
headers = sql_data.first.keys
headers.each_with_index do |value, col_number|
worksheet.insert_cell(0, col_number, value).change_font_color('ffffff')
worksheet.change_row_fill(0, '333333')
end
# set the number of pages at the end
page_number += 1
puts "Created the #{table} section and page count is now #{page_number}/#{total_pages}."
end
report.write "#{root}/exports/all_data.xlsx"
puts "Everything worked congrats!"