• Stars
    star
    53
  • Rank 552,529 (Top 11 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 16 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

ODF generation library for Ruby

RODF

Gem Version CI Status RubyGems Downloads Buy Me a Coffee

RODF is a library for writing to ODF output from Ruby. It mainly focuses creating ODS spreadsheets.

As well as writing ODS spreadsheets, this library also can write ODT text documents but it is undocumented and will require knowledge of the ODF spec. It currently does not support ODP Slide shows. Also this is NOT an ODF reading library.

Install

gem install rodf

Usage

RODF works pretty much like Builder, but with ODF-aware constructs. For example:

RODF::Spreadsheet.file("my-spreadsheet.ods") do
  table 'My first table from Ruby' do
    row do
      cell 'Hello world!'
    end
  end
end

For access to variables and methods from outer code you can use block parameter:

@data = 'Hello world!'

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
  sheet.table 'My first table from Ruby' do |table|
    table.row do |row|
      row.cell @data
    end
  end
end

Adding many rows or cells at once is supported as well:

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
  sheet.table 'My first table from Ruby' do |t|
    t.add_rows([
      [1, 'Alice'],
      [2, { value: 'Bob', color: '#ff0000'}],
      [3, 'Carol']
    ])

    t.row do |r|
      r.add_cells ['ID', 'Name']
    end
  end
end

Procedural style

The declarative style shown above is just syntatic sugar. A more procedural style can also be used. Like so:

ss = RODF::Spreadsheet.new

t = ss.table 'My first table from Ruby'
r = t.row
c = r.cell 'Hello world!'

# two methods to write to file
ss.write_to 'my-spreadsheet.ods'
# or
File.write('my-spreadsheet.ods', ss.bytes) # you can send your data in Rails over HTTP using the bytes method

Both styles can be mixed and matched at will:

ss = RODF::Spreadsheet.new

ss.table 'My first table from Ruby' do |t|
  t.row do |r|
    r.cell 'Hello world!'
  end
end

ss.write_to 'my-spreadsheet.ods'

Styling and Formatting

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|

  sheet.style 'red-cell', family: :cell do |s|
    s.property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
  end

  sheet.style 'row-height', family: :row do |s|
    s.property :row, 'row-height' => '18pt', 'use-optimal-row-height' => 'true'
  end

  sheet.table 'Red text table' do |t|
    t.row style: 'row-height' do |r|
      r.cell 'Red', style: 'red-cell'
    end
  end

  sheet.table 'Text with Paragraphs' do |t|
    t.row style: 'row-height' do |r|
      r.cell do |cell|
        cell.paragraph do |paragraph|
          text_array = my_text_content.split("\n").select{|x| !x.empty? }

          text_array.each do |str|
            if str.start_with?("#")
              paragraph.span(str, style: 'red-cell')
            else
              paragraph.span(str)
            end
          end
        end
      end
    end
  end

end

Conditional formatting is also possible:

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|

  sheet.office_style 'red-cell', family: :cell do |s|
    s.property :text, 'font-weight' => 'bold', 'color' => '#ff0000'
  end

  sheet.office_style 'green-cell', family: :cell do |s|
    s.property :text, 'font-weight' => 'bold', 'color' => '#00ff00'
  end

  # conditional formating must be defined as style and the value of
  # apply-style-name must be an office_style
  sheet.style 'cond1', family: :cell do |s|
    s.property :conditional, 'condition' => 'cell-content()<0', 'apply-style-name' => 'red-cell'

    s.property :conditional, 'condition' => 'cell-content()>0', 'apply-style-name' => 'green-cell'
  end

  sheet.table 'Red text table' do |t|
    t.row do |r|
      r.cell 'Red force', style: 'red-cell'
    end
    t.row do |r|
      r.cell '-4', type: :float, style: 'cond1'
    end
    t.row do |r|
      r.cell '0', type: :float, style: 'cond1'
    end
    t.row do |r|
      r.cell '5', type: :float, style: 'cond1'
    end
  end
end

Changing Columns Widths

Adding columns or columns width to your spreadsheet can be done with the following

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
  sheet.table "foo" do |t|
    sheet.style('default-col-width', family: :column) do |s|
      s.property(:column, 'column-width' => '1.234in')
    end

    col_count.times do
      t.column style: 'default-col-width'
    end

    ### OR

    ### Warning this will overwrite any existing table columns (cells remain unaffected)
    sheet.set_column_widths(
      table: t, 
      column_widths: [
        {'column-width' => '1in'},
        {'column-width' => '2cm'},
        {'column-width' => '2.54cm'},
      ],
    )
  end
end

Columns Types

Available columns types are:

  • :string
  • :float
  • :date
  • :time
  • :currency
  • :percentage

Style List

### family: :cell or "table-cell"
style "my-cell-style", family: :cell do
  property :text,
    'font-weight' => :bold, #options are :bold, :thin
    'font-size' => 12,
    'font-name' => 'Arial',
    'font-style' => 'italic',
    'text-underline-style' => 'solid', # solid, dashed, dotted, double
    'text-underline-type' => 'single',
    'text-line-through-style' => 'solid',
    align: true,
    color: "#000000"

  property :cell,
    'background-color' => "#DDDDDD",
    'wrap-option' => 'wrap',
    'vertical_align' => 'automatic',
    'border-top' => '0.75pt solid #999999',
    'border-bottom' => '0.75pt solid #999999',
    'border-left' => '0.75pt solid #999999',
    'border-right' => '0.75pt solid #999999',
    'writing-mode' => 'lr-tb',

end

### family: :column or "table-column"
style('my-col-style', family: :column) do
  property :column,
    'column-width' => '4.0cm'
end

### family: :row or "table-row"
style('my-row-style', family: :row) do
  property :row,
    'row-height' => '18pt',
    'use-optimal-row-height' => 'true'
end

### family: :table
style('my-row-style', family: :table) do
  property :table,
    'writing-mode' => 'lr-tb',

Adding Arbitrary XML Attributes

RODF::Spreadsheet.file("my-spreadsheet.ods") do |sheet|
  sheet.table 'My first table from Ruby', attributes: {'table-protected' => 'true'} do |table|
    table.row do |row|
      row.cell @data
    end
  end
end

Production Usage Examples

Credits

Originally Created by @thiagoarrais

Maintained by @westonganger since 2016, for simplified ODS spreadsheet creation within the spreadsheet_architect gem

More Repositories

1

spreadsheet_architect

Spreadsheet Architect is a library that allows you to create XLSX, ODS, or CSV spreadsheets super easily from ActiveRecord relations, plain Ruby objects, or tabular data.
Ruby
1,298
star
2

rails_i18n_manager

Web interface to manage i18n translations helping to facilitate the editors of your translations. Provides a low-tech and complete workflow for importing, translating, and exporting your I18n translation files. Designed to allow you to keep the translation files inside your projects git repository where they should be.
Ruby
205
star
3

paper_trail-association_tracking

Plugin for the PaperTrail gem to track and reify associations
Ruby
118
star
4

rearmed-js

A collection of helpful methods and monkey patches for Arrays, Objects, Numbers, and Strings in Javascript
JavaScript
104
star
5

active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
Ruby
96
star
6

protected_attributes_continued

The community continued version of protected_attributes for Rails 5+
Ruby
45
star
7

rearmed-rb

A collection of helpful methods and monkey patches for Arrays, Hash, Enumerables, Strings, Objects & Dates in Ruby
Ruby
41
star
8

sexy_form.rb

Dead simple HTML form field builder for Ruby with built-in support for many popular UI libraries such as Bootstrap
Ruby
38
star
9

rearmed_rails

A collection of helpful methods and monkey patches for Rails
Ruby
33
star
10

form_builder.cr

Dead simple HTML form builder for Crystal with built-in support for many popular UI libraries such as Bootstrap
Crystal
31
star
11

bootstrap-directional-buttons

Directional / Arrow buttons for Bootstrap
HTML
22
star
12

capistrano-precompile-chooser

Capistrano plugin to precompile your Rails assets locally, remotely, or not at all provided with a very convenient default terminal prompt.
Ruby
13
star
13

active_sort_order

The "easy-peasy" dynamic sorting pattern for ActiveRecord that your Rails apps deserve
Ruby
13
star
14

input-autogrow

jQuery plugin for autogrowing inputs
JavaScript
8
star
15

chosen-bootstrap-theme

A Bootstrap theme for Chosen Select that actually looks like Bootstrap
CSS
8
star
16

js-try

JS-Try is a Javascript implementation of the try method from Rails for safe navigation
JavaScript
6
star
17

chosen-material-theme

A Material theme for Chosen Select
CSS
6
star
18

search_architect

Dead simple, powerful and fully customizable searching for your Rails or ActiveRecord models and associations.
Ruby
6
star
19

rails_uuid_to_integer_primary_keys

A Rails Migration to convert your UUID primary keys back to integer / bigint primary keys
Ruby
5
star
20

pairer

Pairer is Rails app/engine to Easily rotate and keep track of working pairs
Ruby
5
star
21

chosen-remote-source

Provides remote data source support for chosen-js selects
JavaScript
4
star
22

input-case-enforcer

Enforce uppercase, lowercase, or Capitalized inputs & textareas
JavaScript
4
star
23

paperclip_utils

Collection of Paperclip processors and a Helper class for easier dynamic processors and styles on your Paperclip uploads
Ruby
4
star
24

active_record_simple_execute

Sanitize and Execute your raw SQL queries in ActiveRecord and Rails with a much more intuitive and shortened syntax
Ruby
4
star
25

active_record_case_insensitive_finders

Adds case-insensitive finder methods to Rails and ActiveRecord
Ruby
3
star
26

chosen-readonly

Readonly support for Chosen selects
JavaScript
3
star
27

better_exception_notifier

An exception notifier for Rails and Rack apps
Ruby
3
star
28

rails_nestable_layouts

Rails Nestable Layouts - Dead simple nested layouts for Rails
Ruby
3
star
29

accepts_nested_attributes_for_public_id

A patch for Rails to support using a public ID column instead of ID for use with accepts_nested_attributes_for
Ruby
2
star
30

minitest_change_assertions

Provides assertions for your Minitest suite to determine if an object has been changed
Ruby
2
star
31

common_website_scripts_and_styles

JavaScript
2
star
32

rails_template

Efficient Rails Template
Ruby
2
star
33

rearmed-css

CSS Utility Classes
CSS
2
star
34

basic_ruby_and_rails_style_guide

2
star
35

simple_assets

Dead simple HTML-based assets helper for Ruby. The main idea here is to promote re-usability for projects.
Ruby
2
star
36

active_record_alias_join

Easily add custom SQL table aliases for your joins and includes
2
star
37

github_activity_scraper.rb

Scrape your entire GitHub activity information for all-time. Helps for aggregating how much open source sh!t you get done.
Ruby
2
star
38

devise_whos_here

Devise extension for logging current active users logged in using only the fast Rails cache and not your database
Ruby
2
star
39

github_actions_ci_example

Ruby
1
star
40

jekyll_template

CSS
1
star
41

rubocop_template_for_productive_teams

Rubocop template designed speed up your teams development process instead of dragging it down
Ruby
1
star
42

rails_upgrade

Ruby
1
star
43

ruby_view_template_converters

Complete solutions to convert ERB, SLIM, AND HAML with the least amount of manual effort
Ruby
1
star
44

westonganger

1
star
45

no_bullshit_middleman_template

Template for efficient static website generation using Middleman
HTML
1
star
46

prawn_invoice

Dead simple Prawn based PDF invoice generator with support for custom invoice templates
Ruby
1
star
47

rails_dummy_app

Rails Dummy App and test_helper.rb for testing gems
Ruby
1
star
48

wagon_starter

Starter Template for LocomotiveCMS Wagon Sites
JavaScript
1
star
49

essential_capybara_helpers

A set of essential capybara helpers for everyday use
1
star
50

fast_try

FastTry is a simple method wrapper to the safe navigation operator in Ruby
Ruby
1
star
51

lazy_serialize

Lazy Serialize is an alternative to ActiveRecord's serialize method which does not serialize each column until the first call to the attribute.
Ruby
1
star
52

prawn_resume

Dead simple Prawn based PDF resume generator with support for custom resume templates
Ruby
1
star
53

automatic_rails_route_testing

Template for easy exception testing for all routes within a Rails app
Ruby
1
star
54

select-sync

Javascript plugin for HTML `select` elements to synchronize by selected or disabled options
JavaScript
1
star
55

rails_scrabble_with_friends

Simple web-based scrabble for you and your friends with zero friction authentication
Ruby
1
star
56

facebook_marketplace_scraper

A locally run Rails app to automatically search facebook marketplace for deals and aggregate them all in a list.
Ruby
1
star
57

wagon_kitchen_sink

Example website for LocomotiveCMS / Wagon
JavaScript
1
star
58

rails_clientside_javascript_error_handler

Easily handle client-side Javascript exceptions within your Rails app
Ruby
1
star
59

rails_custom_form_builder

A good example/starter pack for a custom form builder for use with Rails form_for
Ruby
1
star