ruby roo puts column from parsing

715 Views Asked by At

I'm total newbie in programming...thanks in advance for all those who will answer me.. I'm trying to print the columns starting from a search. Actually my excel is composed like this:

|      | Header | Header | Header | Header |
|Header|Server 1|Server 2|Server 3|Server 4|
|Header|  Data  |  Data  |  Data  |  Data  |
|Header|  Data  |  Data  |  Data  |  Data  |
|Header|  Data  |  Data  |  Data  |  Data  |

This is my code but the output is not what I'm looking for....

fo_set_parse = xls.parse(:header_search => ['Server'], :clean => true)
fo_set_parse.each do |row|
row.each do |key,value| 
    if value != nil 
        puts "#{value}"
    end
  end
end

I'd like to print in the same excel style starting from a "Server" search..The number of "Server" change every time, so I'can't use something like

1.upto(xls.last_column) do |col|
   server1 = xls.cell(2,col)
   server2 = xls.cell(3,col)
   server3 = xls.cell(4,col)
   server4 = xls.cell(5,col)
   puts "#{server1}\t #{server2}\t #{server3}\t #{server4}\t"
end

Any help?

1

There are 1 best solutions below

0
Kache On BEST ANSWER

I'm not familiar with roo (and at a quick glance, it doesn't seem like there are many examples to pull from), but how about:

data_row_definitions = {
  :data_a => 2,                          # i.e. data_a is stored in row 2 for all servers
  :data_b => 3,
  :data_c => 4,
}
server_columns = 2.upto(xls.last_column) # or whatever columns the servers are listed

server_columns.map do |server_col|
  data_for_server = Hash[
    data_row_definitions.map do |data_name, row|
      cell_value = xls.cell(row, server_col)
      [data_name, cell_value]
    end
  ]
end

With a table like:

|       |Server 1|Server 2|Server N|
| dataA |       1|       3|       7|
| dataB |      10|      35|      14|
| dataC |     100|      95|      28|

(I would imagine) You would get the data structure:

[
  {
    "dataA" => 1,
    "dataB" => 10,
    "dataC" => 100,
  },
  {
    "dataA" => 3,
    "dataB" => 35,
    "dataC" => 95,
  },
  {
    "dataA" => 7,
    "dataB" => 14,
    "dataC" => 28,
  },
]

And should work for however many N servers/columns you have due to every sever/column being enumerated in the server_columns variable.