excel - Validate number of columns in .xls file before upload in RAILS -


i uploading .xls file using carrierwave gem in rails. before uploading xls file, want validate .xls file:

1) contains amount of columns

2) first row contains headers.

i don't know begin. know validations must occur in model, , saw there gem validation csv, nothing xls. need pointed in correct direction.

you try roo gem

sheet = roo::excel.new("./excel_file.xls")  # header sheet.row(1)  # number of columns sheet.last_column 

http://railscasts.com/episodes/396-importing-csv-and-excel

for validation create method job:

def validate(sheet)   errors = []    header = sheet.row(1)   num_of_columns = sheet.last_column    errors << 'need headers' unless (array_of_needed_headers - header).empty?   errors << 'need more columns' if num_of_columns < number_of_columns    errors end 

you create own validator on model: http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -