-
Notifications
You must be signed in to change notification settings - Fork 772
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
Bug: Auto sizing not working properly due to rounding error when calculating defaultCharWidth #541
base: trunk
Are you sure you want to change the base?
Conversation
I added some changes in 8819952 I didn't want to break backward compatibility so I've added new methods and deprecated the ones that they replace. It's hard to automate tests because the calculated widths depend on the font setup that testers have installed. If you can spot a mistake or could try out my change that would be great. I will double check the code change later and it is possible it might break a few tests. |
It will be a few weeks before POI 5.2.5 is released. We are concentrating on releasing XMLBeans 5.2.0 first and once that is released, we should be in a position to start a POI release vote. |
Alright, thank you for addressing the issue so quickly! |
@NiklasMelznerExxcellent Some users are complaining that this change is still creating cells that are not wide enough. Would you have any interest in having another look? I'm wondering whether it might be worth just arbitrarily increasing the width of each cell by some hardcoded width. Maybe we could make this configurable but default to some non-zero value. |
I guess you're referring to this bug? |
Was this fixed in Apache POI 5.3.0 which was released two days ago? |
Changes from this PR were applied as part of Apache POI 5.3.0, see r1913676, however some manual tests showed that the actual result is dependent on the application used to display the workbook, so columns may still be either too small, see https://bz.apache.org/bugzilla/show_bug.cgi?id=68094 for some details. |
With Apache POI 5.2.3 the auto sizing works properly with Excel, I'm using that version currently as newer ones (dis not try 5.3.0 yet) are broken. |
https://svn.apache.org/viewvc?view=rev&rev=1913676 was in POI 5.2.5. So far the only suggested solution is to have a configurable extra width that is added to all cell widths. There are too many variables to get a works in all cases solution (depending on what fonts are installed on the machine used to run the POI code and which spreadsheet tool is used to view the generated spreadsheet). |
I created #657 as an outline |
The XSSFSheet#autoSizeColumn method doesn't work properly for some specific cell values and font metrics.
The following code snippet is an example for this behavior:
Generated excel file:
The bug is caused by a rounding error in SheetUtil#getDefaultCharWidth:
poi/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
Lines 301 to 302 in 5533d35
Rounding is not necessary at all, removing the operation and converting the return type of getDefaultCharWidth() to double solves the issue.
The column width calculation that causes the error (and uses a value returned by SheetUtil#getDefaultCharWidth) occurs in SheetUtil#getCellWidth:
poi/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java
Line 244 in 5533d35