Class: Decidim::Admin::Import::Readers::XLSX

Inherits:
Base
  • Object
show all
Defined in:
decidim-admin/lib/decidim/admin/import/readers/xlsx.rb

Overview

Imports any exported XLSX file to local objects. It transforms the import data using the creator into the final target objects.

Constant Summary collapse

MIME_TYPE =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Decidim::Admin::Import::Readers::Base

Class Method Details

.first_data_indexObject


14
15
16
# File 'decidim-admin/lib/decidim/admin/import/readers/xlsx.rb', line 14

def self.first_data_index
  1
end

Instance Method Details

#example_file(data) ⇒ Object

Returns a StringIO


33
34
35
36
37
38
39
40
41
42
43
44
# File 'decidim-admin/lib/decidim/admin/import/readers/xlsx.rb', line 33

def example_file(data)
  workbook = RubyXL::Workbook.new
  sheet = workbook.worksheets[0]

  data.each_with_index do |row, rowi|
    row.each_with_index do |col, coli|
      sheet.add_cell(rowi, coli, col)
    end
  end

  workbook.stream
end

#read_rowsObject


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'decidim-admin/lib/decidim/admin/import/readers/xlsx.rb', line 18

def read_rows
  workbook = RubyXL::Parser.parse(file)
  sheet = workbook.worksheets[0]
  sheet.each_with_index do |row, index|
    if row
      yield row.cells.map { |c| c && c.value }, index
    else
      yield [], index
    end
  end
rescue Zip::Error
  raise Decidim::Admin::Import::InvalidFileError, "The provided XLSX file is not valid"
end