Class: DocumentToRichHtml::ExcelConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/document_to_rich_html/excel_converter.rb

Class Method Summary collapse

Class Method Details

.convert(file_path) ⇒ Object



7
8
9
10
# File 'lib/document_to_rich_html/excel_converter.rb', line 7

def self.convert(file_path)
  content = extract_content(file_path)
  HtmlFormatter.format(content)
end

.extract_content(file_path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/document_to_rich_html/excel_converter.rb', line 12

def self.extract_content(file_path)
  spreadsheet = Roo::Spreadsheet.open(file_path)
  html = '<table>'
  spreadsheet.each_with_index do |row, index|
    html += index.zero? ? '<thead><tr>' : '<tr>'
    row.each_with_index do |cell, cell_index|
      cell_style = spreadsheet.font(index, cell_index)
      style = "style='"
      style += 'font-weight: bold;' if cell_style&.bold?
      style += 'font-style: italic;' if cell_style&.italic?
      style += 'text-decoration: underline;' if cell_style&.underline?
      style += "'"
      html += index.zero? ? "<th #{style}>#{cell}</th>" : "<td #{style}>#{cell}</td>"
    end
    html += index.zero? ? '</tr></thead><tbody>' : '</tr>'
  end
  html += '</tbody></table>'
  html
end