Class: Markdownator::Converters::Xlsx

Inherits:
Base
  • Object
show all
Defined in:
lib/markdownator/converters/xlsx.rb

Overview

Converts an Excel .xlsx workbook into Markdown: one ‘## SheetName` heading and a Markdown table per sheet, using the `roo` gem.

Instance Method Summary collapse

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/markdownator/converters/xlsx.rb', line 10

def accepts?(_io, stream_info)
  matches?(
    stream_info,
    extensions: %w[xlsx],
    mimetypes: %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet]
  )
end

#convert(io, _stream_info, **_options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/markdownator/converters/xlsx.rb', line 18

def convert(io, _stream_info, **_options)
  Markdownator.require_optional("roo", feature: "XLSX conversion")

  with_tempfile(io) do |path|
    workbook = Roo::Excelx.new(path)
    sections = workbook.sheets.map { |name| render_sheet(workbook, name) }
    Result.new(markdown: sections.compact.join("\n\n"))
  end
rescue StandardError => e
  raise FileConversionError, "Could not read XLSX: #{e.message}"
end