Last active
November 3, 2015 00:37
rspreadsheet gem usage example (https://github.com/gorn/rspreadsheet)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In that particular case it would be better to use
#reduce
instead of#each
in line #27.