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

Question: Column Auto-Fit #118

Closed
petricf opened this issue Aug 11, 2017 · 1 comment
Closed

Question: Column Auto-Fit #118

petricf opened this issue Aug 11, 2017 · 1 comment
Assignees
Labels

Comments

@petricf
Copy link

petricf commented Aug 11, 2017

I've read in the FAQ that this is not available here. But a hint for a workaround was given:

It is possible to simulate "AutoFit" by tracking the width of the data in the column as your write it.

I don't know how evaluate the data width on strings or numbers i intend to write. How to map it to a column width for use (for example) in function worksheet_set_column ?

Is there some sample code available ?

@jmcnamara jmcnamara self-assigned this Aug 12, 2017
@jmcnamara
Copy link
Owner

jmcnamara commented Aug 12, 2017

I don't know how evaluate the data width on strings or numbers i intend to write. How to map it to a column width for use (for example) in function worksheet_set_column ?

From the docs for worksheet_set_column():

The width parameter sets the column width in the same units used by Excel which is: the number of characters in the default font. The default width is 8.43 in the default font of Calibri 11. The actual relationship between a string width and a column width in Excel is complex. See the following explanation of column widths from the Microsoft support documentation for more details.

What this means in practice is that in the default font the column width is approximately equal to the length of the string (unless the string is iiiiiiii or WWWWWWWW). To be sure you could multiply the strlen() by 1.1 or 1.2. You could build up a map of character widths to column widths for each letter and number but that probably isn't worth the effort unless you really need precision.

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