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

Incorrect Column Widths Using SetColWidth and GetColWidth #260

Open
teschste opened this issue Aug 9, 2018 · 3 comments
Open

Incorrect Column Widths Using SetColWidth and GetColWidth #260

teschste opened this issue Aug 9, 2018 · 3 comments
Labels
confirmed This issue can be reproduced

Comments

@teschste
Copy link

teschste commented Aug 9, 2018

Description
When using SetColWidth, I have found that the actual column width seen when opening the file in Excel is 0.71 or 0.72 narrower than the width requested.
Conversely, I have found that GetColWidth reports the colum to be 0.71 or 0.72 wider than it really is.

I have heard it mentioned that this difference will vary depending on the user's printer selection but I have now tried this with at least 4 different printers and the results are exactly the same, regardless which printer I have selected.

Steps to reproduce the issue:

  1. Create an excelize file.
  2. Use SetColWidth to set column A to a specific width.
  3. Save the file.
  4. Open the workbook with Excel and check the width of column A.
  5. Save the Excel workbook (even if no changes have been made).
  6. Open the workbook using excelize.
  7. Use GetColWidth to get the width of column A.

Describe the results you received:
In step 4, the width of column A in Excel was 0.71 or 0.72 narrower than the requested width.
In step 7, the width returned for column A was 0.71 or 0.72 wider than the actual width.

Describe the results you expected:
I expected the column width I set in step 4 to match the the value I set using excelize.
I expected the column width returned in step 7 to match the actual column width.

Upon inspection of the unzipped workbooks at all stages, I have found that Excel stores the column width at a size 0.71 or 0.72 wider than the actual column width desired. I have also found that if View Gridlines is turned off, that difference changes to somewhere between 0.57 and 0.62. Again, the printer selected makes no difference.

Output of go version:

go1.10 windows/amd64

Excelize version or commit ID:

ec37b114c3b704a84c66fcf3e135c9df88ffb24d

Environment details (OS, Microsoft Excel™ version, physical, etc.):
Windows 7 Professional SP1 64-bit, Core i5-2400 @ 3.10GHz, with 8GB RAM
Excel 2007 12.0.6787.5000 SP3 MSO

@xuri xuri added the confirmed This issue can be reproduced label Aug 13, 2018
@AuroraTea
Copy link

AuroraTea commented Sep 8, 2022

I have also encountered this problem. But I'm getting different results on different devices...
_ = f.SetColWidth("Sheet1", "B", "B", 10)
1, 27 inch 2K - 10->9.22
2, 14 inch 1080P - 10->9.36
Both Microsoft Office Excel
for example 1:
image

the row height and column width is affected by different screen resolutions and can't be accurately calculated

@xuri I don't understand what you said in #1223. Why even set the width will be affected by the monitor resolution. So how do we get consistent operation in all environments. Or is it that the data is in fact certain and set correctly by Excelize, the problem is that Microsoft Office Excel itself for the display of this data.

@xuri
Copy link
Member

xuri commented Sep 8, 2022

We can't get a consistent unit in all environments, which same as the Excel app. The columns width and row height depend on display device resolution, as said in ECMA-376, ISO/IEC 29500 §18.3.1.13:

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.

width = Truncate([{Number of Characters} * {Maximum Digit Width} + {5 pixel
padding}]/{Maximum Digit Width}*256)/256

[Example: Using the Calibri font as an example, the maximum digit width of 11 point font
size is 7 pixels (at 96 dpi). In fact, each digit is the same width for this font. Therefore, if
the cell width is 8 characters wide, the value of this attribute must be
Truncate([8*7+5]/7*256)/256 = 8.7109375. end example]

To translate the value of width in the file into the column width value at runtime
(expressed in terms of pixels), use this calculation:

=Truncate(((256 * {width} + Truncate(128/{Maximum Digit Width}))/256)*{Maximum
Digit Width})

[Example: Using the same example as above, the calculation would be
Truncate(((256*8.7109375+Truncate(128/7))/256)*7) = 61 pixels. end example]

To translate from pixels to character width, use this calculation:
=Truncate(({pixels}-5)/{Maximum Digit Width} * 100+0.5)/100

[Example: Using the example above, the calculation would be Truncate((61-
5)/7*100+0.5)/100 = 8 characters. end example]

[Note: when wide borders are applied, part of the left/right border must overlap with the
2 pixel padding on each side. Wide borders do not affect the width calculation of the
column. end note]

[Note: When the sheet is in the mode to view formulas instead of values, the pixel width
of the column is doubled. end note]

The possible values for this attribute are defined by the W3C XML Schema double
datatype.

@TheSaltwaterRoom
Copy link

按着这个公式计算后,还是不行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed This issue can be reproduced
Projects
None yet
Development

No branches or pull requests

4 participants