glebm / to_spreadsheet

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

Search for .xlsx.* views before .html.* #10

Closed wooly closed 11 years ago

wooly commented 11 years ago

Hey,

In my controller, I have the following:

format.xlsx { render xlsx: :show, filename: "my_items_doc" }

and my show.xlsx.erb looks like this:

<table class="data">
  <tr>
    <th>Entry Header 1</th>
    <th>Entry Header 2</th>
    <th>Entry Header 3</th>
    <th>Entry Header 4</th>
  </tr>
  <tr>
    <td>Entry First Line 1</td>
    <td>Entry First Line 2</td>
    <td>Entry First Line 3</td>
    <td>Entry First Line 4</td>
  </tr>
  <tr>
    <td>Entry Line 1</td>
    <td>Entry Line 2</td>
    <td>Entry Line 3</td>
    <td>Entry Line 4</td>
  </tr>
  <tr>
    <td>Entry Last Line 1</td>
    <td>Entry Last Line 2</td>
    <td>Entry Last Line 3</td>
    <td>Entry Last Line 4</td>
  </tr>
</table>

However, this results in an empty spreadsheet document being created with two empty sheets in it. Any idea what I'm doing wrong?

glebm commented 11 years ago

Hi! to_spreadsheet at the moment will only look for .html.* views (as I can see from your use case I'll need to change this in the future).

Please try renaming show.xlsx.erb to show.html.erb and let me know if it works

I will update the docs and improve the template handling some time later this week

wooly commented 11 years ago

Ahh alright, I'll give that a try! thanks!

Edit: That's working great. I'll probably just create a new member method to put the stuff I currently have in show.html.erb as it shouldn't really be there. Thanks though!

glebm commented 11 years ago

to_spreadsheet will only use the tables from your view (ignoring the rest), so you should be able to reuse the same view for both html and xlsx

Custom formatting can be done by using format_xls (as in readme)

Will keep this open, because to_spreadsheet should look for .xlsx.* views first and only then fallback to .html.*

wooly commented 11 years ago

ahh grand, that's good to know. Thanks!