Class: Decidim::Exporters::Excel

Inherits:
CSV show all
Defined in:
lib/decidim/exporters/excel.rb

Overview

Exports any serialized object (Hash) into a readable Excel file. It transforms the columns using slashes in a way that can be afterwards reconstructed into the original nested hash.

For example, ‘{ name: { ca: “Hola”, en: “Hello” } }` would result into the columns: `name/ca` and `name/es`.

It will maintain types like Integers, Floats & Dates so Excel can deal with them.

Instance Method Summary collapse

Methods inherited from Exporter

#initialize

Constructor Details

This class inherits a constructor from Decidim::Exporters::Exporter

Instance Method Details

#exportObject

Public: Exports a file in an Excel readable format.

Returns an ExportData instance.



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
# File 'lib/decidim/exporters/excel.rb', line 20

def export
  book = Spreadsheet::Workbook.new
  sheet = book.create_worksheet
  sheet.name = "Export"

  sheet.row(0).default_format = Spreadsheet::Format.new(
    weight: :bold,
    pattern: 1,
    pattern_fg_color: :xls_color_14,
    horizontal_align: :center
  )

  sheet.row(0).replace headers

  headers.length.times.each do |index|
    sheet.column(index).width = 20
  end

  processed_collection.each_with_index do |resource, index|
    sheet.row(index + 1).replace(headers.map { |header| custom_sanitize(resource[header]) })
  end

  output = StringIO.new
  book.write output

  ExportData.new(output.string, "xls")
end