-
Notifications
You must be signed in to change notification settings - Fork 750
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
fix: NumberFormatException if XSSFName.setNameName is set with a long name #55
Conversation
… name which consists of a letter followed by a lot of numbers.
Why are we attempting to parse the name as a cell address? Sounds like that is the core of the problem, and changing from Integer to BigInt doesn't address the core problem. |
The reason is described here (https://support.office.com/en-us/article/Define-and-use-names-in-formulas-4d0f13ac-53b7-422e-afd2-abd7ff379c64, Section "Learn about syntax rules for names"): Cell references disallowed: Names cannot be the same as a cell reference, such as Z$100 or R1C1. Thus, you have to validate that the given name is not a cell reference. |
|
||
//Name with very long name. Looks like a cell reference but this is not a reference. | ||
XSSFName nameSheet1 = wb.createName(); | ||
nameSheet1.setNameName("F04030020010"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense to adjust setNameName to catch exceptions from isRowWithinRange. isRowWithinRange seems like it likely to be used for me than just this use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean to change the code inside XSSFName.setNameName and not altering the test case?
Sorry for my question but your comment is just below the test case, so I'm a little bit confused.
How about private XSSFName.validateName swallows the NumberFormatException? This will means that public CellReference.cellReferenceIsWithinRange will continue to raise a NFE if it gets a silly input, letting the caller decide how to handle a non-integer row string. |
Thanks @onealj - that's basically what I was trying to suggest. |
Danke schön @Mille4Ever für die Pull-Anfrage. Thanks @pjfanning for pointing out the EXCEL97 error. I fixed that too and grep'd through src/ooxml to make sure there weren't any other oversights in XSSF. |
If you call XSSFName.setNameName with a long value consisting of a letter followed by a big number, you will get a NumberFormatException.
For example:
I want to set the name "F04030020010". In Excel using name box, I can set the name without any problems. If I want set the same name using poi, I will get the exception mentioned above.
The reason for the NumberFormatException:
The method XSSFName.validateName splits the value "F04030020010" in a column part and in a row part. Columns only have letters, rows only numbers. The outcome looks like:
Column = F
Row = 04030020010
In the next step, row will be converted into a number using Integer.parseInt. But the current row value exceeds the max value of an Integer resulting in a NumberFormatException.
Since the logic is fine, I replaced Integer.parseInt with BigDecimal, so there is no problem with parsing big numbers anymore.