Pavani Akula
Pavani Akula

Reputation: 15

How to add a column to an excel in rails using Axlsx gem?

I have few column names defined in my choices and I am adding it to the sheet as a first row but, one of the column should not be present all the time, it should vary conditionally, so I want to add a specific column based on some condition.

Here is my code sheet.add_row choices, style: style_shout

NOW, How should I add a column to the sheet?

Upvotes: 0

Views: 1160

Answers (1)

Gurmukh Singh
Gurmukh Singh

Reputation: 2017

try something like this:

adding first row with column names, then second row with column values

sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4', 'col5']
sheet.add_row [1, 2, 0.3, 4, 5.0]

and depending on the conditions you want you can modify as required, for example:

  #Hide the 5th column
  sheet.column_info[4].hidden = true

you could encapsulate this within a condition to hide or display a column

Upvotes: 0

Related Questions