class Roo::Excelx::Sheet

Public Class Methods

new(name, rels_path, sheet_path, comments_path, styles, shared_strings, workbook) click to toggle source
# File lib/roo/excelx.rb, line 146
def initialize(name, rels_path, sheet_path, comments_path, styles, shared_strings, workbook)
  @name = name
  @rels = Relationships.new(rels_path)
  @comments = Comments.new(comments_path)
  @styles = styles
  @sheet = SheetDoc.new(sheet_path, @rels, @styles, shared_strings, workbook)
end

Public Instance Methods

cells() click to toggle source
# File lib/roo/excelx.rb, line 154
def cells
  @cells ||= @sheet.cells(@rels)
end
column(col_number) click to toggle source
# File lib/roo/excelx.rb, line 180
def column(col_number)
  first_row.upto(last_row).map do |row|
    cells[[row,col_number]]
  end.map {|cell| cell && cell.value }
end
comments() click to toggle source
# File lib/roo/excelx.rb, line 213
def comments
  @comments.comments
end
dimensions() click to toggle source
# File lib/roo/excelx.rb, line 217
def dimensions
  @sheet.dimensions
end
each_row(options = {}, &block) click to toggle source

Yield each row as array of Excelx::Cell objects accepts options max_rows (int) (offset by 1 for header) and pad_cells (boolean)

# File lib/roo/excelx.rb, line 165
def each_row(options = {}, &block)
  row_count = 0
  @sheet.each_row_streaming do |row|
    break if options[:max_rows] && row_count == options[:max_rows] + 1
    block.call(cells_for_row_element(row, options)) if block_given?
    row_count += 1
  end
end
excelx_format(key) click to toggle source
# File lib/roo/excelx.rb, line 205
def excelx_format(key)
  @styles.style_format(cells[key].style).to_s
end
first_column(sheet=nil) click to toggle source

returns the number of the first non-empty column

# File lib/roo/excelx.rb, line 196
def first_column(sheet=nil)
  @first_column ||= present_cells.keys.map {|row, col| col }.min
end
first_row() click to toggle source

returns the number of the first non-empty row

# File lib/roo/excelx.rb, line 187
def first_row
  @first_row ||= present_cells.keys.map {|row, col| row }.min
end
last_column(sheet=nil) click to toggle source

returns the number of the last non-empty column

# File lib/roo/excelx.rb, line 201
def last_column(sheet=nil)
  @last_column ||= present_cells.keys.map {|row, col| col }.max
end
last_row() click to toggle source
# File lib/roo/excelx.rb, line 191
def last_row
  @last_row ||= present_cells.keys.map {|row, col| row }.max
end
present_cells() click to toggle source
# File lib/roo/excelx.rb, line 158
def present_cells
  @present_cells ||= cells.select {|key, cell| cell && cell.value }
end
row(row_number) click to toggle source
# File lib/roo/excelx.rb, line 174
def row(row_number)
  first_column.upto(last_column).map do |col|
    cells[[row_number,col]]
  end.map {|cell| cell && cell.value }
end

Private Instance Methods

cells_for_row_element(row_element, options = {}) click to toggle source

Take an xml row and return an array of Excelx::Cell objects optionally pad array to header width(assumed 1st row). takes option pad_cells (boolean) defaults false

# File lib/roo/excelx.rb, line 226
def cells_for_row_element(row_element, options = {})
  return [] unless row_element
  cell_col = 0
  cells = []
  @sheet.each_cell(row_element) do |cell|
    cells.concat(pad_cells(cell, cell_col)) if options[:pad_cells]
    cells << cell
    cell_col = cell.coordinate.column
  end
  cells
end
pad_cells(cell, last_column) click to toggle source
# File lib/roo/excelx.rb, line 238
def pad_cells(cell, last_column)
  pad = []
  (cell.coordinate.column - 1 - last_column).times { pad << nil }
  pad
end