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
|
# File 'app/importers/concerns/exportable.rb', line 58
def export(data_rows)
xls = Axlsx::Package.new
xls.use_shared_strings = true
workbook = xls.workbook
sheet = workbook.add_worksheet(name: friendly_name&.pluralize || model.name.demodulize.pluralize)
workbook.styles do |style|
introduction_style = style.add_style(bg_color: "E2EEDA")
= style.add_style(b: true, bg_color: "A8D08E", border: {style: :thin, color: "000000"})
= style.add_style(b: true, bg_color: "A8D08E", fg_color: "C00100", border: {style: :thin, color: "000000"})
introduction.each_with_index do |intro, i|
text = intro.is_a?(Symbol) ? I18n.t(intro, scope: [:importers, self.class.name.underscore.to_s, :introduction]) : intro
sheet.add_row [text], style: [introduction_style] * export_columns.count
sheet.merge_cells "A#{i + 1}:#{nr_to_col(export_columns.count - 1)}#{i + 1}"
end
sheet.add_row export_columns.values.map(&:name), style: export_columns.map { |_, c| c.options[:required] ? : }
export_columns.each.with_index do |f, i|
field = f.last
sheet. ref: "#{nr_to_col(i)}#{introduction.count + 1}", author: "", text: field.hint, visible: false if field.hint.present?
end
styles = export_columns.map do |_, c|
if c.options.dig(:export, :format) == "number" || (c.options.dig(:export, :format).nil? && c.options.dig(:export, :example).is_a?(Numeric))
number = workbook.styles.add_style format_code: "#"
elsif c.options.dig(:export, :format) == "text" || (c.options.dig(:export, :format).nil? && c.options.dig(:export, :example).is_a?(String))
text = workbook.styles.add_style format_code: "@"
elsif c.options.dig(:export, :format)
workbook.styles.add_style format_code: c.options.dig(:export, :format).to_s
else
workbook.styles.add_style format_code: "General"
end
end
data_rows.each do |data|
sheet.add_row data, style: styles
end
end
sheet.column_info[0].width = 10
sheet = workbook.add_worksheet(name: I18n.t("importo.sheet.explanation.name"))
workbook.styles do |style|
introduction_style = style.add_style(bg_color: "E2EEDA")
= style.add_style(b: true, bg_color: "A8D08E", border: {style: :thin, color: "000000"})
column_style = style.add_style(b: true)
required_style = style.add_style(b: true, fg_color: "C00100")
wrap_style = workbook.styles.add_style alignment: {wrap_text: true}
introduction.each_with_index do |intro, i|
text = intro.is_a?(Symbol) ? I18n.t(intro, scope: [:importers, self.class.name.underscore.to_s, :introduction]) : intro
sheet.add_row [text], style: [introduction_style] * 2
sheet.merge_cells "A#{i + 1}:B#{i + 1}"
end
sheet.add_row [I18n.t("importo.sheet.explanation.column"), I18n.t("importo.sheet.explanation.value"), I18n.t("importo.sheet.explanation.explanation"), I18n.t("importo.sheet.explanation.example")], style: [] * 4
export_columns.each do |_, c|
styles = [c.options[:required] ? required_style : column_style, wrap_style]
sheet.add_row [c.name, c.value, c.explanation, c.example], style: styles
end
end
sheet.column_info[0].width = 40
sheet.column_info[1].width = 150
xls.to_stream
end
|