Class: SpreadsheetEncrypt::XlsConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheet_encrypt/xls_converter.rb

Overview

Converts a legacy .xls workbook into a temporary .xlsx file (cell values only).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path) ⇒ XlsConverter

Returns a new instance of XlsConverter.



15
16
17
# File 'lib/spreadsheet_encrypt/xls_converter.rb', line 15

def initialize(input_path)
  @input_path = input_path
end

Class Method Details

.to_xlsx(input_path) ⇒ Object



11
12
13
# File 'lib/spreadsheet_encrypt/xls_converter.rb', line 11

def self.to_xlsx(input_path)
  new(input_path).to_xlsx
end

Instance Method Details

#to_xlsx ⇒ Tempfile

Returns caller must close/unlink when done.

Returns:

  • (Tempfile) —

    caller must close/unlink when done



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/spreadsheet_encrypt/xls_converter.rb', line 20

def to_xlsx
  book = Roo::Spreadsheet.open(@input_path, extension: :xls)
  tempfile = Tempfile.new(["spreadsheet_encrypt", ".xlsx"])
  tempfile.binmode

  package = Axlsx::Package.new
  book.each_with_pagename do |name, sheet|
    package.workbook.add_worksheet(name: sanitize_sheet_name(name)) do |ws|
      next if sheet.first_row.nil? || sheet.last_row.nil?

      (sheet.first_row..sheet.last_row).each do |row_index|
        ws.add_row(Array(sheet.row(row_index)))
      end
    end
  end

  package.serialize(tempfile.path)
  tempfile.rewind
  tempfile
end