excelize icon indicating copy to clipboard operation
excelize copied to clipboard

is there a way to auto fit columns?

Open OneOfOne opened this issue 8 years ago • 15 comments

^ title, is there a way to auto fit columns (auto width)?

If not, how to set the width on all cols to fit the content?

OneOfOne avatar Aug 02 '17 21:08 OneOfOne

Thanks for your issue. To auto fit columns width, we need to calculate the column width based on the calculated value of the column (so on the result of any formulae, we need to implement formula calculation engine first), and any additional characters added by format masks such as thousand separators, and set the width, bestFit(this property is an information property, we still need to provide the width for the column) and customWidth properties of the columns. That's an interesting job and I will consider adding support for this feature later. But now there is no way to set that. I'll certainly accept that patch if somebody did that.

xuri avatar Aug 03 '17 02:08 xuri

Hello,

Just want to know if there is still no solution ?

Thank's for your amazing job !

unkls avatar May 09 '18 09:05 unkls

@unkls I'm sorry, this feature has not yet been developed. There's a lot of things it could do with, but I'm not really able to commit more time to this project right now.

xuri avatar May 09 '18 10:05 xuri

I want to set auto col and height, but...,

imyuliz avatar Sep 25 '19 06:09 imyuliz

hope for this feature

imom0 avatar Feb 25 '20 13:02 imom0

Is this Feature ready yet? Or If nobody is working on it, let me know If I can help in anyway, @xuri

hinupurthakur avatar Sep 21 '20 10:09 hinupurthakur

Thank you for your hard work. Please let us know if this feature will be available anytime soon

BinodKafle avatar Feb 02 '21 13:02 BinodKafle

You can do something like this :

// Autofit all columns according to their text content
cols, err := f.GetCols(sheetName)
if err != nil {
        return err
}
for idx, col := range cols {
        largestWidth := 0
        for _, rowCell := range col {
                cellWidth := utf8.RuneCountInString(rowCell) + 2 // + 2 for margin
                if cellWidth > largestWidth {
                        largestWidth = cellWidth
                }
        }
        name, err := excelize.ColumnNumberToName(idx + 1)
        if err != nil {
                return err
        }
        f.SetColWidth(sheetName, name, name, float64(largestWidth))
}

Inspired from https://github.com/tealeg/xlsx/pull/618 It just works for string cells.

I'm not sure how it should be implemented into this library though. I did not find an effective way to get all cells for a column.

gnuletik avatar Apr 16 '21 21:04 gnuletik

We could take inspiration from the library SpreadsheetLight for .NET. I've used for a reasonable time and the auto fit feature works great..

https://github.com/jmsanmu/SpreadsheetLight/blob/fa9ab90bab19654868d100c0b1fee0ca613f7ffd/source/RowColumnFunctions.cs#L2492

mniak avatar May 17 '21 21:05 mniak

Any news on this?

Is there any way to do this currently?

eduardo-mior avatar Jan 23 '22 14:01 eduardo-mior

We could take inspiration from the library SpreadsheetLight for .NET. I've used for a reasonable time and the auto fit feature works great..

https://github.com/jmsanmu/SpreadsheetLight/blob/fa9ab90bab19654868d100c0b1fee0ca613f7ffd/source/RowColumnFunctions.cs#L2492

well, let me do some research on this C# code...

gucio321 avatar Sep 19 '22 06:09 gucio321

Also keep in mind it can become troublesome if we have different fonts in the same column. The same number of characters will occupy a different space depending on the font. 😉

qdm12 avatar Sep 27 '23 12:09 qdm12

Hello, When can this PR be merged into the main branch? I really need this feature

tangbo1430 avatar May 03 '24 16:05 tangbo1430

Hello, When can this PR be merged into the main branch? I really need this feature

All code review issues of PR #1386 should be resolved before merging it.

xuri avatar May 04 '24 12:05 xuri