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
|
# File 'lib/tallty_import_export/export_form.rb', line 16
def export_workbook workbook, association_records, **options
alignment = { vertical: :center, horizontal: :center }
border = { color: '969696', style: :thin }
title1 = workbook.styles.add_style(alignment: alignment, border: border, sz: 12, b: true)
title2 = workbook.styles.add_style(alignment: alignment, border: border, bg_color: "2a5caa", sz: 12, fg_color: "fffffb")
title3 = workbook.styles.add_style(alignment: alignment.merge(wrap_text: true), border: border, sz: 10)
_sheet_name = (respond_to?(:sheet_name) ? self.sheet_name : nil) || options[:sheet_name]
= **options
workbook.add_worksheet(name: _sheet_name) do |sheet|
index = 0
if respond_to?(:first_header)
row_index = Axlsx.col_ref(.size - 1)
sheet.merge_cells("A1:#{row_index}1")
sheet.add_row [], style: title1, height: 30
index += 1
end
..each do ||
sheet.add_row(.map(&:name), style: title2, height: 25)
index += 1
end
..values.each do |axios_ary|
if axios_ary.count > 1
top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
sheet.merge_cells(
Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
)
end
end
return if options[:header_only]
value_seq_to_axios = {}
formats = []
association_records.each do |association_record|
records = @each_method.present? ?
(try_method(association_record, @each_method) || [nil]) :
[association_record]
records.each do |record|
value_living_alone_col_index_to_value_count = {}
payload = TalltyImportExport::ExportPayload.new(record, header: ) do |payload, , **opts|
_data = [:source] ?
handle_data(association_record, , index, **opts) :
handle_data(payload, , index, **opts)
end
payload.lines.each_with_index do |line, row_index|
row = []
line.each_with_index do |value, col_index|
value_living_alone_col_index_to_value_count[col_index] ||= 0
if (TalltyImportExport::ExportPayload::Value === value)
row << value.value
value_living_alone_col_index_to_value_count[col_index] += 1
unless value_seq_to_axios[value.seq]
value_seq_to_axios[value.seq] = []
end
value_seq_to_axios[value.seq] << [col_index , row_index + index]
else
row << nil
end
formats.push(
.flatten_value[col_index].format&.to_sym || (row.last.is_a?(String) ? :string : nil)
)
end
sheet.add_row(row, style: title3, height: @row_height, types: formats)
formats = []
end
value_living_alone_col_index_to_value_count.each_pair do |col_index, count|
if count == 1
sheet.merge_cells(
Axlsx::cell_r(col_index, index) + ':' + Axlsx::cell_r(col_index, index + payload.max_matrix_height - 1)
)
end
end
index += payload.max_matrix_height
end
end
value_seq_to_axios.values.each do |axios_ary|
if axios_ary.count > 1
top_right = [axios_ary.map(&:first).min, axios_ary.map(&:last).min]
bottom_left = [axios_ary.map(&:first).max, axios_ary.map(&:last).max]
sheet.merge_cells(
Axlsx::cell_r(top_right.first, top_right.last) + ':' + Axlsx::cell_r(bottom_left.first, bottom_left.last)
)
end
end
end
end
|