class Spreet::Sheet

Attributes

columns[R]
current_row[RW]
document[R]
name[R]

Public Class Methods

new(document, name=nil) click to toggle source
# File lib/spreet/sheet.rb, line 7
def initialize(document, name=nil)
  @document = document
  self.name = name
  raise ArgumentError.new("Must be a Document") unless document.is_a? Document
  @current_row = 0
  @cells = {} # BigArray::Cells.new
  @bound = compute_bound
end

Public Instance Methods

[](*args) click to toggle source
# File lib/spreet/sheet.rb, line 35
def [](*args)
  coord = Coordinates.new(*args)
  @cells[coord.to_i] ||= Cell.new(self, coord)
  return @cells[coord.to_i]
end
[]=(*args) click to toggle source
# File lib/spreet/sheet.rb, line 41
def []=(*args)
  value = args.delete_at(-1)
  cell = self[*args]
  cell.value = value
  @updated = true
end
bound() click to toggle source
# File lib/spreet/sheet.rb, line 77
def bound
  if @updated
    compute_bound
  else
    @bound
  end
end
cell(*args) click to toggle source

Find or build cell

# File lib/spreet/sheet.rb, line 73
def cell(*args)
  return c
end
each_row() { |rows(j)| ... } click to toggle source
# File lib/spreet/sheet.rb, line 66
def each_row(&block)
  for j in 0..bound.y
    yield rows(j)
  end
end
move_higher(increment=1) click to toggle source

Moves the sheet higher in the list of sheets

# File lib/spreet/sheet.rb, line 97
def move_higher(increment=1)
  @document.sheets.move(self, increment)
end
move_lower(increment=1) click to toggle source

Moves the sheet lower in the list of sheets

# File lib/spreet/sheet.rb, line 102
def move_lower(increment=1)
  @document.sheets.move(self, -increment)
end
move_to(position) click to toggle source

Moves the sheet to an other position in the list of sheets

# File lib/spreet/sheet.rb, line 92
def move_to(position)
  @document.sheets.move_at(self, position)
end
name=(value) click to toggle source
# File lib/spreet/sheet.rb, line 16
def name=(value)
  unless value
    value = (@document.sheets.count > 0 ? @document.sheets[-1].name.succ : "Sheet 1")
  end 
  raise ArgumentError.new("Name of sheet must be given") if value.to_s.strip.size.zero?
  if @document.sheets[value]
    raise ArgumentError.new("Name of sheet must be unique")
  end
  @name = value
end
next_row(increment = 1) click to toggle source
# File lib/spreet/sheet.rb, line 27
def next_row(increment = 1)
  @current_row += increment
end
previous_row(increment = 1) click to toggle source
# File lib/spreet/sheet.rb, line 31
def previous_row(increment = 1)
  @current_row -= increment
end
remove!(coordinates) click to toggle source
# File lib/spreet/sheet.rb, line 85
def remove!(coordinates)
  raise ArgumentError.new("Must be a Coordinates") unless document.is_a?(Coordinates)
  @cells.delete(coordinates.to_i)
  @updated = true
end
row(*args) click to toggle source
# File lib/spreet/sheet.rb, line 48
def row(*args)
  options = {}
  options = args.delete_at(-1) if args[-1].is_a? Hash
  row = options[:row] || @current_row
  args.each_index do |index|
    self[index, row] = args[index]
  end
  next_row
end
rows(index) click to toggle source
# File lib/spreet/sheet.rb, line 58
def rows(index)
  row = []
  for i in 0..bound.x
    row[i] = self[i, index]
  end
  return row
end

Private Instance Methods

compute_bound() click to toggle source
# File lib/spreet/sheet.rb, line 108
def compute_bound
  bound = Coordinates.new(0,0)
  for index, cell in @cells
  # for cell in @cells.compact
    unless cell.empty?
      bound.x = cell.coordinates.x if cell.coordinates.x > bound.x
      bound.y = cell.coordinates.y if cell.coordinates.y > bound.y
    end
  end
  @updated = false
  @bound = bound
  return @bound
end