glebm / to_spreadsheet

Render XLSX from Rails using existing views (html ⇒ xlsx)
Other
91 stars 37 forks source link

Styling not working in ERB template #30

Closed rohitpbhore closed 7 years ago

rohitpbhore commented 7 years ago

I could able to successfully deliver .xlsx file using this gem. But facing issue while formatting. I have this below ERB template where I am trying to format, but it didn't work.

results.xlsx.erb

ruby:
  = format_xls 'table.my-table' do
    workbook use_autowidth: true
    format 'th', b: true
    format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
  end

<table id="my-table">
  <tr><td>One</td></tr>
  <tr><td>Two</td></tr>
  <tr><td>Three</td></tr>
  <tr><td>Four</td></tr>
  <tr><td>Five</td></tr>
</table>

Thanks in advance @glebm

glebm commented 7 years ago

That's not how ruby code is written in ERB. Try this instead:

<%
  format_xls 'table.my-table' do
    workbook use_autowidth: true
    format 'th', b: true
    format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
  end
%>

<table id="my-table">
  <tr><td>One</td></tr>
  <tr><td>Two</td></tr>
  <tr><td>Three</td></tr>
  <tr><td>Four</td></tr>
  <tr><td>Five</td></tr>
</table>
rohitpbhore commented 7 years ago

apologize for the silly mistake.

biju-mouli commented 6 years ago

That's not how ruby code is written in ERB. Try this instead: <% format_xls 'table.my-table' do workbook use_autowidth: true format 'th', b: true format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? } end %>

One
Two
Three
Four
Five

I tried this exact same code - I get the spreadsheet without any colors anywhere! Please help!