• Stars
    star
    92
  • Rank 362,966 (Top 8 %)
  • Language
    Ruby
  • License
    Other
  • Created over 13 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render XLSX from Rails using existing views (html โ‡’ xlsx)

to_spreadsheet Build Status

to_spreadsheet lets your Rails 3+ app render Excel files using the existing slim/haml/erb/etc views.

Installation

Add it to your Gemfile:

gem 'to_spreadsheet'

Usage

In the controller:

# my_thingies_controller.rb
class MyThingiesController < ApplicationController
  respond_to :xlsx, :html
  def index
    @my_items = MyItem.all
    respond_to do |format|
      format.html
      format.xlsx { render xlsx: :index, filename: "my_items_doc" }
    end
  end
end

In the view partial:

# _my_items.haml
%table
  %caption My items
  %thead
    %tr
      %td ID
      %td Name
  %tbody
    - my_items.each do |my_item|
      %tr
        %td.number= my_item.id
        %td= my_item.name
  %tfoot
    %tr
      %td(colspan="2") #{my_items.length}

In the XLSX view:

# index.xlsx.haml
= render 'my_items', my_items: @my_items

In the HTML view:

# index.html.haml
= link_to 'Download spreadsheet', my_items_url(format: :xlsx)
= render 'my_items', my_items: @my_items

Worksheets

Every table in the view will be converted to a separate sheet. The sheet title will be assigned to the value of the tableโ€™s caption element if it exists.

Formatting

You can define formats in your view file (local to the view) or in the initializer

format_xls 'table.my-table' do
  workbook use_autowidth: true
  sheet    orientation: landscape
  format 'th', b: true # bold
  format 'tbody tr', bg_color: lambda { |row| 'ddffdd' if row.row_index.odd? }
  format 'A3:B10', i: true # italic
  format column: 0, width: 35
  format 'td.custom', lambda { |cell| modify cell somehow.}
  # default value (fallback value when value is blank or 0 for integer / float)
  default 'td.price', 10
end

For the full list of supported properties head here: http://rubydoc.info/github/randym/axlsx/Axlsx/Styles#add_style-instance_method In addition, for column formats, Axlsx columnInfo properties are also supported

Advanced formatting

to_spreadsheet associates HTML nodes with Axlsx objects as follows:

HTML tag Axlsx object
table worksheet
tr row
td, th cell

For example, to directly manipulate a worksheet:

format_xls do
  format 'table' do |worksheet|
    worksheet.add_chart ...
    # to get the associated Nokogiri node:
    el = context.to_xml_node(worksheet)
  end
end

Themes

You can define themes, i.e. blocks of formatting code:

ToSpreadsheet.theme :zebra do
  format 'tr', bg_color: lambda { |row| 'ddffdd' if row.row_index.odd? }
end

And then use them:

format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)

Using along side axlsx-rails

If you are using axlsx-rails, :xlsx renderer might have already been defined. In that case define a custome renderer using

# app/config/application.rb
config.to_spreadsheet.renderer = :html2xlsx

And then in controller

respond_to do |format|
  format.html2xlsx
end

Types

The default theme uses class names on td/th to cast values. Here is the list of class to type mapping:

CSS class Format
decimal or float Decimal
num or int Integer
datetime DateTime (Chronic.parse)
date Date (Date.parse)
time Time (Chronic.parse)

More Repositories

1

i18n-tasks

Manage translation and localization with static analysis, for Ruby i18n
Ruby
2,060
star
2

rails_email_preview

Preview and edit app mailer templates in Rails.
Ruby
567
star
3

order_query

Find next / previous Active Record(s) in one query
Ruby
507
star
4

gulp-webpack-react-bootstrap-sass-template

Web App Client Template: React. Sass, Coffee, JSX. Bootstrap for Sass. Compiled with Gulp and Webpack.
CoffeeScript
111
star
5

render-whitespace-on-github

Are they tabs? Are they spaces? How many? Never wonder again!
JavaScript
73
star
6

DOMBrew

A fast 2.5 KB client-side DOM builder
CoffeeScript
28
star
7

popper_js-rubygem

Popper.js assets as a Ruby gem. https://popper.js.org/
Ruby
24
star
8

katex-ruby

Renders KaTeX from Ruby.
Ruby
24
star
9

sass-rewrite-url

Automatically rewrite paths in url() calls. Avoid asset-url.
CSS
12
star
10

redis_stats

Ruby
4
star
11

blog-glebm-com

Sources for blog.glebm.com
CSS
3
star
12

critical-rubygem

The `critical` npm package wrapped as a Ruby gem
Ruby
3
star
13

mini_settings

A minimal key-value config file reader/writer in C
C
2
star
14

glebm-nokogiri

a version of nokogiri that doesn't have the segfault problem. installable from bundler as git
Ruby
2
star
15

advent-of-code

Julia
2
star
16

od-slitherlink

Dingoo Slitherlink re-packaged for OpenDingux and RetroFW
C++
1
star
17

has_cache_key

Ruby
1
star
18

LifeGL

OpenGL driven cellular automata simulator (Delphi, 2007)
1
star
19

timeago_js-rubygem

Timeago.js assets as a Ruby gem. http://timeago.org/
JavaScript
1
star
20

Dicey

Configurable Dice app for DnD and other games (Delphi) --go to wiki for details
1
star