From: Javen O'Neal Date: Thu, 19 Jan 2017 07:44:25 +0000 (+0000) Subject: bug 60601: unlink hyperlinks from a sheet if all hyperlinks have been removed. Patch... X-Git-Tag: REL_3_16_BETA2~33 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f055f995498ab1739957abe10644f292feeb151b;p=poi.git bug 60601: unlink hyperlinks from a sheet if all hyperlinks have been removed. Patch from Joachim Piketz git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1779426 13f79535-47bb-0310-9956-ffa450edef68 --- 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 bde93d663c..b7b30126cb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -3484,6 +3484,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } worksheet.getHyperlinks().setHyperlinkArray(ctHls); } + else { + worksheet.unsetHyperlinks(); + } int minCell=Integer.MAX_VALUE, maxCell=Integer.MIN_VALUE; for(XSSFRow row : _rows.values()){ diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java index 4e7179109b..8e010a6c05 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java @@ -1231,6 +1231,29 @@ public abstract class BaseTestSheet { workbook.close(); } + + @Test + public void removeAllHyperlinks() throws IOException { + Workbook workbook = _testDataProvider.createWorkbook(); + Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL); + hyperlink.setAddress("https://poi.apache.org/"); + Sheet sheet = workbook.createSheet(); + Cell cell = sheet.createRow(5).createCell(1); + cell.setHyperlink(hyperlink); + + assertEquals(1, workbook.getSheetAt(0).getHyperlinkList().size()); + // Save a workbook with a hyperlink + Workbook workbook2 = _testDataProvider.writeOutAndReadBack(workbook); + assertEquals(1, workbook2.getSheetAt(0).getHyperlinkList().size()); + + // Remove all hyperlinks from a saved workbook + workbook2.getSheetAt(0).getRow(5).getCell(1).removeHyperlink(); + assertEquals(0, workbook2.getSheetAt(0).getHyperlinkList().size()); + + // Verify that hyperlink was removed from workbook after writing out + Workbook workbook3 = _testDataProvider.writeOutAndReadBack(workbook2); + assertEquals(0, workbook3.getSheetAt(0).getHyperlinkList().size()); + } @Test