mercredi 1 août 2018

Ruby Rails Excel Format Array Manipulation

I need some help/idea on how to dynamically position values in a 2D array.

Basically, I have a condition that must be met and that determines which column should the value be written.

This is what I have (a loop that pass conditions before writing the value):

@records.each do |client|

  client_info  = Report.get_client_info(client)
  client_address = Report.get_client_address(client)

  client_records = []

  client_records << [client_info.id, client_info.full_name]

  if @include_address == "1"
    client_address.each_with_index do |address, i|
      if client_records[i].present?                                    
        client_records[i][2] = address.full_address               
        client_records[i][3] = address.address_type   
      else
        client_records << ["","", address.full_address, address.address_type]
      end             
    end
  end

  if @include_contact == "1"
    client_contact.each_with_index do |contact, i|
      if client_records[i].present?                                    
        client_records[i][4] = contact.value                         
        client_records[i][5] = contact.label
      else
        client_records << ["","","","", contact.value, contact.label]               
      end
    end
  end
end

Problem is: IF I want to make another condition, and the condition in between the two (conditions) is not true, the position of the column value set by: client_records[i][2] >> 2,3 etc... is hard coded, instead of the third condition occupying the place of the second condition's columns, it naturally leaves it blank and stays at the same place.

This is how it would look like:

enter image description here

Instead of:

enter image description here

How can I work around this?

Aucun commentaire:

Enregistrer un commentaire