Skip to content

Instantly share code, notes, and snippets.

@rubiety
Created August 11, 2010 20:15
Show Gist options
  • Save rubiety/519650 to your computer and use it in GitHub Desktop.
Save rubiety/519650 to your computer and use it in GitHub Desktop.
## Concatenates a subset of columns in Excel amongst multiple sheets into one dump
require "rubygems"
require "active_support"
require "roo"
require "faster_csv"
COLUMNS = ["A", "B"] # Column names of whatever you want
[].tap do |o|
Excel.new("/full/path/to/file.csv").sheets.each do |sheet|
2.upto(sheet.last_row) do |i|
o << COLUMNS.map {|c| sheet.cell(i, c) }
end
end
end.to_csv
# Or this crazier way using triply-nested array mapping and depth-flattening...
Excel.new("/full/path/to/file.csv").sheets.map do |sheet|
[2..sheet.last_row].map do |i|
COLUMNS.map {|c| sheet.cell(i, c) }
end
end.flatten(1).to_csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment