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 Width returned By GetColWidth #261

Open
teschste opened this issue Aug 9, 2018 · 1 comment
Open

Incorrect Column Width returned By GetColWidth #261

teschste opened this issue Aug 9, 2018 · 1 comment
Labels
confirmed This issue can be reproduced

Comments

@teschste
Copy link

teschste commented Aug 9, 2018

Description
I have found that if you open a workbook that was previously saved with excelize but has not yet been opened and re-saved with Excel, and then call GetColWidth on any column, it will report the column width as 64, no matter what the actual column width is.

Steps to reproduce the issue:

  1. Create an excelize file.
  2. Create a worksheet.
  3. Use SetColWidth to set column A to a specific width.
  4. Save the file.
  5. Open the workbook with excelize.
  6. Check the width of column A; the reported width will be 64.
  7. Check the width of column B (an undefined column); the reported width will be 64.
  8. Unzip the workbook.
  9. Edit the customWidth property of the col property for column A, setting it to "1" instead of "true".
  10. Rezip the workbook.
  11. Open the workbook with excelize.
  12. Check the width of column A; the reported width will now be correct.
  13. Check the width of column B (an undefined column); the reported width will still be 64.

Describe the results you received:
After comparing the unzipped version of a sheet created by excelize, both before and after re-saving the same workbook with Excel, I have found two problems:

  1. For a column that had no been specifically adressed during the file creating process, there is no col property to read, so some process is defaulting the width to 64, which is not the default column width in Excel.

  2. When excelize saves the column properties (say after calling SetColwidth), it sets the customWidth property to "true" but when Excel saves the same value, it sets it to "1".

For the second problem above, I have found that if I take a workbook that was created by excelize, unzip it, edit the column's customWidth property so it is set to "1" instead of "true", then zip the workbook up again, the problem is resolved andGetColWidth now reports the correct column width.

Describe the results you expected:
I expected the correct default value returned by GetColWidth for undefined columns.
I expected excelize to store properties in the same way that Excel does (i.e. customWidth property on defined columns set to "1" instead of "true).

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
@piaoger
Copy link

piaoger commented Sep 18, 2018

@teschste I met this issue and borrown a solution from sheetjs :) I called px2width before setting col width

// borrowed from SheetJS/js-xlsx
// https://github.com/SheetJS/js-xlsx/blob/master/bits/46_stycommon.js

// /* 18.3.1.13 width calculations */
// /* [MS-OI29500] 2.1.595 Column Width & Formatting */
// var DEF_MDW = 6, MAX_MDW = 15, MIN_MDW = 1, MDW = DEF_MDW;
// function width2px(width) { return Math.floor(( width + (Math.round(128/MDW))/256 )* MDW ); }
// function px2char(px) { return (Math.floor((px - 5)/MDW * 100 + 0.5))/100; }
// function char2width(chr) { return (Math.round((chr * MDW + 5)/MDW*256))/256; }

const (
	DEF_MDW = 6
	MAX_MDW = 15
	MIN_MDW = 1
	MDW     = DEF_MDW
)

func width2px(width float64) float64 {
	return math.Floor((width + (math.Round(128.0/MDW))/256.0) * MDW)
}

func px2char(px float64) float64 {
	return math.Floor(((px-5)/MDW*100 + 0.5) / 100.0)
}

func char2width(chr float64) float64 {
	return (math.Round((chr*MDW + 5) / MDW * 256.0)) / 256.0
}

func px2width(px float64) float64 {
	wch := px2char(px)
	width := char2width(wch)
	return width
}

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

3 participants