Class: XlsxExportService

Inherits:
Object
  • Object
show all
Includes:
DataExportHelper
Defined in:
app/models/rails_data/export_services/xlsx_export_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DataExportHelper

#convert_parameters, #field_result, #footer_result, #header_result

Constructor Details

#initialize(table_list: nil, data_list: nil, params: {}, headers: []) ⇒ XlsxExportService

Returns a new instance of XlsxExportService.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 8

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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 4

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



4
5
6
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 4

def params
  @params
end

#sheetObject (readonly)

Returns the value of attribute sheet.



4
5
6
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 4

def sheet
  @sheet
end

#table_listObject (readonly)

Returns the value of attribute table_list.



4
5
6
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 4

def table_list
  @table_list
end

Instance Method Details

#cached_xlsxObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 38

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

#direct_xlsxObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/rails_data/export_services/xlsx_export_service.rb', line 26

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