axlsx icon indicating copy to clipboard operation
axlsx copied to clipboard

Bordered style on merged cells

Open kaluznyo opened this issue 13 years ago • 3 comments

If a apply a style on a merged cell, border is only apply on the first cell. My code :

cell_bordered = wb.styles.add_style(border: {style: :thin, color: '000000'}, sz: 12)
wb.add_worksheet(name: 'Test') do |sheet|
  sheet.merge_cells('A1:D1')
  sheet.add_row ['Test'], style: [cell_bordered] #or [cell_bordered]*4
end

And it's produce this : http://tinypic.com/r/2a8q9hl/5 I'm running with Ruby 2, and Office for mac OS 2011 14.3.8

kaluznyo avatar Nov 01 '13 08:11 kaluznyo

To make it work for me I made a couple small changes to your code.

    cell_bordered = wb.styles.add_style(border: {style: :thin, color: '000000'}, sz: 12)
    wb.add_worksheet(name: 'Test') do |sheet|
      sheet.merge_cells('A1:D1')
      sheet.add_row ['Test', "", "", ""], style: [cell_bordered, cell_bordered, cell_bordered, cell_bordered]
    end

I believe there are two things happening here. Firstly a cell has to be initialized at some point. If it is not then you cannot perform any actions on it later i.e. changing style, editing content. The second thing is you need to list the format for all cells. By initializing the three extra cells you want in the merge and placing a format on them all I get the output I think you were looking for . capture

ottodude125 avatar Nov 01 '13 17:11 ottodude125

The right was to do it is exactly as @ottodude125 specified.

The merge will collapse the cells and use whatever algorithm your renderer has for determining styles. In Excels's case it looks like it wants to pull the right border from the right-most cell, and only keeps the bottom border if all the cells in the merge have a bottom border. I expect the same behaviour for the top and left borders. That is to say, left most cell and all cells respectively.

As axlsx is sparse, you need to populate those cells and assign the proper style.

randym avatar Nov 02 '13 16:11 randym

Thank you for the explanation !

kaluznyo avatar Nov 04 '13 08:11 kaluznyo