Class: SpreadsheetFuel::Library::Serialize::Xlsx
- Inherits:
-
Burner::JobWithRegister
- Object
- Burner::JobWithRegister
- SpreadsheetFuel::Library::Serialize::Xlsx
- Defined in:
- lib/spreadsheet_fuel/library/serialize/xlsx.rb
Overview
This job can take in one or many registers and create a Microsoft Excel Open XML Spreadsheet file out of them. Each register will be written to the specified sheets within the workbook. If no sheet_mappings are entered, then the input register will be used for input and output and only one sheet will be created with the name ‘Sheet1’.
Expected Payload input: array of arrays. Payload output: XLSX data string blob.
Constant Summary collapse
- DEFAULT_SHEET =
'Sheet1'
Instance Attribute Summary collapse
-
#sheet_mappings ⇒ Object
readonly
Returns the value of attribute sheet_mappings.
Instance Method Summary collapse
-
#initialize(name: '', register: Burner::DEFAULT_REGISTER, sheet_mappings: nil) ⇒ Xlsx
constructor
A new instance of Xlsx.
- #perform(output, payload) ⇒ Object
Constructor Details
#initialize(name: '', register: Burner::DEFAULT_REGISTER, sheet_mappings: nil) ⇒ Xlsx
Returns a new instance of Xlsx.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/spreadsheet_fuel/library/serialize/xlsx.rb', line 25 def initialize(name: '', register: Burner::DEFAULT_REGISTER, sheet_mappings: nil) super(name: name, register: register) @sheet_mappings = Modeling::SheetMapping.array(sheet_mappings) # If no sheets/register mappings are specified then lets just use the default # sheet and the current register. if @sheet_mappings.empty? @sheet_mappings << Modeling::SheetMapping.new(register: register, sheet: DEFAULT_SHEET) end freeze end |
Instance Attribute Details
#sheet_mappings ⇒ Object (readonly)
Returns the value of attribute sheet_mappings.
23 24 25 |
# File 'lib/spreadsheet_fuel/library/serialize/xlsx.rb', line 23 def sheet_mappings @sheet_mappings end |
Instance Method Details
#perform(output, payload) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/spreadsheet_fuel/library/serialize/xlsx.rb', line 39 def perform(output, payload) output.detail("Writing spreadsheet to register: #{register}") # This will implicitly create a tempfile for FastExcel to use workbook = FastExcel.open sheet_mappings.each do |sheet_mapping| name = sheet_mapping.sheet.to_s worksheet = workbook.add_worksheet(name) rows = array(payload[sheet_mapping.register]) output.detail("Writing #{rows.length} row(s) to sheet: #{name}") rows.each { |row| worksheet.append_row(row) } end # readstring should close and remove the tmpfile for us: # https://github.com/Paxa/fast_excel/blob/master/lib/fast_excel.rb#L393 value = workbook.read_string payload[register] = value end |