class SpreadsheetFuel::Library::Deserialize::Xlsx

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.

Attributes

sheet_mappings[R]

Public Class Methods

new(name: '', register: Burner::DEFAULT_REGISTER, sheet_mappings: nil) click to toggle source
Calls superclass method
# 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

Public Instance Methods

perform(output, payload) click to toggle source
# 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 = <<~MESSAGE
              Loading #{rows.length} record(s) from #{sheet} into register: #{register}
            MESSAGE

            output.detail(message)

            payload[sheet_mapping.register] = rows
          end
        end

Private Instance Methods

friendly_sheet(name) click to toggle source
# File lib/spreadsheet_fuel/library/deserialize/xlsx.rb, line 61
def friendly_sheet(name)
  name ? "sheet: #{name}" : 'the default sheet'
end