axlsx
axlsx copied to clipboard
Wrapping does not work when cells are merged and cell width is set
Text wrapping does not work if I merge the cells for which wrapping is turned on.
I have a scenario where I merge several cells horizontally because the text is rather large. However, I also need to wrap because the merge alone will not display all the text. But text wrapping does not work if I merge the cells. It works if I do not merge, but in that case, the volume of the text will cause the vertical size of the cell to be too large.
axlsx version: v2.01 Excel Version: Microsoft Office 365, version 1810
Here's some code to re-create the issue:
msg1 = "This is a really long line of text. "
msg2 = "I am going to add it to the cell A1."
msg3 = "I will then add empty string to B1 and C1. "
msg4 = "Finally, I will merge A1:C1. "
msg5 = "Notice that the text does not wrap even though the style specifies wrapping."
full_msg = msg1 + msg2 + msg3 + msg4 + msg5
p = Axlsx::Package.new
p.use_shared_strings = true
wb = p.workbook
wrap_text = wb.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}} )
wb.add_worksheet(:name => "WrapMerge") do | sheet |
sheet.add_row [full_msg, "", ""], :style => [wrap_text, wrap_text, wrap_text]
sheet.merge_cells("A1:C1")
sheet.column_widths(15.0,15.0,15.0)
end
p.serialize 'wrap_merge.xlsx'
I also wanted to add that if I don't set the cell width, then the cells expand horizontally to display the text. However, in that case, just three cells can take up the entire width of the displayed portion of the spreadsheet. That's not what I am looking for.
Attaching the spreadsheet generated with the code pasted above: wrap_merge.xlsx