Import shared Google spreadsheet in Rails

1.4k views Asked by At

I'm trying to import a shared (to everyone) Google spreadsheet into my database in Rails, using Roo. The following code:

url = 'https://docs.google.com/spreadsheets/d/1-4kCLEXRcIlJB1wK5muZWsIL1nz5NsEWXyLrbe_HJNQ/edit?usp=sharing'
xls = Roo::Spreadsheet.open(url)

gives me

undefined method `spreadsheet_by_key' for nil:NilClass" error message.

Could you suggest what am I doing wrong? What is the easiest way to import a shared Google spreadsheet in Rails?

2

There are 2 answers

0
mfazekas On

You are trying to import the edit url. Use the export xlsx link and try to import that using roo. And you might also need to specify the extension via extension option.

0
Serge On

Yes, worked like a charm:

url = 'https://docs.google.com/spreadsheets/d/MY_FILE_ID/export?format=xlsx'
xls = Roo::Spreadsheet.open(url, extension: :xlsx)

You were quite right about the "extension" option, it would not work without it.

Thank you a lot!