Module: WorkerTools::XlsxOutput

Defined in:
lib/worker_tools/xlsx_output.rb

Instance Method Summary collapse

Instance Method Details

#xlsx_ensure_output_target_folderObject



58
59
60
# File 'lib/worker_tools/xlsx_output.rb', line 58

def xlsx_ensure_output_target_folder
  FileUtils.mkdir_p(xlsx_output_target_folder) unless File.directory?(xlsx_output_target_folder)
end

#xlsx_insert_headers(spreadsheet, headers) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/worker_tools/xlsx_output.rb', line 62

def xlsx_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_insert_rows(spreadsheet, rows, headers) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/worker_tools/xlsx_output.rb', line 76

def xlsx_insert_rows(spreadsheet, rows, headers)
  rows.each_with_index do |row, row_index|
    xlsx_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_iterators(iterable, compare_hash = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/worker_tools/xlsx_output.rb', line 84

def xlsx_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_column_formatObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/worker_tools/xlsx_output.rb', line 40

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_headersObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/worker_tools/xlsx_output.rb', line 26

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_contentObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/worker_tools/xlsx_output.rb', line 11

def xlsx_output_content
  {
    sheet1: {
      label: 'Sheet 1',
      headers: xlsx_output_column_headers,
      rows: xlsx_output_values,
      column_style: xlsx_output_column_format
    }
  }
end

#xlsx_output_targetObject

if defined, this file will be written to this destination (regardless of whether the model saves the file as well)



6
7
8
9
# File 'lib/worker_tools/xlsx_output.rb', line 6

def xlsx_output_target
  # Ex: Rails.root.join('shared', 'foo', 'bar.xlsx')
  raise "xlsx_output_target has to be defined in #{self}"
end

#xlsx_output_target_folderObject



54
55
56
# File 'lib/worker_tools/xlsx_output.rb', line 54

def xlsx_output_target_folder
  @xlsx_output_target_folder ||= File.dirname(xlsx_output_target)
end

#xlsx_output_valuesObject



22
23
24
# File 'lib/worker_tools/xlsx_output.rb', line 22

def xlsx_output_values
  raise "xlsx_output_values has to be defined in #{self}"
end

#xlsx_style_columns(spreadsheet, styles, headers) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/worker_tools/xlsx_output.rb', line 94

def xlsx_style_columns(spreadsheet, styles, headers)
  return false unless headers

  xlsx_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_write_output_targetObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/worker_tools/xlsx_output.rb', line 116

def xlsx_write_output_target
  xlsx_ensure_output_target_folder

  book = RubyXL::Workbook.new
  xlsx_output_content.each_with_index do |(_, object), index|
    xlsx_write_sheet(book, object, index)
  end

  book.write xlsx_output_target
end

#xlsx_write_sheet(workbook, sheet_content, index) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/worker_tools/xlsx_output.rb', line 106

def xlsx_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_style_columns(sheet, sheet_content[:column_style], sheet_content[:headers])
  xlsx_insert_headers(sheet, sheet_content[:headers])
  xlsx_insert_rows(sheet, sheet_content[:rows], sheet_content[:headers])
end