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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 13
def to_axlsx_package(opts={}, package=nil)
opts = SpreadsheetArchitect::Utils.get_options(opts, self)
options = SpreadsheetArchitect::Utils.get_cell_data(opts, self)
if options[:column_types] && !(options[:column_types].compact.collect(&:to_sym) - SpreadsheetArchitect::XLSX_COLUMN_TYPES).empty?
raise SpreadsheetArchitect::Exceptions::ArgumentError.new("Invalid column type. Valid XLSX values are #{SpreadsheetArchitect::XLSX_COLUMN_TYPES}")
end
= SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:header_style])
row_style = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(options[:row_style])
if package.nil?
package = Axlsx::Package.new
end
row_index = -1
package.workbook.add_worksheet(name: options[:sheet_name]) do |sheet|
max_row_length = options[:data].empty? ? 0 : options[:data].max_by{|x| x.length}.length
if options[:headers]
= package.workbook.styles.add_style()
options[:headers].each do ||
row_index += 1
missing = max_row_length - .count
if missing > 0
missing.times do
.push(nil)
end
end
sheet.add_row , style:
if options[:conditional_row_styles]
conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, )
unless conditional_styles_for_row.empty?
sheet.add_style(
"#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}",
SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
)
end
end
end
end
if options[:data].empty?
break
end
row_style_index = package.workbook.styles.add_style(row_style)
default_date_style_index = nil
default_time_style_index = nil
options[:data].each do |row_data|
row_index += 1
missing = max_row_length - row_data.count
if missing > 0
missing.times do
row_data.push(nil)
end
end
types = []
styles = []
row_data.each_with_index do |x,i|
if (x.respond_to?(:empty) ? x.empty? : x.nil?)
types[i] = nil
styles[i] = row_style_index
else
if options[:column_types]
types[i] = options[:column_types][i]
end
types[i] ||= SpreadsheetArchitect::Utils::XLSX.get_type(x)
if [:date, :time].include?(types[i])
if types[i] == :date
default_date_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd'}))
styles[i] = default_date_style_index
else
default_time_style_index ||= package.workbook.styles.add_style(row_style.merge({format_code: 'yyyy-mm-dd h:mm AM/PM'}))
styles[i] = default_time_style_index
end
else
styles[i] = row_style_index
end
end
end
sheet.add_row row_data, style: styles, types: types
if options[:conditional_row_styles]
options[:conditional_row_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:conditional_row_styles])
conditional_styles_for_row = SpreadsheetArchitect::Utils::XLSX.conditional_styles_for_row(options[:conditional_row_styles], row_index, row_data)
unless conditional_styles_for_row.empty?
sheet.add_style(
"#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES.first}#{row_index+1}:#{SpreadsheetArchitect::Utils::XLSX::COL_NAMES[max_row_length-1]}#{row_index+1}",
SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(conditional_styles_for_row)
)
end
end
end
if options[:column_widths]
sheet.column_widths(*options[:column_widths])
end
if options[:borders] || options[:column_styles] || options[:range_styles] || options[:merges]
num_rows = options[:data].count + (options[:headers] ? options[:headers].count : 0)
end
if options[:borders]
options[:borders] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:borders])
options[:borders].each do |x|
if x[:range].is_a?(Hash)
x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
else
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
end
sheet.add_border x[:range], (x[:border_styles] || x[:styles])
end
end
if options[:column_styles]
options[:column_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:column_styles])
options[:column_styles].each do |x|
start_row = (options[:headers] ? options[:headers].count : 0) + 1
x[:styles] = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])
add_column_style = ->(col){
SpreadsheetArchitect::Utils::XLSX.verify_column(col, max_row_length)
range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (start_row..num_rows), columns: col}, max_row_length, num_rows)
sheet.add_style range_str, x[:styles]
if x[:include_header] && start_row > 1
range_str = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str({rows: (1..start_row-1), columns: col}, max_row_length, num_rows)
sheet.add_style(range_str, x[:styles])
end
}
case x[:columns]
when Array, Range
x[:columns].each do |col|
add_column_style.call(col)
end
when Integer, String
add_column_style.call(x[:columns])
else
SpreadsheetArchitect::Utils::XLSX.verify_column(x[:columns], max_row_length)
end
end
end
if options[:range_styles]
options[:range_styles] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:range_styles])
options[:range_styles].each do |x|
styles = SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles])
if x[:range].is_a?(Hash)
x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
else
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
end
sheet.add_style x[:range], styles
end
end
if options[:merges]
options[:merges] = SpreadsheetArchitect::Utils.hash_array_symbolize_keys(options[:merges])
options[:merges].each do |x|
if x[:range].is_a?(Hash)
x[:range] = SpreadsheetArchitect::Utils::XLSX.range_hash_to_str(x[:range], max_row_length, num_rows)
else
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows)
end
sheet.merge_cells x[:range]
end
end
if options[:freeze_headers]
sheet.sheet_view.pane do |pane|
pane.state = :frozen
pane.y_split = options[:headers].count
end
elsif options[:freeze]
options[:freeze] = SpreadsheetArchitect::Utils.symbolize_keys(options[:freeze])
sheet.sheet_view.pane do |pane|
pane.state = :frozen
if !options[:freeze][:rows]
raise SpreadsheetArchitect::Exceptions::ArgumentError.new("The :rows key must be specified in the :freeze option hash")
elsif options[:freeze][:rows].is_a?(Range)
pane.y_split = options[:freeze][:rows].count
else
pane.y_split = 1
end
if options[:freeze][:columns] && options[:freeze][:columns] != :all
if options[:freeze][:columns].is_a?(Range)
pane.x_split = options[:freeze][:columns].count
else
pane.x_split = 1
end
end
end
end
end
return package
end
|