Skip to content

Instantly share code, notes, and snippets.

@gorn
Last active November 3, 2015 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorn/42e33d086d9b4fda10ec to your computer and use it in GitHub Desktop.
Save gorn/42e33d086d9b4fda10ec to your computer and use it in GitHub Desktop.
rspreadsheet gem usage example (https://github.com/gorn/rspreadsheet)
require 'rspreadsheet'
book = Rspreadsheet.open('./test.ods')
sheet = book.worksheets(1)
# get value of a cell B5 (there are more ways to do this)
sheet.B5 # => 'cell value'
sheet[5,2] # => 'cell value'
sheet.row(5).cell(2).value # => 'cell value'
# set value of a cell B5
sheet.F5 = 'text'
sheet[5,2] = 7
sheet.cell(5,2).value = 1.78
# working with cell format
sheet.cell(5,2).format.bold = true
sheet.cell(5,2).format.background_color = '#FF0000'
# calculating sum of cells in row
sheet.row(5).cellvalues.sum
sheet.row(5).cells.sum{ |cell| cell.value.to_f }
# iterating over list of people and displaying the data
total = 0
sheet.rows.each do |row|
puts "Sponsor #{row[1]} with email #{row[2]} has donated #{row[3]} USD."
total += row[3].to_f
end
puts "Totally fundraised #{total} USD"
# saving file
book.save
book.save('different_filename.ods')
@zekefast
Copy link

In that particular case it would be better to use #reduce instead of #each in line #27.

@gorn
Copy link
Author

gorn commented Nov 3, 2015

@zekefast I agree that this is not a best example, it was intended like example of simple iteration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment