Class: Rubino::Documents::Converters::Xlsx

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/documents/converters/xlsx.rb

Overview

XLSX (and ODS/legacy XLS where roo supports them) -> Markdown. Each sheet becomes a ‘## SheetName` heading followed by a GFM table emitted by the shared Table emitter. The `roo` gem (MIT) is OPTIONAL: #available? reports false when it can’t be required, so the registry never offers this converter on an install without roo – the caller then falls back to the shell-extraction hint.

Constant Summary collapse

MIMES =
%w[
  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  application/vnd.oasis.opendocument.spreadsheet
  application/vnd.ms-excel
].freeze
EXTS =
%w[.xlsx .ods .xls].freeze

Instance Method Summary collapse

Instance Method Details

#accepts?(mime, path) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/rubino/documents/converters/xlsx.rb', line 27

def accepts?(mime, path)
  return true if MIMES.include?(mime.to_s)

  EXTS.include?(File.extname(path.to_s).downcase)
end

#available?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/rubino/documents/converters/xlsx.rb', line 20

def available?
  require "roo"
  true
rescue LoadError
  false
end

#convert(path) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubino/documents/converters/xlsx.rb', line 33

def convert(path)
  require "roo"
  book = Roo::Spreadsheet.open(path)
  parts = book.sheets.map { |name| sheet_markdown(book, name) }.compact
  parts.join("\n\n")
ensure
  book&.close if defined?(book) && book.respond_to?(:close)
end