fix: NumberFormatException if XSSFName.setNameName is set with a long name #55

Closed
wants to merge 1 commit into
from
@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.apache.poi.util.StringUtil.endsWithIgnoreCase;
+import java.math.BigDecimal;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -359,7 +360,7 @@ public static boolean isRowWithnRange(String rowStr, SpreadsheetVersion ssVersio
}
public static boolean isRowWithinRange(String rowStr, SpreadsheetVersion ssVersion) {
- int rowNum = Integer.parseInt(rowStr) - 1;
+ int rowNum = new BigDecimal(rowStr).intValue() - 1;
return 0 <= rowNum && rowNum <= ssVersion.getLastRowIndex();
}
@@ -26,6 +26,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.ss.usermodel.BaseTestNamedRange;
import org.apache.poi.ss.util.CellRangeAddress;
+import java.io.IOException;
+
/**
* @author Yegor Kozlov
*/
@@ -130,4 +132,22 @@ public void testSetNameName() throws Exception {
wb.close();
}
+
+ @Test
+ public void testSetNameNameLongName() throws IOException {
+ // Test that renaming named ranges doesn't break our new named range map
+ XSSFWorkbook wb = new XSSFWorkbook();
+ wb.createSheet("First Sheet");
+
+ //Name with very long name. Looks like a cell reference but this is not a reference.
+ XSSFName nameSheet1 = wb.createName();
+ nameSheet1.setNameName("F04030020010");
@pjfanning

pjfanning May 18, 2017

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.

@Mille4Ever

Mille4Ever May 19, 2017

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.

+ nameSheet1.setRefersToFormula("'First Sheet'!$A$1");
+ nameSheet1.setSheetIndex(0);
+
+ assertEquals(1, wb.getNames("F04030020010").size());
+ assertEquals(nameSheet1, wb.getName("F04030020010"));
+
+ wb.close();
+ }
}