Formatting Excel cell from number to text in rails -


i have made application on have provide feature import records csv , excel file. using roo gem it. record added problem @ time of importing records excel, adds .0 every field number. don't want because have fields enrollment_no, roll_no, contact_no , adds .0 every filed made 23 23.0. had converted these filed varchar in database , want format excel cell number text. solve problem. tell me how format excel cell number string using rails.

here code importing file:

student.rb :

def self.import(file, current_organization_id)   spreadsheet = open_spreadsheet(file)   header = spreadsheet.row(1)   (2..spreadsheet.last_row).each |i|     row = hash[[header, spreadsheet.row(i)].transpose]     record = student.find_by(:organization_id => current_organization_id,:enrollment_no => row["enrollment_no"]) || new     record.organization_id= current_organization_id     record.attributes = row.to_hash.slice(*row.to_hash.keys)     record.save!   end end   def self.open_spreadsheet(file)   case file.extname(file.original_filename)   when ".csv" roo::csv.new(file.path)   when ".xls" roo::excel.new(file.path)   when ".xlsx" roo::excelx.new(file.path)   else raise "unknown file type: #{file.original_filename}"   end end 

students_controller.rb :

def import     student.import(params[:file], session[:current_organization_id])     #puts @session[:current_organization_id].inspect     redirect_to students_path, notice: "record imported successfully."   end 

new.html.erb :

<%= form_tag import_students_path, multipart: true %>     <%= file_field_tag :file , :required=> true%> <br/>     <%= submit_tag "import" , :class => "btn btn-primary btn-block" %> <% end %>    

i doing similar in application import made easier importing csv.

it seems cell type pretty common problem in roo , there few workaround suggested using regex or char include in cell.

my solution easier:

# student.rb  columns_to_string = ["organization_id", "enrollment_no", "contact_no"] # , on   def self.import(file, current_organization_id)   spreadsheet = open_spreadsheet(file)   header = spreadsheet.row(1)   (2..spreadsheet.last_row).each |i|     row = hash[[header, spreadsheet.row(i)].transpose]     row = clean_for row, columns_to_string     record = student.find_by(:organization_id => current_organization_id,:enrollment_no => row["enrollment_no"]) || new     record.organization_id= current_organization_id     record.attributes = row.to_hash.slice(*row.to_hash.keys)     record.save!   end end  def self.clean_for row_as_hash, string_columns_array   row_as_hash.each |key, value|     if string_columns_array.include?key       row_as_hash[key] = value.to_i.to_s     end   end end  def self.open_spreadsheet(file)   case file.extname(file.original_filename)   when ".csv" roo::csv.new(file.path)   when ".xls" roo::excel.new(file.path)   when ".xlsx" roo::excelx.new(file.path)   else raise "unknown file type: #{file.original_filename}"   end end 
  • get index of columns want format differently
  • convert value imported float integer
  • convert integer string

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -