how to set the automatic width? #1473
Labels
No Label
DBF
Dates
Defined Names
Features
Formula
HTML
Images
Infrastructure
Integration
International
ODS
Operations
Performance
PivotTables
Pro
Protection
Read Bug
SSF
SYLK
Style
Write Bug
good first issue
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#1473
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hello, use it for the first time. I don't know how to set the col width. The content is hidden
You can set the width of a column doing this
I created a library that does it automatically and uses this xlsx library, check out json-as-xlsx
The file formats do not have an option to recalculate width on read. You can use VBA for it by calling the sheet
.Range("A:Z").EntireColumn.AutoFit
OR you can set thewch
property of the specific column array object to the desired number of characters as explained in the READMEPS: our Pro Basic build solves the problem by iterating through the cells in each row, deducing Excel's calculated width based on the Maximum Digit Width algorithm, then taking the largest width
@cjlhll
Please see my solution
you can try see it
https://dave-wind.github.io/excel/index.html
@chenlitchian i've edited your code for complete autofit with values (your versione) and headers
Creds: @chenlitchian + @dave-wind
This is esp useful when you are user
header
option that will change the order of your cols. (Please note this is typescript. Just remove the types from fun-args).I ended up using:
All great solutions, thanks. But value.length does not give the same column width as Excel's own auto-fit... is there any way around this? Is there something I'm missing?
Column widths are nontrivial: https://docs.sheetjs.com/#column-properties (read this first)
As an auto-fit example, try setting A1 to "iiiiiiiiii" (10 lowercase letter i) and setting B1 to "wwwww" (5 lowercase letter w), then adjust the widths. The 10-character string is actually smaller than the 5-character string!
There are a few other complicating factors, like conditional formatting and tables ("action at a distance"), and the auto-filter box.
One could also use the canvas method measureText() to get the width of a text string in px, see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/measureText.
For anyone whose looking for doing this for Array of Arrays (aoa), I'm sharing how I did it.
Not sure the check for
null
values is necessary but it was necessary in my case.@JonArnfred the big issue with
measureText
is that the system-wide Calibri font (present in the various TTFs that ship with Office) do not have the same metrics as the "Calibri (Body)" that Excel uses by default.@MohannadNaj within the
aoa.map
,arr
is an array so you can simplyforEach
:for those just passing a worksheet i found this to be the fasted way to get an autofit (using the lodash range function)
ref: https://github.com/adambisek/string-pixel-width
npm install string-pixel-width
Creds: @andreElrico , @sysoft
I re-changed for my way and share it in case someone needed it.
npm install string-pixel-width
Thanks for the helpful answers! I ended up using this solution:
More examples:
10ae7c9fec/tests/write.js (L21-L26)
How is this working? Just incrementing v.length by one?
File formats don't have a flag to recalculate widths on open, so any "automatic width" approach has to be calculated when generating the spreadsheet file.
The naive method of looking at string lengths is decent on average. For Calibri the degenerate cases involve "i" and "w": 6 "w" is wider than 8 "0" and 8 "0" is wider than 16 "i".
Since column widths are written before the string table, this will require a second workbook scan.
Open questions:
Given that the calculation can be done outside of the library, and there is no implementation advantage to integrating it in the library, should it be added?
If it is added, should this behavior be "opt-in" or "opt-out" (always calculate if no width is specified)?
If the behavior is opt-in, should this be implemented as a column property or relegated to an API function (
XLSX.utils.sheet_calc_col_width
)?I hope I can help :)