Class: Roo::Excelx::Sheet

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

Instance Method Summary collapse

Constructor Details

#initialize(name, rels_path, sheet_path, comments_path, styles, shared_strings, workbook) ⇒ Sheet

Returns a new instance of Sheet.



146
147
148
149
150
151
152
# 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

Instance Method Details

#cellsObject



154
155
156
# File 'lib/roo/excelx.rb', line 154

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

#column(col_number) ⇒ Object



180
181
182
183
184
# 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

#commentsObject



213
214
215
# File 'lib/roo/excelx.rb', line 213

def comments
  @comments.comments
end

#dimensionsObject



217
218
219
# File 'lib/roo/excelx.rb', line 217

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) and pad_cells (boolean)



165
166
167
168
169
170
171
172
# 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) ⇒ Object



205
206
207
# File 'lib/roo/excelx.rb', line 205

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

#first_column(sheet = nil) ⇒ Object

returns the number of the first non-empty column



196
197
198
# File 'lib/roo/excelx.rb', line 196

def first_column(sheet=nil)
  @first_column ||= present_cells.keys.map {|row, col| col }.min
end

#first_rowObject

returns the number of the first non-empty row



187
188
189
# File 'lib/roo/excelx.rb', line 187

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


209
210
211
# File 'lib/roo/excelx.rb', line 209

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

#last_column(sheet = nil) ⇒ Object

returns the number of the last non-empty column



201
202
203
# File 'lib/roo/excelx.rb', line 201

def last_column(sheet=nil)
  @last_column ||= present_cells.keys.map {|row, col| col }.max
end

#last_rowObject



191
192
193
# File 'lib/roo/excelx.rb', line 191

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

#present_cellsObject



158
159
160
# File 'lib/roo/excelx.rb', line 158

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

#row(row_number) ⇒ Object



174
175
176
177
178
# 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