Excel Headers breaking the text onto separate lines using sheet.add_row

237 views
Skip to first unread message

Bharat

unread,
Nov 26, 2013, 6:33:34 PM11/26/13
to ax...@googlegroups.com
The following is an example to clarify my question:

I have an excel spreadsheet with a column labeled

Period (Monthly)

I store this in a ruby variable called hperiod as:

hperiod = "Period (Monthly)"

and simply print out the header array using the sheet.add_row function as shown below:

sheet.add_row [hperiod,..,..,..,]

this works fine.  But I have a requirement to break the words "Period" and "(Monthly)" on two separate lines as shown below:

Period
(Monthly)

I tried to use the standard line break in ruby in between Period and (Monthly) as shown below:

hperiod = "Period\n(Monthly)"

but that does not work.

I do this sort of thing all over my excel outputs so breaking this into two different rows will make my life very difficult.  Is there a way that the line break can occur between the two words within the SAME cell?

Thanks in advance for your time.

Bharat

Bharat

unread,
Nov 28, 2013, 4:15:35 PM11/28/13
to ax...@googlegroups.com
Looks like not much activity here :)  Anyway, I was able to figure this out.  For those who might need similar functionality.  Two things to note here:

1.  Instead of using '\n' in ruby which stands for carriage return and line feed (character 13 followed by character 10), you need to use '\r' which is line feed only (character 10).  Why?  Because Excel uses this for this purpose.

So hperiod = "Period\n(Monthly)"
should be
hperiod = "Period\r(Monthly)"
instead.

2.  Along with '\r', you need to set the wrap parameter to true as well in the custom style code that you develop.  Like so:

header          = s.add_style :b => true, :sz => 10, :alignment => {:horizontal => :left, :wrap_text => true}

I hope this helps.

Thanks.

Bharat
Reply all
Reply to author
Forward
0 new messages