support removing hyperlink from one cell when hyperlink is shared by many cells

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892249 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-08-11 21:25:00 +00:00
parent 80b73d65b1
commit ef61f3da7c
1 changed files with 28 additions and 0 deletions

View File

@ -32,6 +32,8 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import static org.junit.jupiter.api.Assertions.*;
@ -491,6 +493,32 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
testRemoveSharedHyperlinkFromOneCell("D3:D5", new CellAddress("D5"));
}
@Test
void testRemoveSharedHyperlinkFromOneCellWithCellRefs() throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
XSSFHyperlink hyperlink = new XSSFHyperlink(HyperlinkType.URL);
hyperlink.setAddress("https://poi.apache.org");
hyperlink.setLocation("poi-location");
hyperlink.setLabel("poi-label");
hyperlink.setCellReference("A1:E5");
sheet.addHyperlink(hyperlink);
CellAddress cellAddress = new CellAddress("C3");
sheet.removeHyperlink(cellAddress.getRow(), cellAddress.getColumn());
assertNull(sheet.getHyperlink(cellAddress), "cell " + cellAddress.formatAsString() + "should no longer has a hyperlink");
ArrayList<String> newRefs = new ArrayList<>();
for (XSSFHyperlink testHyperlink : sheet.getHyperlinkList()) {
newRefs.add(testHyperlink.getCellRef());
}
assertEquals(4, newRefs.size());
HashSet<String> set = new HashSet<>(newRefs);
assertTrue(set.contains("A1:B5"), "contains A1:B5");
assertTrue(set.contains("D1:E5"), "contains D1:E5");
assertTrue(set.contains("C1:C2"), "contains C1:C2");
assertTrue(set.contains("C4:C5"), "contains C4:C5");
}
}
private void testRemoveSharedHyperlinkFromOneCell(String area, CellAddress cellAddress) throws IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();