Class: Roo::Excelx::Sheet
- Inherits:
-
Object
- Object
- Roo::Excelx::Sheet
- Extended by:
- Forwardable
- Defined in:
- lib/roo/excelx/sheet.rb
Instance Method Summary collapse
- #cells ⇒ Object
- #column(col_number) ⇒ Object
- #comments ⇒ Object
- #dimensions ⇒ Object
-
#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).
- #excelx_format(key) ⇒ Object
-
#first_column ⇒ Object
returns the number of the first non-empty column.
-
#first_row ⇒ Object
returns the number of the first non-empty row.
- #hyperlinks ⇒ Object
-
#initialize(name, shared, sheet_index, options = {}) ⇒ Sheet
constructor
A new instance of Sheet.
-
#last_column ⇒ Object
returns the number of the last non-empty column.
- #last_row ⇒ Object
- #present_cells ⇒ Object
- #row(row_number) ⇒ Object
Constructor Details
#initialize(name, shared, sheet_index, options = {}) ⇒ Sheet
Returns a new instance of Sheet.
9 10 11 12 13 14 15 |
# File 'lib/roo/excelx/sheet.rb', line 9 def initialize(name, shared, sheet_index, = {}) @name = name @shared = shared @rels = Relationships.new(rels_files[sheet_index]) @comments = Comments.new(comments_files[sheet_index]) @sheet = SheetDoc.new(sheet_files[sheet_index], @rels, shared, ) end |
Instance Method Details
#cells ⇒ Object
17 18 19 |
# File 'lib/roo/excelx/sheet.rb', line 17 def cells @cells ||= @sheet.cells(@rels) end |
#column(col_number) ⇒ Object
46 47 48 49 50 |
# File 'lib/roo/excelx/sheet.rb', line 46 def column(col_number) first_row.upto(last_row).map do |row| cells[[row, col_number]] end.map { |cell| cell && cell.value } end |
#comments ⇒ Object
80 81 82 |
# File 'lib/roo/excelx/sheet.rb', line 80 def comments @comments.comments end |
#dimensions ⇒ Object
84 85 86 |
# File 'lib/roo/excelx/sheet.rb', line 84 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)
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/roo/excelx/sheet.rb', line 28 def each_row( = {}, &block) row_count = 0 [:offset] ||= 0 @sheet.each_row_streaming do |row| break if [:max_rows] && row_count == [:max_rows] + [:offset] + 1 if block_given? && !([:offset] && row_count < [:offset]) block.call(cells_for_row_element(row, )) end row_count += 1 end end |
#excelx_format(key) ⇒ Object
71 72 73 74 |
# File 'lib/roo/excelx/sheet.rb', line 71 def excelx_format(key) cell = cells[key] styles.style_format(cell.style).to_s if cell end |
#first_column ⇒ Object
returns the number of the first non-empty column
62 63 64 |
# File 'lib/roo/excelx/sheet.rb', line 62 def first_column @first_column ||= present_cells.keys.map { |_, col| col }.min end |
#first_row ⇒ Object
returns the number of the first non-empty row
53 54 55 |
# File 'lib/roo/excelx/sheet.rb', line 53 def first_row @first_row ||= present_cells.keys.map { |row, _| row }.min end |
#hyperlinks ⇒ Object
76 77 78 |
# File 'lib/roo/excelx/sheet.rb', line 76 def hyperlinks @hyperlinks ||= @sheet.hyperlinks(@rels) end |
#last_column ⇒ Object
returns the number of the last non-empty column
67 68 69 |
# File 'lib/roo/excelx/sheet.rb', line 67 def last_column @last_column ||= present_cells.keys.map { |_, col| col }.max end |
#last_row ⇒ Object
57 58 59 |
# File 'lib/roo/excelx/sheet.rb', line 57 def last_row @last_row ||= present_cells.keys.map { |row, _| row }.max end |
#present_cells ⇒ Object
21 22 23 |
# File 'lib/roo/excelx/sheet.rb', line 21 def present_cells @present_cells ||= cells.select { |_, cell| cell && !cell.empty? } end |
#row(row_number) ⇒ Object
40 41 42 43 44 |
# File 'lib/roo/excelx/sheet.rb', line 40 def row(row_number) first_column.upto(last_column).map do |col| cells[[row_number, col]] end.map { |cell| cell && cell.value } end |