I want to allow a user to add and edit data through an excel spreadsheets interface and have the database either mirror those spreadsheets, or automatically update daily. The user will not be allowed to edit the structure of the spreadsheets, just simply add or edit existing rows. Are there any gems that allow for this type of functionality?
-
1Do you mean you want to have a user actually access the DB through Excel? Or in a web page that looks like an Excel spreadsheet? i.e. just a raw table.Beartech– Beartech2015-03-09 01:36:22 +00:00Commented Mar 9, 2015 at 1:36
-
I want them to actually access it through excel (it can be a csv file or any other file editable via excel). The file would be in a specific location on a server that the user can access. It's an application for internal use, so I'm not worried about security.sabrams– sabrams2015-03-09 01:40:48 +00:00Commented Mar 9, 2015 at 1:40
-
Well, Excel can use a DB as a data source, but I don't know if you can actually use Excel to edit the data. I think you might only be able to view it. Why not just present the entire table as a web page where each cell is a form field filled in with the current value. The could then edit values and hit a save button.Beartech– Beartech2015-03-09 02:04:02 +00:00Commented Mar 9, 2015 at 2:04
-
What is the DB format? Or do you mean you want the Excel file to BE the data storage? Then you are actually asking for an Excel spreadsheet that can be viewed and edited by multiple people simultaneously. That is something that Excel doesn't really do, and for very good reasons.Beartech– Beartech2015-03-09 02:05:55 +00:00Commented Mar 9, 2015 at 2:05
-
The excel file will be a stand alone file. I would like the MySQL database to either mirror this file, or update daily by reading the file. I am dealing with a very small company that has specifically requested the ability to add and edit data via excel due to the company's non-tech savvyness. If they need to add columns or new tables, they would come to me as an administrator.sabrams– sabrams2015-03-09 02:11:40 +00:00Commented Mar 9, 2015 at 2:11
1 Answer
I'm leaving the Excel part out of this, as I don't think its even possible, let alone practical in the long run.
Create your table in your Rails app. You can use the standard view that is created by the scaffold command.
then install the best_in_place gem by adding it to your gem file
gem 'best_in_place'
In your application.js file add best_in_place and jquery.purr after jquery in your file:
//= require jquery
//= require jquery.purr
//= require best_in_place
Create a coffee script file for your table's controller (if you scaffolded it, there will already be one). In my example I'm calling it my_tables:
my_tables.js.coffee:
jQuery ->
$('.best_in_place').best_in_place()
That's about it for setup, now just modify your table to use the best_in_place class:
<h1>Listing my_tables</h1>
<table>
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
</tr>
</thead>
<tbody>
<% @my_tables.each do |my_table| %>
<tr>
<td><%= best_in_place my_table, :col1 %></td>
<td><%= best_in_place my_table, :col2 %></td>
<td><%= best_in_place my_table, :col3 %></td>
<td><%= best_in_place my_table, :col4 %></td>
<td><%= best_in_place my_table, :col5 %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New My table', new_my_table_path %>
The only difference is in the individual cells. Instead of the standard:
<td><%= my_table.col1 %></td>
We use the best_in_place method and pass it the object and a symbol of the column name instead of a dot method:
<td><%= best_in_place my_table, :col1 %></td>
Now if you refresh your view you'll see the same table as always. BUT click on any cell and it becomes editable. Change the value and hit ENTER. The new value stays. Reload the page and you'll see that it still has the new value you gave it.