Skip to content

Commit

Permalink
Allows sheet_for to accept an integer (#455)
Browse files Browse the repository at this point in the history
* Allows sheet_for and default_sheet= to accept int

sheet_for and default_sheet= can now accept the sheet as integer and as
a string.

* Adds spec for sheet_for
  • Loading branch information
tgturner authored and chopraanmol1 committed Sep 21, 2018
1 parent bd0b0f8 commit bfcfba4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/roo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def default_sheet
end

# sets the working sheet in the document
# 'sheet' can be a number (1 = first sheet) or the name of a sheet.
# 'sheet' can be a number (0 = first sheet) or the name of a sheet.
def default_sheet=(sheet)
validate_sheet!(sheet)
@default_sheet = sheet
@default_sheet = sheet.is_a?(String) ? sheet : sheets[sheet]
@first_row[sheet] = @last_row[sheet] = @first_column[sheet] = @last_column[sheet] = nil
@cells_read[sheet] = false
end
Expand Down Expand Up @@ -569,7 +569,7 @@ def validate_sheet!(sheet)
when nil
fail ArgumentError, "Error: sheet 'nil' not valid"
when Integer
sheets.fetch(sheet - 1) do
sheets.fetch(sheet) do
fail RangeError, "sheet index #{sheet} not found"
end
when String
Expand Down
2 changes: 1 addition & 1 deletion lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def sheets
def sheet_for(sheet)
sheet ||= default_sheet
validate_sheet!(sheet)
@sheets_by_name[sheet]
@sheets_by_name[sheet] || @sheets[sheet]
end

def images(sheet = nil)
Expand Down
43 changes: 40 additions & 3 deletions spec/lib/roo/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,22 @@ def sheets
end
end

describe '#row' do
it 'should return the specified row' do
describe "#row" do
it "should return the specified row" do
expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', 'forty-three', 'forty-four', 'forty-five', nil, nil])
expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
end

it "should return the specified row if default_sheet is set by a string" do
spreadsheet.default_sheet = "my_sheet"
expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
end

it "should return the specified row if default_sheet is set by an integer" do
spreadsheet.default_sheet = 0
expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
end
end

Expand Down Expand Up @@ -173,6 +185,31 @@ def sheets
end
end

describe "#default_sheet=" do
it "should correctly set the default sheet if passed a string" do
spreadsheet.default_sheet = "my_sheet"
expect(spreadsheet.default_sheet).to eq("my_sheet")
end

it "should correctly set the default sheet if passed an integer" do
spreadsheet.default_sheet = 0
expect(spreadsheet.default_sheet).to eq("my_sheet")
end

it "should correctly set the default sheet if passed an integer for the second sheet" do
spreadsheet.default_sheet = 1
expect(spreadsheet.default_sheet).to eq("blank sheet")
end

it "should raise an error if passed a sheet that does not exist as an integer" do
expect { spreadsheet.default_sheet = 10 }.to raise_error RangeError
end

it "should raise an error if passed a sheet that does not exist as a string" do
expect { spreadsheet.default_sheet = "does_not_exist" }.to raise_error RangeError
end
end

describe '#to_yaml' do
it 'should convert the spreadsheet to yaml' do
expect(spreadsheet.to_yaml({}, 5, 1, 5, 1)).to eq("--- \n" + yaml_entry(5, 1, 'date', '1961-11-21'))
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/roo/excelx_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@
it 'returns the expected result' do
expect(subject.sheet_for("Tabelle1").instance_variable_get("@name")).to eq "Tabelle1"
end

it 'returns the expected result when passed a number' do
expect(subject.sheet_for(0).instance_variable_get("@name")).to eq "Tabelle1"
end

it 'returns the expected result when passed a number that is not the first sheet' do
expect(subject.sheet_for(1).instance_variable_get("@name")).to eq "Name of Sheet 2"
end

it "should raise an error if passed a sheet that does not exist as an integer" do
expect { subject.sheet_for(10) }.to raise_error RangeError
end

it "should raise an error if passed a sheet that does not exist as a string" do
expect { subject.sheet_for("does_not_exist") }.to raise_error RangeError
end
end

describe '#row' do
Expand Down

0 comments on commit bfcfba4

Please sign in to comment.