I'm using Ruby version 1.9.3 and Rails version 3.0.20 with the axlsx ruby gem to generate a file which has a rather complicated structure, involving cellmerging. The gem is great but I hit the wall when trying to merge more the 1 set of cells in one sheet.
Exp:
After calling:
sheet.merge_cells("A1:A2")
sheet.merge_cells("B1:B2")
Only the last 2 cells(b1, b2) get merged(a1, a2 stay unchanged).
Here is an example that merges both cell ranges using the latest 2.0.1 version of the axlsx gem. The important thing to understand here about axlsx is that it is a sparse generator. Only cells that you have specifically added to the worksheet can be merged, styled, edited etc. In your example you are only adding one row of cells, so only those cells can be merged.
If you add another row with
sheet.add_row [nil, nil, nil, nil]
I expect you will find the cells merged as you expect when you open excel.
https://github.com/randym/axlsx/
require 'axlsx'
package = Axlsx::Package.new
package.workbook do |workbook|
workbook.add_worksheet name: 'merged_cells' do |sheet|
4.times do
sheet.add_row %w(a b c d e f g)
end
sheet.merge_cells "A1:A2"
sheet.merge_cells "B1:B2"
end
end
package.serialize 'merged_cells.xlsx'
best
randym
This is a simple example of what I want to do:
Merge cells with coordinates "A1:A2" and "B1:2"
The code I use is as follows:
4.times do
sheet.add_row %w[a b c d e]
end
sheet.merge_cells "A1:A2"
sheet.merge_cells "B1:B2"
Here is an image of the result I get (on the left) and what I want to get as a result(on the right):
https://docs.google.com/file/d/0B9eWbU2sLebWdEIya1B5cUpya2c/edit?usp=sharing
As you can see only the last set of coordinates("B1:B2") get merged and the first ones("A1:A2") say unchanged. I've tried merging more then 2 sets of coordinates and the result merges every time only the last set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With