[GH-ISSUE #118] Question: Column Auto-Fit #98

Closed
opened 2026-05-05 11:40:07 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @petricf on GitHub (Aug 11, 2017).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/118

Originally assigned to: @jmcnamara on GitHub.

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 ?

Originally created by @petricf on GitHub (Aug 11, 2017). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/118 Originally assigned to: @jmcnamara on GitHub. 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 ?
gitea-mirror 2026-05-05 11:40:07 -06:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jmcnamara commented on GitHub (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.

<!-- gh-comment-id:321949939 --> @jmcnamara commented on GitHub (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()](http://libxlsxwriter.github.io/worksheet_8h.html#a9656e4e05d3787eee6b3e4d8e82d9b7f): >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](https://support.microsoft.com/en-us/help/214123/description-of-how-column-widths-are-determined-in-excel). 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 in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#98
No description provided.