forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_sheet.rb
175 lines (151 loc) · 5.06 KB
/
excel_sheet.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
class ExcelSheet
def initialize(title:, header:, records:, locale:, timezone: nil)
require 'writeexcel' # Only load this gem when it is really used.
@title = title
@header = header
@records = records
@timezone = timezone.presence || Setting.get('timezone_default')
@locale = locale || Locale.default
@tempfile = Tempfile.new('excel-export.xls')
@workbook = WriteExcel.new(@tempfile)
@worksheet = @workbook.add_worksheet
@contents = nil
@current_row = 0
@current_column = 0
@lookup_cache = {}
@format_time = @workbook.add_format(num_format: 'yyyy-mm-dd hh:mm:ss')
@format_date = @workbook.add_format(num_format: 'yyyy-mm-dd')
@format_headline = @workbook.add_format
@format_headline.set_bold
@format_headline.set_size(14)
@format_headline.set_color('black')
@format_header = @workbook.add_format
@format_header.set_italic
@format_header.set_bg_color('gray')
@format_header.set_color('white')
@format_footer = @workbook.add_format
@format_footer.set_italic
@format_footer.set_color('gray')
@format_footer.set_size(8)
end
def contents
file = File.new(@tempfile, 'r')
contents = file.read
file.close
contents
end
def content
gen_header
gen_rows
gen_footer
contents
end
def gen_header
@worksheet.write_string(@current_row, @current_column, @title, @format_headline)
@worksheet.set_row(0, 18)
@current_row += 2
@current_column = 0
@header.each do |header|
if header[:width]
@worksheet.set_column(@current_column, @current_column, header[:width])
end
@worksheet.write_string(@current_row, @current_column, header[:display] || header[:name], @format_header)
@current_column += 1
end
end
def gen_rows
@records.each do |record|
gen_row_by_array(record)
end
end
def gen_row_by_array(record)
@current_row += 1
@current_column = 0
record.each do |item|
begin
if item.acts_like?(:time) || item.acts_like?(:date)
value_convert(item, nil, { data_type: 'datetime' })
elsif item.is_a?(Integer) || item.is_a?(Float)
value_convert(item, nil, { data_type: 'integer' })
else
value_convert(item, nil, { data_type: 'string' })
end
rescue => e
Rails.logger.error e
end
@current_column += 1
end
end
def gen_row_by_header(record, additional = {})
@current_row += 1
@current_column = 0
@header.each do |header|
begin
value_convert(record, header[:name], header, additional)
rescue => e
Rails.logger.error e
end
@current_column += 1
end
end
def gen_footer
@current_row += 2
@worksheet.write_string(@current_row, 0, "#{Translation.translate(@locale, 'Timezone')}: #{@timezone}", @format_footer)
@workbook.close
end
def timestamp_in_localtime(time)
return if time.blank?
time.in_time_zone(@timezone).strftime('%F %T') # "2019-08-19 16:21:52"
end
def value_lookup(record, attribute, additional)
value = record[attribute.to_sym]
if attribute[-3, 3] == '_id'
ref = attribute[0, attribute.length - 3]
if record.respond_to?(ref.to_sym)
@lookup_cache[attribute] ||= {}
return @lookup_cache[attribute][value] if @lookup_cache[attribute][value]
ref_object = record.send(ref.to_sym)
ref_name = value
if ref_object.respond_to?(:fullname)
ref_name = ref_object.fullname
elsif ref_object.respond_to?(:name)
ref_name = ref_object.name
end
@lookup_cache[attribute][value] = ref_name
return ref_name
end
end
value = record.try(attribute)
# if no value exists, check additional values
if !value && additional && additional[attribute.to_sym]
value = additional[attribute.to_sym]
end
if value.is_a?(Array)
value = value.join(',')
end
value
end
def value_convert(record, attribute, object, additional = {})
value = if attribute
value_lookup(record, attribute, additional)
else
record
end
case object[:data_type]
when 'boolean', 'select'
if object[:data_option] && object[:data_option]['options'] && object[:data_option]['options'][value]
value = object[:data_option]['options'][value]
end
@worksheet.write_string(@current_row, @current_column, value) if value.present?
when 'datetime'
@worksheet.write_date_time(@current_row, @current_column, timestamp_in_localtime(value), @format_time) if value.present?
when 'date'
@worksheet.write_date_time(@current_row, @current_column, value.to_s, @format_date) if value.present?
when 'integer'
@worksheet.write_number(@current_row, @current_column, value) if value.present?
else
@worksheet.write_string(@current_row, @current_column, value.to_s) if value.present?
end
end
end