Ruby Rails Excel 格式化数组操作
Ruby Rails Excel Format Array Manipulation
我需要一些关于如何在二维数组中动态定位值的 help/idea。
基本上,我有一个必须满足的条件,它决定了应该将值写入哪一列。
这就是我所拥有的(一个在写入值之前传递条件的循环):
@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
问题是:如果我想再做一个条件,并且两个(条件)之间的条件不成立,列值的位置设置为:client_records[i][2]
>> 2,3 etc ...是硬编码的,而不是第三个条件占据第二个条件列的位置,它自然留空并留在同一个地方。
这是它的样子:
而不是:
我该如何解决这个问题?
例如,尝试这样做:
@records.each do |client|
client_info = Report.get_client_info(client)
client_address = Report.get_client_address(client)
client_records = []
line = [client_info.id, client_info.full_name]
[client_address.count, client_contact.count].max.times do |i|
if @include_address == "1" && client_address[i]
line << client_address[i].full_address << client_address[i].address_type
else
line << '' << ''
end
if @@include_contact == "1" && client_contact[i]
line << client_contact[i].value << client_contact[i].label
else
line << '' << ''
end
client_records << line.clone
line = ['', '']
end
end
我需要一些关于如何在二维数组中动态定位值的 help/idea。
基本上,我有一个必须满足的条件,它决定了应该将值写入哪一列。
这就是我所拥有的(一个在写入值之前传递条件的循环):
@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
问题是:如果我想再做一个条件,并且两个(条件)之间的条件不成立,列值的位置设置为:client_records[i][2]
>> 2,3 etc ...是硬编码的,而不是第三个条件占据第二个条件列的位置,它自然留空并留在同一个地方。
这是它的样子:
而不是:
我该如何解决这个问题?
例如,尝试这样做:
@records.each do |client|
client_info = Report.get_client_info(client)
client_address = Report.get_client_address(client)
client_records = []
line = [client_info.id, client_info.full_name]
[client_address.count, client_contact.count].max.times do |i|
if @include_address == "1" && client_address[i]
line << client_address[i].full_address << client_address[i].address_type
else
line << '' << ''
end
if @@include_contact == "1" && client_contact[i]
line << client_contact[i].value << client_contact[i].label
else
line << '' << ''
end
client_records << line.clone
line = ['', '']
end
end