module RubyXL::LegacyWorksheet

Public Class Methods

new(params = {}) click to toggle source
Calls superclass method
# File lib/rubyXL/worksheet.rb, line 5
def initialize(params = {})
  super
  self.workbook   = params[:workbook]
  self.sheet_name = params[:sheet_name]
  self.sheet_id   = params[:sheet_id]
  self.sheet_data = RubyXL::SheetData.new
  self.cols = RubyXL::ColumnRanges.new
  @comments = [] # Do not optimize! These are arrays, so they will share the pointer!
  @printer_settings = []
  @generic_storage = []
end

Public Instance Methods

[](row = 0) click to toggle source

allows for easier access to sheet_data

# File lib/rubyXL/worksheet.rb, line 18
def [](row = 0)
  sheet_data[row]
end
add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrite = true) click to toggle source
# File lib/rubyXL/worksheet.rb, line 32
def add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrite = true)
  validate_workbook
  validate_nonnegative(row_index)
  validate_nonnegative(column_index)
  row = sheet_data.rows[row_index] || add_row(row_index)

  c = row.cells[column_index]

  if overwrite || c.nil?
    c = RubyXL::Cell.new
    c.worksheet = self
    c.row = row_index
    c.column = column_index

    if formula then
      c.formula = RubyXL::Formula.new(:expression => formula)
      c.raw_value = data
    else
      case data
      when Numeric          then c.raw_value = data
      when String           then
        c.raw_value = data
        c.datatype = RubyXL::DataType::RAW_STRING
      when RubyXL::RichText then
        c.is = data
        c.datatype = RubyXL::DataType::INLINE_STRING
      when NilClass         then nil
      end
    end

    range = cols && cols.locate_range(column_index)
    c.style_index = row.style_index || (range && range.style_index) || 0
    row.cells[column_index] = c
  end

  c
end
add_row(row_index = 0, params = {}) click to toggle source
# File lib/rubyXL/worksheet.rb, line 26
def add_row(row_index = 0, params = {})
  new_row = RubyXL::Row.new(params)
  new_row.worksheet = self
  sheet_data.rows[row_index] = new_row
end
each() { |row| ... } click to toggle source
# File lib/rubyXL/worksheet.rb, line 22
def each
  sheet_data.rows.each { |row| yield(row) }
end

Private Instance Methods

ensure_cell_exists(row_index, column_index = 0) click to toggle source

Ensures that storage space for a cell with row_index and column_index exists in sheet_data arrays, growing them up if necessary.

# File lib/rubyXL/worksheet.rb, line 83
def ensure_cell_exists(row_index, column_index = 0)
  validate_nonnegative(row_index)
  validate_nonnegative(column_index)

  sheet_data.rows[row_index] || add_row(row_index)
end
get_col_xf(column_index) click to toggle source
# File lib/rubyXL/worksheet.rb, line 90
def get_col_xf(column_index)
  @workbook.cell_xfs[get_col_style(column_index)]
end
get_row_xf(row) click to toggle source
# File lib/rubyXL/worksheet.rb, line 94
def get_row_xf(row)
  validate_nonnegative(row)
  @workbook.cell_xfs[get_row_style(row)]
end
validate_nonnegative(row_or_col) click to toggle source
# File lib/rubyXL/worksheet.rb, line 99
def validate_nonnegative(row_or_col)
  raise 'Row and Column arguments must be nonnegative' if row_or_col < 0
end
validate_workbook() click to toggle source

validates Workbook, ensures that this worksheet is in @workbook

# File lib/rubyXL/worksheet.rb, line 73
def validate_workbook()
  unless @workbook.nil? || @workbook.worksheets.nil?
    return if @workbook.worksheets.any? { |sheet| sheet.equal?(self) }
  end

  raise "This worksheet #{self} is not in workbook #{@workbook}"
end