问题
I have an application I'm building that is basically just an online spreadsheet. I've been wondering how I should start building this. My primary tech stack is with Rails, but I can change depending on the job, though I'd prefer to keep at least the backend with Rails (and then maybe a backbone frontend?)
The application isn't exactly a spreadsheet though, instead of columns going by A,B,C like they do in Excel, users will be able to create names for columns. So for example they could create a column called Revenue and have the rows 1,2,3 have values of $1000, $2000, and $3000. Users will also be able to sort the columns ASC or DESC.
I thought about just having a Mysql table with say 30 fields where 15 are the values, and 15 are the corresponding column names. Except won't this be an incredibly inefficient method? Not to mention there would have to be a hard limit on how many columns a user could put in.
So is there a better method? Or the method I just described the best way.
回答1:
If "columns" are a core part of your problem domain then it may be useful to create a table to store them. Spreadsheets usually have tables, columns, rows, and cells with well defined relationships between them (cells belong to columns and rows, columns and rows belong to tables). That seems like a natural fit to a relational data store like MySql.
You could create a table for your column definitions which contains the index of each column, a foreign key to the table it belongs to, and the user-specified display name for that column.
回答2:
I would go with storing them as CSV files on your server or just having a table in MySQL with one additional column with the CSV text. Since it seems like the only data your users will be putting in their spreadsheet will be text, you will not have to worry about fields being too large.
回答3:
Using Rails and ActiveRecord, you could have the following models for your app:
class Table < ActiveRecord::Base #attributes - name:string
has_many :columns
end
class Column < ActiveRecord::Base #attributes - name:string, number:integer, table_id:integer
has_many :rows
belongs_to :table
end
class Row < ActiveRecord:: Base #attributes - number:integer, value:text, column_id: integer
belongs_to :column
end
So creating a column called 'Revenue' which is going to be the first column in an excel sheet will translate to something like this in ActiveRecord:
@table.columns.create(name: "Revenue", number: 1)
You could then add a row with a value of '$1000' to that column like this:
@table.columns.find(1).rows.create(value: "$1000")
Hope that helps!
来源:https://stackoverflow.com/questions/20748099/creating-a-spreadsheet