Sha256: 1945f9bf9e1148fca5be1c1afec10ade101cd8f571255de806d1ff97c4483516

Contents?: true

Size: 1021 Bytes

Versions: 4

Compression:

Stored size: 1021 Bytes

Contents

require "metacrunch/file"
require "axlsx"

module Metacrunch
  class File::XLSXDestination

    DEFAULT_OPTIONS = {
      worksheet_title: "My data"
    }

    def initialize(filename, columns, options = {})
      @filename = filename
      @columns = columns
      @options = DEFAULT_OPTIONS.deep_merge(options)

      @package = Axlsx::Package.new
      @workbook = @package.workbook
      @sheet = @workbook.add_worksheet(name: @options[:worksheet_title])

      @sheet.add_row(columns, types: :string)
    end

    def write(row)
      return if row.blank?
      @sheet.add_row(row, types: :string)
    end

    def close
      # Make the first row a header
      @sheet.sheet_view.pane do |pane|
        pane.top_left_cell = "A2"
        pane.state = :frozen_split
        pane.y_split = 1
        pane.x_split = 0
        pane.active_pane = :bottom_right
      end

      # Add a filter
      @sheet.auto_filter = @sheet.dimension.sqref

      # Generate file
      @package.serialize(@filename)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
metacrunch-file-1.3.0 lib/metacrunch/file/xlsx_destination.rb
metacrunch-file-1.2.1 lib/metacrunch/file/xlsx_destination.rb
metacrunch-file-1.2.0 lib/metacrunch/file/xlsx_destination.rb
metacrunch-file-1.1.0 lib/metacrunch/file/xlsx_destination.rb