Class: Roo::Excelx::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/roo/excelx/sheet.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, rels_path, sheet_path, comments_path, styles, shared_strings, workbook, options = {}) ⇒ Sheet

Returns a new instance of Sheet.



4
5
6
7
8
9
10
# File 'lib/roo/excelx/sheet.rb', line 4

def initialize(name, rels_path, sheet_path, comments_path, styles, shared_strings, workbook, options = {})
  @name = name
  @rels = Relationships.new(rels_path)
  @comments = Comments.new(comments_path)
  @styles = styles
  @sheet = SheetDoc.new(sheet_path, @rels, @styles, shared_strings, workbook, options)
end

Instance Method Details

#cellsObject



12
13
14
# File 'lib/roo/excelx/sheet.rb', line 12

def cells
  @cells ||= @sheet.cells(@rels)
end

#column(col_number) ⇒ Object



41
42
43
44
45
# File 'lib/roo/excelx/sheet.rb', line 41

def column(col_number)
  first_row.upto(last_row).map do |row|
    cells[[row, col_number]]
  end.map { |cell| cell && cell.value }
end

#commentsObject



75
76
77
# File 'lib/roo/excelx/sheet.rb', line 75

def comments
  @comments.comments
end

#dimensionsObject



79
80
81
# File 'lib/roo/excelx/sheet.rb', line 79

def dimensions
  @sheet.dimensions
end

#each_row(options = {}, &block) ⇒ Object

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/roo/excelx/sheet.rb', line 23

def each_row(options = {}, &block)
  row_count = 0
  options[:offset] ||= 0
  @sheet.each_row_streaming do |row|
    break if options[:max_rows] && row_count == options[:max_rows] + options[:offset] + 1
    if block_given? && !(options[:offset] && row_count < options[:offset])
      block.call(cells_for_row_element(row, options))
    end
    row_count += 1
  end
end

#excelx_format(key) ⇒ Object



66
67
68
69
# File 'lib/roo/excelx/sheet.rb', line 66

def excelx_format(key)
  cell = cells[key]
  @styles.style_format(cell.style).to_s if cell
end

#first_columnObject

returns the number of the first non-empty column



57
58
59
# File 'lib/roo/excelx/sheet.rb', line 57

def first_column
  @first_column ||= present_cells.keys.map { |_, col| col }.min
end

#first_rowObject

returns the number of the first non-empty row



48
49
50
# File 'lib/roo/excelx/sheet.rb', line 48

def first_row
  @first_row ||= present_cells.keys.map { |row, _| row }.min
end


71
72
73
# File 'lib/roo/excelx/sheet.rb', line 71

def hyperlinks
  @hyperlinks ||= @sheet.hyperlinks(@rels)
end

#last_columnObject

returns the number of the last non-empty column



62
63
64
# File 'lib/roo/excelx/sheet.rb', line 62

def last_column
  @last_column ||= present_cells.keys.map { |_, col| col }.max
end

#last_rowObject



52
53
54
# File 'lib/roo/excelx/sheet.rb', line 52

def last_row
  @last_row ||= present_cells.keys.map { |row, _| row }.max
end

#present_cellsObject



16
17
18
# File 'lib/roo/excelx/sheet.rb', line 16

def present_cells
  @present_cells ||= cells.select { |_, cell| cell && cell.value }
end

#row(row_number) ⇒ Object



35
36
37
38
39
# File 'lib/roo/excelx/sheet.rb', line 35

def row(row_number)
  first_column.upto(last_column).map do |col|
    cells[[row_number, col]]
  end.map { |cell| cell && cell.value }
end