Skip to content

Instantly share code, notes, and snippets.

View randym's full-sized avatar

Randy Morgan randym

  • Freelance
  • Ishigaki, Japan
View GitHub Profile
@randym
randym / boxed_border.rb
Created October 30, 2012 14:22
Surrounding Box Border in Axlsx
require 'axlsx'
p = Axlsx::Package.new
p.workbook do |wb|
# Stuff like this is why I LOVE RUBY
# If you dont know about hash default values
# LEARN IT! LIVE IT! LOVE IT!
defaults = { :style => :thick, :color => "000000" }
borders = Hash.new do |hash, key|
hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } )
@randym
randym / outline_level.rb
Created December 14, 2012 10:15
Collapsed outline level
#```ruby
wb.add_worksheet(name: 'outline_level') do |sheet|
sheet.add_row [1, 2, 3, 4, Time.now, 149455.15]
sheet.add_row [1, 2, 5, 6, Time.now,14100.19]
sheet.add_row [9500002267, 1212, 1212, Time.now,14100.19]
sheet.add_row [], :collapsed => 1
sheet.rows[0..2].each do |row|
row.outline_level = 1
# This will collapse the outline level rows
@randym
randym / comment-hack.rb
Created October 11, 2013 01:26
axlsx hacking comment position
# Hacking comment position!
# Use at your own risk :)
# add_comment is the preferred way to add comments to your worksheet. However, if you really need to position a
# a specific comment, here is one way to do it.
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
wb.add_worksheet(:name => 'comments') do |sheet|
sheet.add_row ['Can we build it?', 'Yes we can!']
sheet.add_comment(:ref => 'A1', :author => 'Bob', :text => 'Yes We Can!').tap do |comment|
@randym
randym / axlsx.example.rb
Created April 12, 2012 23:42
axlsx Generating Excel documents with Ruby
require 'axlsx'
Axlsx::Package.new do |p|
p.workbook do |wb|
styles = wb.styles
header = styles.add_style :bg_color => "DD", :sz => 16, :b => true, :alignment => {:horizontal => :center}
tbl_header = styles.add_style :b => true, :alignment => { :horizontal => :center }
ind_header = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => {:indent => 1}
col_header = styles.add_style :bg_color => "FFDFDEDF", :b => true, :alignment => { :horizontal => :center }
label = styles.add_style :alignment => { :indent => 1 }
@randym
randym / getting_barred.rb
Created April 24, 2012 23:16
Axlxs: Writing Excel With Ruby - Conditional Formatting
require 'axlsx'
p = Axlsx::Package.new
p.workbook do |wb|
# define your regular styles
styles = wb.styles
title = styles.add_style :sz => 15, :b => true, :u => true
default = styles.add_style :border => Axlsx::STYLE_THIN_BORDER
header = styles.add_style :bg_color => '00', :fg_color => 'FF', :b => true
money = styles.add_style :format_code => '#,###,##0', :border => Axlsx::STYLE_THIN_BORDER
percent = styles.add_style :num_fmt => Axlsx::NUM_FMT_PERCENT, :border => Axlsx::STYLE_THIN_BORDER
@randym
randym / date_styles.rb
Created September 14, 2012 02:12
Axlsx date formatting with custom styles
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'axlsx'
require 'date'
p = Axlsx::Package.new
wb = p.workbook
wb.styles do |style|
# Date/Time Styles
#
@randym
randym / example_table_autofilter.rb
Created July 25, 2012 23:21
Example: Setting up auto filters / Table with axlsx
#Using Tables:
#This will add in the sorting filters for you as well as apply default table styling in excel.
#LibraOffice does the filters, but not the styling (as does Google docs if I recall correctly.)
#```ruby
wb.add_worksheet(:name => "Table") do |sheet|
sheet.add_row ["Build Matrix"]
sheet.add_row ["Build", "Duration", "Finished", "Rvm"]
sheet.add_row ["19.1", "1 min 32 sec", "about 10 hours ago", "1.8.7"]