roo-rb / roo

Roo provides an interface to spreadsheets of several sorts.
MIT License
2.8k stars 502 forks source link

Sheet references are not stable while calling other API... either issue or docs issue #550

Open untoldone opened 4 years ago

untoldone commented 4 years ago

Thanks for filing an issue. Following these instructions will help us solve your problem sooner.

Steps to reproduce

require "roo"

xlsx = Roo::Excelx.new("/Users/untoldone/Downloads/UDS-2019-Full-Dataset.xlsx")
sheet1 = xlsx.sheet("HealthCenterSiteInfo")
sheet2 = xlsx.sheet("HealthCenterInfo")

puts sheet1.row(1)

In case this is file specific, the above UDS-2019-Full-Dataset.xlsx file can be found at https://www.hrsa.gov/sites/default/files/hrsa/foia/UDS-2019-Full-Dataset.xlsx

Issue

The above code outputs row 1 of the sheet titled "HealthCenterInfo". My expected behavior is that it would output the first row of the sheet titled "HealthCenterSiteInfo".

It is possible I misunderstood how the API was intended to behave and perhaps you can only have one sheet "open" at a time... but given the way the API is structured my expectation would be that a reference to a sheet does not change which sheet its pointing at once created. If this is the expected behavior, then I would describe this issue as a documentation issue as this behavior isn't clear given the current documentation.

System configuration

Roo version: 2.8.3

Ruby version: 2.6.6p146

wleeper commented 4 years ago

Seeing this issue as well. This is not expected behavior for ruby to change an objects reference like that. If this is the desired behavior of this method it should be changed to sheet! and the documentation updated to reflect what happens when you call this method.

anton-kachan-dev commented 3 years ago

@untoldone, It happens because the method .sheet changes the default sheet and returns the default sheet. And when you call it two times you will have the last sheet in two variables (sheet1 and sheet2). The behavior really looks like a bug. You can fix it fast by opening the document two times (create a two instance of a class):

require "roo"

xlsx1 = Roo::Excelx.new("/Users/untoldone/Downloads/UDS-2019-Full-Dataset.xlsx")
xlsx2 = Roo::Excelx.new("/Users/untoldone/Downloads/UDS-2019-Full-Dataset.xlsx")

sheet1 = xlsx1.sheet("HealthCenterSiteInfo")
sheet2 = xlsx2.sheet("HealthCenterInfo")

puts sheet1.row(1)