diff options
author | Dominik Stadler <centic@apache.org> | 2019-06-22 06:12:19 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2019-06-22 06:12:19 +0000 |
commit | 9cde034ab97f4fc512682f3320300d9b55f48095 (patch) | |
tree | 651e820aae314da5fe674db13fdd1122a860f665 /src | |
parent | 16779b96e54ce1b8f8e770979278728b0cb52167 (diff) | |
download | poi-9cde034ab97f4fc512682f3320300d9b55f48095.tar.gz poi-9cde034ab97f4fc512682f3320300d9b55f48095.zip |
Bug 62906 and 63401: Ensure tables have an initial name which does not conflict with
existing names
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1861819 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
3 files changed, 51 insertions, 4 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index f897d80547..f23e31c477 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -4097,6 +4097,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Creates a new Table, and associates it with this Sheet. + * <p> + * The table is assigned a default display name (since 4.1.1) which can be overridden + * by calling {@code setDisplayName}. The default display name is guaranteed to not conflict + * with the names of any {@code XSSFName} or {@code XSSFTable} in the workbook. * * @param tableArea * the area that the table should cover, should not be null @@ -4140,9 +4144,17 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { table.setArea(tableArea); } - // Bug 62906: Must set a display name; can be overridden using setDisplayName - final String displayName = "Table" + tableNumber; - table.setDisplayName(displayName); + // Set the default name of the table. This must not conflict with any defined names. + while(tableNumber<Integer.MAX_VALUE) { + final String displayName="Table"+tableNumber; + if(getWorkbook().getTable(displayName) == null && + getWorkbook().getName(displayName) == null) { + table.setDisplayName(displayName); + table.setName(displayName); + break; + } + ++tableNumber; + } return table; } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java index b26f72be50..ab874c8bde 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java @@ -369,7 +369,7 @@ public class XSSFTable extends POIXMLDocumentPart implements Table { * @return the name of the Table, if set */ public String getName() { - if (name == null) { + if (name == null && ctTable.getName() != null) { setName(ctTable.getName()); } return name; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java index 0b3e7da219..955e149101 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; +import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; @@ -35,6 +36,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -587,4 +589,37 @@ public final class TestXSSFTable { table1.setDisplayName(""); } } + + /** + * Delete table2, and create a named range in sheet0; it should automatically be assigned the name "Table4" + */ + @Test + public void testBug63401And62906() throws IOException { + try (XSSFWorkbook workbook = new XSSFWorkbook()) { + XSSFSheet sheet0 = workbook.createSheet(); + XSSFTable table = addTable(sheet0, 3, 0, 2, 2); + + final String procName = "testXSSFTableGetName"; + final String name = table.getName(); + System.out.println(String.format(Locale.ROOT, "%s: table.getName=%s", procName, name)); + } + } + + private static XSSFTable addTable(XSSFSheet sheet,int nRow, int nCol, int nNumRows, int nNumCols) { + for (int i = 0; i < nNumRows; i++) { + XSSFRow row = sheet.createRow(i + nRow); + for (int j = 0; j < nNumCols; j++) { + XSSFCell localXSSFCell = row.createCell(j + nCol); + if (i == 0) { + localXSSFCell.setCellValue(String.format(Locale.ROOT, "Col%d", j + 1)); + } else { + localXSSFCell.setCellValue(String.format(Locale.ROOT, "(%d,%d)", i + 1, j + 1)); + } + } + } + final CellReference upperLeft = new CellReference(nRow, nCol); + final CellReference lowerRight = new CellReference(nNumRows - 1, nNumCols - 1); + final AreaReference area = new AreaReference(upperLeft, lowerRight, SpreadsheetVersion.EXCEL2007); + return sheet.createTable(area); + } } |