Module: WorkerTools::XlsxOutput
- Defined in:
- lib/worker_tools/xlsx_output.rb
Instance Method Summary collapse
- #xlsx_output_add_attachment ⇒ Object
- #xlsx_output_column_format ⇒ Object
- #xlsx_output_column_headers ⇒ Object
- #xlsx_output_content ⇒ Object
- #xlsx_output_entries ⇒ Object
- #xlsx_output_file_name ⇒ Object
- #xlsx_output_insert_headers(spreadsheet, headers) ⇒ Object
- #xlsx_output_insert_rows(spreadsheet, rows, headers) ⇒ Object
- #xlsx_output_iterators(iterable, compare_hash = nil) ⇒ Object
- #xlsx_output_row_values(entry) ⇒ Object
- #xlsx_output_style_columns(spreadsheet, styles, headers) ⇒ Object
- #xlsx_output_tmp_file ⇒ Object
- #xlsx_output_write_file ⇒ Object
- #xlsx_output_write_sheet(workbook, sheet_content, index) ⇒ Object
Instance Method Details
#xlsx_output_add_attachment ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/worker_tools/xlsx_output.rb', line 103 def model.( xlsx_output_tmp_file, file_name: xlsx_output_file_name, content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) end |
#xlsx_output_column_format ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/worker_tools/xlsx_output.rb', line 37 def xlsx_output_column_format # These columns are used to set the headers, also # to set the row values depending on your implementation. # # To ignore them set it to _false_ # # Ex: # @xlsx_output_column_format ||= { # foo: { width: 10, text_wrap: true }, # bar: { width: 20, text_wrap: false } # } {} end |
#xlsx_output_column_headers ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/worker_tools/xlsx_output.rb', line 8 def xlsx_output_column_headers # These columns are used to set the headers, also # to set the row values depending on your implementation. # # To ignore them set it to _false_ # # Ex: # @xlsx_output_column_headers ||= { # foo: 'Foo Header', # bar: 'Bar Header' # } raise "xlsx_output_column_headers has to be defined in #{self}" end |
#xlsx_output_content ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/worker_tools/xlsx_output.rb', line 22 def xlsx_output_content { sheet1: { label: 'Sheet 1', headers: xlsx_output_column_headers, rows: xlsx_output_entries.lazy.map { |entry| xlsx_output_row_values(entry) }, column_style: xlsx_output_column_format } } end |
#xlsx_output_entries ⇒ Object
4 5 6 |
# File 'lib/worker_tools/xlsx_output.rb', line 4 def xlsx_output_entries raise "xlsx_output_entries has to be defined in #{self}" end |
#xlsx_output_file_name ⇒ Object
99 100 101 |
# File 'lib/worker_tools/xlsx_output.rb', line 99 def xlsx_output_file_name "#{model_kind}.xlsx" end |
#xlsx_output_insert_headers(spreadsheet, headers) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/worker_tools/xlsx_output.rb', line 51 def xlsx_output_insert_headers(spreadsheet, headers) return unless headers iterator = if headers.is_a? Hash headers.values else headers end iterator.each_with_index do |header, index| spreadsheet.add_cell(0, index, header.to_s) end end |
#xlsx_output_insert_rows(spreadsheet, rows, headers) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/worker_tools/xlsx_output.rb', line 65 def xlsx_output_insert_rows(spreadsheet, rows, headers) rows.each_with_index do |row, row_index| xlsx_output_iterators(row, headers).each_with_index do |value, col_index| spreadsheet.add_cell(row_index + 1, col_index, value.to_s) end end end |
#xlsx_output_iterators(iterable, compare_hash = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/worker_tools/xlsx_output.rb', line 73 def xlsx_output_iterators(iterable, compare_hash = nil) if iterable.is_a? Hash raise 'parameter compare_hash should be a hash, too.' if compare_hash.nil? || !compare_hash.is_a?(Hash) iterable.values_at(*compare_hash.keys) else iterable end end |
#xlsx_output_row_values(entry) ⇒ Object
33 34 35 |
# File 'lib/worker_tools/xlsx_output.rb', line 33 def xlsx_output_row_values(entry) entry.values_at(*xlsx_output_column_headers.keys) end |
#xlsx_output_style_columns(spreadsheet, styles, headers) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/worker_tools/xlsx_output.rb', line 83 def xlsx_output_style_columns(spreadsheet, styles, headers) return false unless headers xlsx_output_iterators(styles, headers).each_with_index do |format, index| next unless format spreadsheet.change_column_width(index, format[:width]) spreadsheet.change_text_wrap(index, format[:text_wrap]) end true end |
#xlsx_output_tmp_file ⇒ Object
95 96 97 |
# File 'lib/worker_tools/xlsx_output.rb', line 95 def xlsx_output_tmp_file @xlsx_output_tmp_file ||= Tempfile.new(['output', '.xlsx']) end |
#xlsx_output_write_file ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/worker_tools/xlsx_output.rb', line 121 def xlsx_output_write_file book = RubyXL::Workbook.new xlsx_output_content.each_with_index do |(_, object), index| xlsx_output_write_sheet(book, object, index) end book.write xlsx_output_tmp_file end |
#xlsx_output_write_sheet(workbook, sheet_content, index) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/worker_tools/xlsx_output.rb', line 111 def xlsx_output_write_sheet(workbook, sheet_content, index) sheet = workbook.worksheets[index] sheet = workbook.add_worksheet(sheet_content[:label]) if sheet.nil? sheet.sheet_name = sheet_content[:label] xlsx_output_style_columns(sheet, sheet_content[:column_style], sheet_content[:headers]) xlsx_output_insert_headers(sheet, sheet_content[:headers]) xlsx_output_insert_rows(sheet, sheet_content[:rows], sheet_content[:headers]) end |