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.

Reads the OOXML zip directly with rubyzip and Nokogiri, the same approach used by the DOCX, PPTX, and EPUB converters.

Instance Method Summary collapse

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


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

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/markdownator/converters/xlsx.rb', line 19

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

  ::Zip::File.open_buffer(io) do |zip|
    shared = shared_strings(zip)
    sections = sheets(zip).filter_map do |name, path|
      table = markdown_table(parse_sheet(read(zip, path), shared))
      "## #{name}\n\n#{table}" unless table.empty?
    end
    return Result.new(markdown: sections.join("\n\n"))
  end
rescue StandardError => e
  raise FileConversionError, "Could not read XLSX: #{e.message}"
end