Tag: CVS的

如何确定在rails中使用csv和excel文件的第一行的内容

我有一个简单的应用程序,用户需要通过csv或Excel上传内容。 我并不总是想假设用户知道数据库中的所有必填字段,我只是希望用户知道第一列是必需的,无论放置在第一列的内容mobile_number进入数据库的mobile_number列。 我在说什么的代码示例如下。 我正在使用roogem。 这是一个简单的应用程序,所以只有两个数据库列在联系人表中使用。 def load_imported_contacts spreadsheet = open_spreadsheet header = spreadsheet.row(1) (2..spreadsheet.last_row).map do |i| row = Hash[[header, spreadsheet.row(i)].transpose] contact = Contact.find_by_id(row["id"]) || Contact.new contact.name = "content of the second field(optional)" contact.mobile_number = "content in the first(required and must be the first column)" contact.save! end end def open_spreadsheet case File.extname(file.original_filename) when ".csv" then Csv.new(file.path, […]