database - How do I populate a table from an Excel Spreadsheet in Rails? -
I have a simple 4-column Excel spreadsheet that matches the ID code for the purposes of the university Is large (300k).
I need to come up with a way to convert this data into a table that is populated in my Rail app. It is a document that has been updated now and later, therefore It can not be solved just once. Ideally, this will be some kind of ruby script, which can read the file and automatically create entries, so that when we email a new version, we can update it automatically. I'm on everybody if it matters at all.
How can I do something like this?
If you can, save the spreadsheet as a CSV, to parse the Excel spreadsheet. In comparison, there are very good gems to parse CSV files. I have found an effective way to deal with this problem, which reads a sequi file and records all records properly. For example, how to read all the lines here from the old, but still effective file using the gem
data = Faster CSV. Block ('lib / tasks / data.csv') column = data. Remove (0) unique_column_index = -1 # index is a column that is always unique in the spreadsheet data line per line. R = records.find_orimize_bie_inic_column (line [unique_col_andex]) columnss.each_with_index. Index, column_name | R [column_name] = line [index] end r.save! Rescue = & gt; E-Rails.logger.error ("# {r.inspect} failed to save")
It relies on a little that you have a unique column in the original spreadsheet, though it Have to leave
If you put it in a rack task, then you can wire it in the Captivates deployment script, so every time you deploy it will be run. Find_or_initialize should ensure that you do not get duplicate records.
Comments
Post a Comment