0

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?

6
  • 1
    Do 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. Commented 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. Commented 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. Commented 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. Commented 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. Commented Mar 9, 2015 at 2:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I hope you try this. I made a quick app using this in Rails. It took about 10 minutes. This will avoid any kind of importing/updating. The changes are reflected in the DB instantly. If you were wanting to copy and paste large groups of cells like in Excel, then this won't work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.