Sha256: d838d85eb3d286f894e79d09f718ac6f2220a4f9f85b0af4a78022ce517b7635

Contents?: true

Size: 1.31 KB

Versions: 37

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require "rubyXL"

module Decidim
  module Admin
    module Import
      module Readers
        # Imports any exported XLSX file to local objects. It transforms the
        # import data using the creator into the final target objects.
        class XLSX < Base
          MIME_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

          def self.first_data_index
            1
          end

          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

          # Returns a StringIO
          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
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.4 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.27.9 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.29.0 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.3 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.27.8 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.29.0.rc4 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.29.0.rc3 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.29.0.rc2 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.29.0.rc1 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.2 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.27.7 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.1 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.27.6 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.26.10 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.26.9 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.0 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.27.5 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.0.rc5 lib/decidim/admin/import/readers/xlsx.rb
decidim-admin-0.28.0.rc4 lib/decidim/admin/import/readers/xlsx.rb