Skip to content

Commit

Permalink
Merge pull request #214 from ignat-zakrevsky/offset-feature-for-excelx
Browse files Browse the repository at this point in the history
Offset option for excelx #each_row
  • Loading branch information
simonoff committed May 7, 2015
2 parents 38d708d + 8bb5517 commit d9daa4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ def present_cells
end

# Yield each row as array of Excelx::Cell objects
# accepts options max_rows (int) (offset by 1 for header)
# and pad_cells (boolean)
# accepts options max_rows (int) (offset by 1 for header),
# pad_cells (boolean) and offset (int)
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] + 1
block.call(cells_for_row_element(row, options)) if block_given?
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
Expand Down
31 changes: 29 additions & 2 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
expect(Roo::Excelx.new(path, cell_max: 100)).to be_a(Roo::Excelx)
end
end

context 'file path is a Pathname' do
let(:path) { Pathname.new('test/files/file_item_error.xlsx') }

it 'creates an instance' do
expect(subject).to be_a(Roo::Excelx)
end
end

end

describe '#cell' do
Expand Down Expand Up @@ -420,5 +420,32 @@
expect(index).to eq 4
end
end

context 'with offset option' do
let(:offset) { 3 }

it 'returns the expected result' do
index = 0
subject.each_row_streaming(offset: offset) do |row|
expect(row.map(&:value)).to eq expected_rows[index + offset]
index += 1
end
expect(index).to eq 11
end
end

context 'with offset and max_rows options' do
let(:offset) { 3 }
let(:max_rows) { 3 }

it 'returns the expected result' do
index = 0
subject.each_row_streaming(offset: offset, max_rows: max_rows) do |row|
expect(row.map(&:value)).to eq expected_rows[index + offset]
index += 1
end
expect(index).to eq 4
end
end
end
end

0 comments on commit d9daa4e

Please sign in to comment.