Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created April 2, 2012 11:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohgpat/2282876 to your computer and use it in GitHub Desktop.
Save kohgpat/2282876 to your computer and use it in GitHub Desktop.
Ruby spreadsheet example
1 require 'spreadsheet'
# Open workbooks
current_workbook = Spreadsheet.open('jan.xls')
total_workbook = Spreadsheet.open('output.xls')
# Process xls
# Iterate through workbook spreadsheets
current_workbook.worksheets.each_with_index do |current_spreadsheet, spreadsheet_index|
# Open current sheet on total xls
total_sheet = total_workbook.worksheet(spreadsheet_index)
row_blacklist = [0, 1, 7]
col_blacklist = [0, 3]
current_spreadsheet.each_with_index do |row, row_idx|
row.each_with_index do |value, col_idx|
if row_blacklist.include?(row_idx)
next
end
if col_blacklist.include?(col_idx)
next
end
if value.class.to_s != 'Spreadsheet::Formula'
total_sheet.row(row_idx)[col_idx] = total_sheet.row(row_idx)[col_idx].to_i + value.to_i
end
end
end
total_workbook.write('output2.xls')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment