Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set automatic column width to fit content? #347

Closed
isc30 opened this issue Sep 17, 2018 · 1 comment
Closed

How to set automatic column width to fit content? #347

isc30 opened this issue Sep 17, 2018 · 1 comment
Labels

Comments

@isc30
Copy link

isc30 commented Sep 17, 2018

I can't see any simple way of achieving this, I tried:

worksheet.column_properties(cell.column()).best_fit = true;

But it doesn't work

@tfussell
Copy link
Owner

I just took a look at the spec, and I'm afraid that this doesn't appear to be possible. That property is described as follows: 'Best fit' means that when numbers are typed into a cell contained in a 'best fit' column, the column width should automatically resize to display the number. If you want the column to fit arbitrary content, I think you'll need to find the number of characters in the longest cell in the column then set the column width to that number. It won't be perfect, but it should be close. Column width is defined as follows in the standard: Column width measured as the number of characters of the maximum digit width of the numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. There are 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines. Someone could probably write a helper method on the xlnt::worksheet that executes that logic, effectively like double-clicking on the divider between columns in Excel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants