Column.AdjustToContents() multiline support

Jan 19, 2012 at 6:33 PM

Any possibility of extending multiline support in Row.AdjustToContents() to Column.AdjustToContents()?  If I have more than a single line of text (newline separated, not wrapped) in a cell, Column.AdjustToContents() includes all lines of text in the calculation?  I'd like to see it calculate each line of text separately (based on the newline character) and adjust accordingly.  Is that possible?

Coordinator
Jan 21, 2012 at 11:11 PM

It already does that. In the following example columns A and B adjust to their second line.

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Sheet1");

            ws.Cell("a1").RichText
                .AddText("Hello World").AddNewLine()
                .AddText("This is just a text, nothing more.").AddNewLine()
                .AddText("Bueller?");

            ws.Cell("b1").Value =
                "Hello World" + Environment.NewLine +
                "This is just a text, nothing more." + Environment.NewLine +
                "Bueller?";
            
            ws.Rows().Style.Alignment.SetWrapText();
            ws.Columns().AdjustToContents();

Sep 17, 2012 at 3:14 PM

But is it possible to use AdjustToContents for text without new lines, but with word wrap enabled on cell?

Coordinator
Sep 17, 2012 at 3:32 PM

What you're asking doesn't make sense.

Adjust to contents = Make the column wide enough for the longest text.

Word wrap = Make the row tall enough to show the contents of the cell at the given column width.

Sep 17, 2012 at 5:06 PM

Hm, strange.

First, for me, word wrap != row height. I mean its when words moved to another line if there is no more space on current line (yeah, given column width). So i was thinking if you set word wrap - then text will not continue beyond edges of column, but if correct height is not set, then part of text (moved to other lines) will not be visible (beyond bottom border of cell).

Second, i was trying closed xml few month ago, and even with word wrap - row height (1 line of text) was same for all rows disregard of amount of lines.

But today i tried last build, and now rows height correctly calculates for multiline columns (with word wrap). Something was changed/fixed in last few months?=) Or it was some problem in my code? As i understand its something related to Format->AutoFit feature in excel? Its not calculated in code, but calculated by excel on document opening? No?

If it was just problem on my side  - then sorry, ignore my comments)

 

P.S.

You have nice lib =)

Coordinator
Sep 17, 2012 at 5:13 PM

Row height, column width, word wrap, and autofit (adjust to contents) are all interconnected.

For example, setting word wrap but not the row height will cause the row height to be as high as it needs to display the text (at the given column width). If you set the row height then word wrap won't affect the row height (it's fixed).

Play with it in Excel and you'll see the pattern more clearly. ClosedXML behaves just like setting things up in Excel. If it doesn't then please let me know.

Manuel