0

i am using axlsx for the first time and not sure what its capabilities are. I am trying to write my watir results to an excel sheet.I found axlsx to be interesting so i'm trying to use it. I am stuck at a point and not able to move forward.I hope i get help from here,

My scenario is i need a heading for each column in my excel sheet and from the 2nd row my results keep appending in a new row everytime, i have tried the below code so far,

     p = Axlsx::Package.new
     p.workbook.add_worksheet(:name => "firefox") do |sheet|
       sheet.add_row ["url", "projectname", "scenarioname","value","execution time"],:b=>true
       sheet.add_row ["#{$url}","#{dirname}","#{scenario_name}","Pass","#{Time.now}"]
     end
     p.serialize('../../Results'+day+'.xlsx')

Thanks in advance.

user3486694
  • 73
  • 1
  • 2
  • 7
  • It's pretty difficult to see what your problem is. The second statement appends another row. Can you share a good/bad example xlsx file? Can you be more specific? Do you really mean a new row, or a new worksheet? – noel Aug 05 '14 at 20:44
  • I dont have enough reputation points to add an excel.A new row is appended using the second statement but i want to put the second statement in a loop which keeps on adding new rows in the results file. – user3486694 Aug 06 '14 at 05:43

1 Answers1

0

I don't know what your process is, so this is rough code. If you need to loop through records, either loop inside add_worksheet:

p = Axlsx::Package.new
p.workbook.add_worksheet(:name => "firefox") do |sheet|
  sheet.add_row ["url", "projectname", "scenarioname","value","execution time"],:b=>true
  run_process = true
  while run_process
    # do something, set run_process = false when done
    sheet.add_row ["#{$url}","#{dirname}","#{scenario_name}","Pass","#{Time.now}"]
  end
end
p.serialize('../../Results'+day+'.xlsx')

Or, access the sheet outside:

p = Axlsx::Package.new
sheet = p.workbook.add_worksheet(:name => "firefox") do |sheet|
  sheet.add_row ["url", "projectname", "scenarioname","value","execution time"],:b=>true
end
while (run process that returns true)
  sheet.add_row ["#{$url}","#{dirname}","#{scenario_name}","Pass","#{Time.now}"]
end
p.serialize('../../Results'+day+'.xlsx')

Does that answer your problem?

noel
  • 2,095
  • 14
  • 14