-1
sheet.add_row['Heading', '', 'Value']
sheet.add_row['value1', 'value2', 'value3']

Say I have the above 2 lines of code to write data to a spreadsheet using axlsx gem. There are 3 columns for which I have added data as shown above. However I would like to merge row1:col1 and row1:col2 ('Heading' and '').

I have used this code and this works:

sheet.merge_cells "A1:B1"

However, I would like to merge the 2 cells in the current row dynamically without having to hardcode the cell positions like A1 & B1.

How can I achieve this?

Biju
  • 820
  • 1
  • 11
  • 34

1 Answers1

1

The answer to your question can be found in axlsx RubyDocs for merge_cells method

worksheet.merge_cells "C1:E1"
# you can also provide an array of cells to be merged
worksheet.merge_cells worksheet.rows.first.cells[(2..4)]
Maciej Majewski
  • 994
  • 11
  • 10
  • I am adding several hundreds of rows using `sheet.add_row[]` function. And based on some condition, I woud like columns A and B to be merged ONLY for the current row which is getting added by my most recent `add_row` call. How do I achieve that? `worksheet.rows.first.cells[(2..4)]` merges C1:E1 and not columns C till E of the current/most recent row in the spreadsheet. – Biju Nov 21 '18 at 06:02
  • `add_row` returns `Row` object, so you can use `cells`method to access cells you are interested it. For future please refer to [documentation](https://www.rubydoc.info/gems/axlsx/2.0.1/Axlsx/Worksheet#add_row-instance_method) – Maciej Majewski Nov 21 '18 at 09:13