Skip to content

Instantly share code, notes, and snippets.

@randym
Created December 14, 2012 10:15
Show Gist options
  • Save randym/4284284 to your computer and use it in GitHub Desktop.
Save randym/4284284 to your computer and use it in GitHub Desktop.
Collapsed outline level
#```ruby
wb.add_worksheet(name: 'outline_level') do |sheet|
sheet.add_row [1, 2, 3, 4, Time.now, 149455.15]
sheet.add_row [1, 2, 5, 6, Time.now,14100.19]
sheet.add_row [9500002267, 1212, 1212, Time.now,14100.19]
sheet.add_row [], :collapsed => 1
sheet.rows[0..2].each do |row|
row.outline_level = 1
# This will collapse the outline level rows
row.hidden = true
end
sheet.column_info[0..2].each do |col|
col.outline_level = 1
#This will collapse the outline level cols
col.hidden = true
end
# This will show the show the [+] symbols
sheet.sheet_view do |view|
view.show_outline_symbols=true
end
end
#```
@pimpin
Copy link

pimpin commented Dec 14, 2012

Yeahh its perfect. I was trying collapsed http://rubydoc.info/github/randym/axlsx/Axlsx/Col#collapsed-instance_method. Thanks !

Notice that as I make loops over summary rows, I have to set outline and hidden to n lasts rows and it seems that sheet.rows[-n..0].each does not work. I had to do :

rows_data_array.each do |data|
  # ... building cells_values_array from data ...
  sheet.add_row cells_values_array
end
sheet.rows[(sheet.rows.size - rows_data_array.size)..sheet.rows.size].each do |row|
  row.outline_level = 1
  row.hidden = true
end

@randym
Copy link
Author

randym commented Dec 15, 2012

I'm thinking this could be made a bit easier by adding something to worksheet like:

def outline_rows(start_index, end_index, level = 1, collapsed = true

So Ive added two helper methods to worksheet.

You can do the same thing as you have above by calling

sheet.outline_rows (sheet.rows.size - rows_data_array.size), sheet.rows.size

It will enable the outline symbols and collapse the rows by default.
Ive added a similar method for outline_columns as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment