my spreadsheets contain many instances where column of numbers has some instances where cells that have no entry.
this is expected - and I would like my field to be assigned a null. instead, your api converts them to "0".
this is wrong. there are entries in the column which have a valid "0" and yet, I can no longer differentiate between those and the that are null but converted to 0.
entering the word "null" into the spreadsheet cell also results in an Integer having value 0.
we cannot get around this with a formatter.
the solution is as follows:
create a new fieldMapping called "Nullify"
where for each field, users have the ability to define what value in that column would be assigned the value of null. in this way, user could specify that all instances of "" (empty string) be assigned the value of null. OR user could specify text such as "null" that if in that column there were the word 'null' that the value assigned would be null.
then please open CSVParser.java
go to the method:
private Object getMappedBean(final List<String> csvLine, final CSVBeanMapping beanMap)
and add code similar to this:
if (! fieldMapping.getNullifyName().equals("none")) {
formattedFieldValue = getMappedField(csvLine, fieldMapping);
if(fieldMapping.getNullifyReference().compareTo(formattedFieldValue.toString())) {
continue;
}
this works - and am using similar logic in my project;
basically, we find that if the cell matches our definition of "null", then we pass over and don't call:
BeanUtils.setProperty(bean, fieldMapping.getAttributeName(), formattedFieldValue);
the only 'problem' that arises is if the user's class definition used an int (not Integer) - and since you can't assign a null to an int - it defaults to being initialized with 0.
such that with native primitives, the new fieldMapping would not apply - it would work only with java primitives.
but this is a small price to pay - because in my spreadsheet, there is huge difference in meaning between 0 and empty string/null. And presently, the api translates empty string/null into 0.
Thanks! George
Logged In: NO
just to clarify, the enhancement I listed (above) was with regard to reading "" values from a spreadsheet. this same idea would apply when writing the spreadsheet - since if your class contained a java Integer that was null, perhaps you would like the word "null" or "" or "some other text" inserted to the cell of that specific column when writing the spreadsheet.
what is the default behavior when writing a spreadsheet using a class with a java Primitive that is null? I have not yet tested the writing aspect - just the reading.
Cheers. George