我的目标是上传一些列并排除其他列。name
在这种情况下,我希望只上传age
和email
并排除其他。我已经能够使用slice
如下所示的方法来实现这一点:
def load_imported_members
spreadsheet = open_spreadsheet
spreadsheet.default_sheet = 'Worksheet'
header = spreadsheet.row(1)
(2..spreadsheet.last_row).map do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
member = Member.find_by_id(row["id"]) || Member.new
member.attributes = row.to_hash.slice("id", "name", "age", "email")
member
end
end
问题是last_row
考虑到最后一行 (13) 之前的所有行,并且由于表单上有验证,因此由于空行(不应考虑)导致数据丢失而导致错误。有没有办法像我一样只上传特定的列,但仅限于有数据的行?