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
|
# File 'lib/spreadsheet_architect/class_methods/xlsx.rb', line 12
def to_axlsx_package(opts={}, package=nil)
opts = SpreadsheetArchitect::Utils.get_cell_data(opts, self)
options = SpreadsheetArchitect::Utils.get_options(opts, self)
= 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
return package if !options[:headers] && options[:data].empty?
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 ||
missing = max_row_length - .count
if missing > 0
missing.times do
.push(nil)
end
end
sheet.add_row , style:
end
end
if options[:data].empty?
break
end
row_style_index = package.workbook.styles.add_style(row_style)
options[:data].each do |row_data|
missing = max_row_length - row_data.count
if missing > 0
missing.times do
row_data.push(nil)
end
end
types = []
row_data.each_with_index do |x,i|
if (x.respond_to?(:empty) ? x.empty? : x.nil?)
types[i] = nil
else
if options[:column_types]
types[i] = options[:column_types][i]
end
types[i] ||= SpreadsheetArchitect::Utils::XLSX.get_type(x)
end
end
sheet.add_row row_data, style: row_style_index, types: types
end
options[:data].first.each_with_index do |x,i|
types = []
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
format_code = 'm/d/yyyy'
else
format_code = 'yyyy/m/d h:mm AM/PM'
end
sheet.col_style(i, package.workbook.styles.add_style(format_code: format_code), row_offset: (options[:headers] ? options[:headers].count : 0))
end
end
if options[:column_widths]
sheet.column_widths(*options[:column_widths])
end
if options[:borders] || options[:column_styles] || options[:range_styles] || options[:merges]
col_names = max_row_length > 675 ? Array('A'..'ZZZ') : Array('A'..'ZZ')
num_rows = options[:data].count
end
if 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, col_names)
end
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows, col_names)
sheet.add_border x[:range], x[:border_styles]
end
end
if options[:column_styles]
options[:column_styles].each do |x|
start_row = !x[:include_header] && options[:headers] ? options[:headers].count : 0
package.workbook.styles do |s|
style = s.add_style row_style.merge(SpreadsheetArchitect::Utils::XLSX.convert_styles_to_axlsx(x[:styles]))
if x[:columns].is_a?(Array) || x[:columns].is_a?(Range)
x[:columns].each do |col|
if col.is_a?(String)
col = col_names.index(col)
end
sheet.col_style(col, style, row_offset: start_row)
end
elsif x[:columns].is_a?(Integer)
sheet.col_style(x[:columns], style, row_offset: start_row)
end
end
end
end
if 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, col_names)
end
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows, col_names)
sheet.add_style x[:range], styles
end
end
if 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, col_names)
end
SpreadsheetArchitect::Utils::XLSX.verify_range(x[:range], num_rows, col_names)
sheet.merge_cells x[:range]
end
end
end
return package
end
|