Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
More possibilities with rspreadshhet (https://github.com/gorn/rspreadsheet)
require 'rspreadsheet'
book = Rspreadsheet::Workbook.new
sheet = book.create_worksheet 'Top icecreams'
sheet[1,1] = 'Top icecreams'
p sheet[1,1].class # => String
p sheet[1,1] # => "Top icecreams"
# These are all the same - alternative syntax for VALUE of the cell.
p sheet[1,1] # => "Top icecreams"
p sheet.A1 # => "Top icecreams"
p sheet['A1'] # => "Top icecreams"
p sheet['1','1'] # => "Top icecreams"
p sheet['1','A'] # => "Top icecreams"
p sheet['A',1] # => "Top icecreams"
p sheet['A','1'] # => "Top icecreams"
# or if you want to get the Cell object for finer access
cell = sheet.cells(1,1) # => Rspreadsheet::Cell at A1
cell = sheet.cells('A1') # => Rspreadsheet::Cell at A1
cell = sheet.cells('A',1) # => Rspreadsheet::Cell at A1
cell = sheet.cells(1,'A') # => Rspreadsheet::Cell at A1
cell = sheet.rows(1).cells(1) # => Rspreadsheet::Cell at A1
cell.value # => "Top icecreams"
# How to manipulate the Cell object
cell.format
cell.format.font_size = '15pt'
cell.format.bold = true
p cell.format.bold? # => true
# How to set/get cell formating
cell.format.bold = true
p cell.format.bold? # => true
cell.format.italic = false
p cell.format.italic? # => false
cell.format.color = '#ff333'
p cell.format.color # => #ff333
cell.format.background_color = '#6666ff'
p cell.format.background_color # => #6666ff
cell.format.font_size = '15pt'
p cell.format.font_size # => 15pt
# Borders
cell.format.top.style = 'dotted'
cell.format.top.width = 0.5 # in pt's
cell.format.top.color = '#005500'
## or you can use other equivalent syntaxes as well
cell.format.border_top.style = 'dotted'
cell.border_top.style = 'dotted'
cell.format.borders[0].style = 'dotted' # not implemented yet
cell.borders[0].style = 'dotted' # not implemented yet
# There are the same assigmenents
value = 'My Icecream list'
sheet.A1 = value
sheet[1,1]= value
sheet.cells(1,1).value = value
p sheet.A1.class # => Rspreadsheet::Cell
# relative cells
sheet.cells(4,7).relative(-1,0) # => cell 3,7
# build the top five list (these features are not implemented yet)
(1..5).each { |i| sheet[i,1] = i }
sheet.columns(1).format.bold = true
sheet.cells[2,1..5] = ['Vanilla', 'Pistacia', 'Chocolate', 'Annanas', 'Strawbery']
sheet.columns(1).cells(1).format.color = :red
book.save('testfile.ods')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.