Class: Roo::Excelx::Sheet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, shared, sheet_index, options = {}) ⇒ Sheet

Returns a new instance of Sheet.



11
12
13
14
15
16
17
18
19
# File 'lib/roo/excelx/sheet.rb', line 11

def initialize(name, shared, sheet_index, options = {})
  @name = name
  @shared = shared
  @sheet_index = sheet_index
  @images = Images.new(image_rels[sheet_index]).list
  @rels = Relationships.new(rels_files[sheet_index])
  @comments = Comments.new(comments_files[sheet_index])
  @sheet = SheetDoc.new(sheet_files[sheet_index], @rels, shared, options)
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



9
10
11
# File 'lib/roo/excelx/sheet.rb', line 9

def images
  @images
end

Instance Method Details

#cellsObject



21
22
23
# File 'lib/roo/excelx/sheet.rb', line 21

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

#column(col_number) ⇒ Object



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

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

#commentsObject



91
92
93
# File 'lib/roo/excelx/sheet.rb', line 91

def comments
  @comments.comments
end

#dimensionsObject



95
96
97
# File 'lib/roo/excelx/sheet.rb', line 95

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)



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/roo/excelx/sheet.rb', line 39

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



82
83
84
85
# File 'lib/roo/excelx/sheet.rb', line 82

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



73
74
75
# File 'lib/roo/excelx/sheet.rb', line 73

def first_column
  @first_column ||= first_last_row_col[:first_column]
end

#first_rowObject

returns the number of the first non-empty row



64
65
66
# File 'lib/roo/excelx/sheet.rb', line 64

def first_row
  @first_row ||= first_last_row_col[:first_row]
end


87
88
89
# File 'lib/roo/excelx/sheet.rb', line 87

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

#last_columnObject

returns the number of the last non-empty column



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

def last_column
  @last_column ||= first_last_row_col[:last_column]
end

#last_rowObject



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

def last_row
  @last_row ||= first_last_row_col[:last_row]
end

#present_cellsObject



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

def present_cells
  @present_cells ||= begin
    warn %{
[DEPRECATION] present_cells is deprecated. Alternate:
  with activesupport    => cells[key].presence
  without activesupport => cells[key]&.presence
    }
    cells.select { |_, cell| cell&.presence }
  end
end

#row(row_number) ⇒ Object



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

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