Sha256: c6876ec98d9a5d333265fb48704459645b39aec219f29a51f6f57646cfc2b603

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'write_xlsx'
class XlsxExportService
  include DataExportHelper
  attr_reader :sheet,
              :table_list,
              :params, :headers

  def initialize(table_list: nil, data_list: nil, params: {}, headers: [])
    if table_list
      @table_list = table_list
      @data_list = table_list.data_list
      @headers = headers.presence || table_list.headers
      convert_parameters(params)
    elsif data_list
      @data_list = data_list
      @headers = headers
      convert_parameters(params)
    end
    @config_table = @data_list.config_table

    @io = StringIO.new
    @workbook = WriteXLSX.new(@io)
    @sheet = @workbook.add_worksheet
  end

  def direct_xlsx
    sheet.write_row(0, 0, headers)

    @config_table.collection.call(@params).each_with_index do |object, index|
      row = field_result(object, index)
      sheet.write_row(index + 1, 0, row)
    end

    @workbook.close
    @io.string
  end

  def cached_xlsx
    sheet.write_row(0, 0, table_list.headers)

    table_list.table_items.each_with_index do |table_item, index|
      sheet.write_row(index + 1, 0, table_item.fields)
    end

    sheet.write_row table_list.table_items_count + 1, 0, table_list.footers

    @workbook.close
    @io.string
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_data-1.0.2 app/models/rails_data/export_services/xlsx_export_service.rb
rails_data-1.0.1 app/models/rails_data/export_services/xlsx_export_service.rb
rails_data-1.0.0 app/models/rails_data/export_services/xlsx_export_service.rb
rails_data-0.0.1 app/models/rails_data/export_services/xlsx_export_service.rb