Home » excel » ruby on rails – Column & row collapsing in xlsx file

ruby on rails – Column & row collapsing in xlsx file

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve been asked to create an Excel (.xlsx) report with collapsed rows and columns.

The application is in ruby/rails using axlsx gem for excel generation.

For the moment I cannot have the collaped symbol “+” facing the hidden row or column that allows to show/hide the rows/columns.

Anybody knows how can I create this collapsing effect? Even with another technology ?

Thanks,

Michael

How to&Answers:

This can be done with axlsx.

require 'axlsx'
xlsx = Axlsx::Package.new
wb = xlsx.workbook
wb.add_worksheet(name: 'outline') do |sheet|
  sheet.add_row [nil, nil, nil, nil, nil, nil, nil, Time.now, 149455.15]
  sheet.add_row [nil, nil, nil, nil, nil, nil, nil, Time.now,14100.19]
  sheet.add_row [9500002267,  'foo', 'bar', 'penut', nil, 1212, 1212, Time.now,14100.19]
  sheet.rows[0..2].each do |row|
    row.outline_level = 1
  end
  sheet.column_info[0..2].each do |col|
    col.outline_level = 1
  end
  # This is required to show the [+] symbols
  sheet.sheet_view do |view|
    view.show_outline_symbols=true
  end
end
xlsx.serialize 'outline.xlsx'

You need to specify the outline_level on the row or column_info you are interested in and set show_ouline_symobols to true.

Best

randym