mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
[bug-65993] support copying hssf hyperlinks to xssf and respect full cell range
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8406fbc3c0
commit
fb95dfdd93
@ -121,7 +121,11 @@ public class XSSFHyperlink implements Hyperlink, Duplicatable {
|
||||
_location = other.getAddress();
|
||||
_externalRel = null;
|
||||
_ctHyperlink = CTHyperlink.Factory.newInstance();
|
||||
setCellReference(new CellReference(other.getFirstRow(), other.getFirstColumn()));
|
||||
_ctHyperlink.setDisplay(other.getLabel());
|
||||
setFirstColumn(other.getFirstColumn());
|
||||
setLastColumn(other.getLastColumn());
|
||||
setFirstRow(other.getFirstRow());
|
||||
setLastRow(other.getLastRow());
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
||||
@ -209,6 +209,78 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCopyHSSFHyperlink() throws IOException {
|
||||
try (HSSFWorkbook hssfworkbook = new HSSFWorkbook()) {
|
||||
HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
|
||||
hlink.setAddress("http://poi.apache.org/");
|
||||
hlink.setFirstColumn(3);
|
||||
hlink.setFirstRow(2);
|
||||
hlink.setLastColumn(3);
|
||||
hlink.setLastRow(2);
|
||||
hlink.setLabel("label");
|
||||
XSSFHyperlink xlink = new XSSFHyperlink(hlink);
|
||||
|
||||
assertEquals("http://poi.apache.org/", xlink.getAddress());
|
||||
assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
|
||||
// Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
|
||||
// assertEquals("label", xlink.getTooltip());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setHyperlinkOnCellFromHSSF() throws Exception {
|
||||
try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook()) {
|
||||
XSSFSheet xssfSheet = xssfWorkbook.createSheet();
|
||||
XSSFRow xssfRow = xssfSheet.createRow(0);
|
||||
XSSFCell xssfCell = xssfRow.createCell(0);
|
||||
xssfCell.setCellValue("link");
|
||||
HSSFHyperlink hssfHyperlink = new HSSFHyperlink(HyperlinkType.URL) {
|
||||
|
||||
};
|
||||
hssfHyperlink.setLabel("label");
|
||||
hssfHyperlink.setAddress("https://example.com/index.html#label");
|
||||
hssfHyperlink.setFirstColumn(2);
|
||||
hssfHyperlink.setLastColumn(3);
|
||||
hssfHyperlink.setFirstRow(4);
|
||||
hssfHyperlink.setLastRow(5);
|
||||
xssfCell.setHyperlink(hssfHyperlink);
|
||||
XSSFHyperlink xssfHyperlink = xssfCell.getHyperlink();
|
||||
assertEquals(hssfHyperlink.getType(), xssfHyperlink.getType());
|
||||
assertEquals(hssfHyperlink.getAddress(), xssfHyperlink.getAddress());
|
||||
assertEquals(hssfHyperlink.getLabel(), xssfHyperlink.getLabel());
|
||||
assertEquals("A1", xssfHyperlink.getCellRef());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setHyperlinkOnSheetFromHSSF() throws Exception {
|
||||
try (XSSFWorkbook xssfWorkbook = new XSSFWorkbook()) {
|
||||
XSSFSheet xssfSheet = xssfWorkbook.createSheet();
|
||||
XSSFRow xssfRow = xssfSheet.createRow(0);
|
||||
XSSFCell xssfCell = xssfRow.createCell(0);
|
||||
xssfCell.setCellValue("link");
|
||||
HSSFHyperlink hssfHyperlink = new HSSFHyperlink(HyperlinkType.URL) {
|
||||
|
||||
};
|
||||
hssfHyperlink.setLabel("label");
|
||||
hssfHyperlink.setAddress("https://example.com/index.html#label");
|
||||
hssfHyperlink.setFirstColumn(2);
|
||||
hssfHyperlink.setLastColumn(3);
|
||||
hssfHyperlink.setFirstRow(4);
|
||||
hssfHyperlink.setLastRow(5);
|
||||
XSSFHyperlink xssfHyperlink = new XSSFHyperlink(hssfHyperlink);
|
||||
xssfSheet.addHyperlink(xssfHyperlink);
|
||||
assertEquals(hssfHyperlink.getType(), xssfHyperlink.getType());
|
||||
assertEquals(hssfHyperlink.getAddress(), xssfHyperlink.getAddress());
|
||||
assertEquals(hssfHyperlink.getLabel(), xssfHyperlink.getLabel());
|
||||
assertEquals(hssfHyperlink.getFirstColumn(), xssfHyperlink.getFirstColumn());
|
||||
assertEquals(hssfHyperlink.getLastColumn(), xssfHyperlink.getLastColumn());
|
||||
assertEquals(hssfHyperlink.getFirstRow(), xssfHyperlink.getFirstRow());
|
||||
assertEquals(hssfHyperlink.getLastRow(), xssfHyperlink.getLastRow());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only for WithMoreVariousData.xlsx !
|
||||
*/
|
||||
@ -300,26 +372,6 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
|
||||
return new XSSFHyperlink(link);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCopyHSSFHyperlink() throws IOException {
|
||||
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
|
||||
HSSFHyperlink hlink = hssfworkbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
|
||||
hlink.setAddress("http://poi.apache.org/");
|
||||
hlink.setFirstColumn(3);
|
||||
hlink.setFirstRow(2);
|
||||
hlink.setLastColumn(5);
|
||||
hlink.setLastRow(6);
|
||||
hlink.setLabel("label");
|
||||
XSSFHyperlink xlink = new XSSFHyperlink(hlink);
|
||||
|
||||
assertEquals("http://poi.apache.org/", xlink.getAddress());
|
||||
assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
|
||||
// Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
|
||||
// assertEquals("label", xlink.getTooltip());
|
||||
|
||||
hssfworkbook.close();
|
||||
}
|
||||
|
||||
/* bug 59775: XSSFHyperlink has wrong type if it contains a location (CTHyperlink#getLocation)
|
||||
* URLs with a hash mark (#) are still URL hyperlinks, not document links
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user