Class: SpreadsheetFuel::Library::Deserialize::Xlsx
- Inherits:
-
Burner::JobWithRegister
- Object
- Burner::JobWithRegister
- SpreadsheetFuel::Library::Deserialize::Xlsx
- Defined in:
- lib/spreadsheet_fuel/library/deserialize/xlsx.rb
Overview
Read in the input register and parse it as a Microsoft Excel Open XML Spreadsheet. Sheets can be specified and mapped to registers using the sheet_mappings option. If not sheet_mappings exist, then the default functionality will be to parse the default sheet and overwrite the input register with the sheet’s parsed contents. The parsed content for each sheet will be a two-dimensional array where each row is in its own second level array so that cell B3 would be at index [2].
Expected Payload input: XLSX blob of data. Payload output: array of arrays.
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/deserialize/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) 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/deserialize/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 |
# File 'lib/spreadsheet_fuel/library/deserialize/xlsx.rb', line 39 def perform(output, payload) output.detail("Reading spreadsheet in register: #{register}") value = payload[register] io = StringIO.new(value) book = Roo::Excelx.new(io) sheet_mappings.each do |sheet_mapping| rows = sheet_mapping.sheet ? book.sheet(sheet_mapping.sheet).to_a : book.to_a sheet = friendly_sheet(sheet_mapping.sheet) = <<~MESSAGE Loading #{rows.length} record(s) from #{sheet} into register: #{register} MESSAGE output.detail() payload[sheet_mapping.register] = rows end end |