/**
* @return hyperlink associated with this cell or <code>null</code> if not found
*/
+ @Override
public HSSFHyperlink getHyperlink(){
- for (Iterator<RecordBase> it = _sheet.getSheet().getRecords().iterator(); it.hasNext(); ) {
- RecordBase rec = it.next();
- if (rec instanceof HyperlinkRecord){
- HyperlinkRecord link = (HyperlinkRecord)rec;
- if(link.getFirstColumn() == _record.getColumn() && link.getFirstRow() == _record.getRow()){
- return new HSSFHyperlink(link);
- }
- }
- }
- return null;
+ return _sheet.getHyperlink(_record.getRow(), _record.getColumn());
}
/**
*
* @param hyperlink hyperlink associated with this cell
*/
+ @Override
public void setHyperlink(Hyperlink hyperlink){
if (hyperlink == null) {
removeHyperlink();
/**
* Low-level record object that stores the actual hyperlink data
*/
- protected HyperlinkRecord record = null;\r
+ final protected HyperlinkRecord record;\r
\r
/**\r
* If we create a new hyperlink remember its type\r
*/\r
- protected int link_type;\r
+ final protected int link_type;\r
\r
/**
* Construct a new hyperlink
}
}
}
+
+ @Override
+ public HSSFHyperlink clone() {
+ return new HSSFHyperlink(record.clone());
+ /*final HSSFHyperlink link = new HSSFHyperlink(link_type);
+ link.setLabel(getLabel());
+ link.setAddress(getAddress());
+ link.setFirstColumn(getFirstColumn());
+ link.setFirstRow(getFirstRow());
+ link.setLastColumn(getLastColumn());
+ link.setLastRow(getLastRow());
+ return link;*/
+ }
/**
* Return the row of the first cell that contains the hyperlink
public int getType(){
return link_type;
}
+
+ /**
+ * @return whether the objects have the same HyperlinkRecord
+ */
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) return true;
+ if (!(other instanceof HSSFHyperlink)) return false;
+ HSSFHyperlink otherLink = (HSSFHyperlink) other;
+ return record == otherLink.record;
+ }
+
+ @Override
+ public int hashCode() {
+ return record.hashCode();
+ }
}
import org.apache.poi.hssf.record.DrawingRecord;
import org.apache.poi.hssf.record.EscherAggregate;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RecordBase;
import org.apache.poi.hssf.record.RowRecord;
import org.apache.poi.hssf.record.SCLRecord;
import org.apache.poi.hssf.record.WSBoolRecord;
public HSSFComment getCellComment(int row, int column) {
return findCellComment(row, column);
}
+
+ /**
+ * Get a Hyperlink in this sheet anchored at row, column
+ *
+ * @param row
+ * @param column
+ * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
+ */
+ @Override
+ public HSSFHyperlink getHyperlink(int row, int column) {
+ for (Iterator<RecordBase> it = _sheet.getRecords().iterator(); it.hasNext(); ) {
+ RecordBase rec = it.next();
+ if (rec instanceof HyperlinkRecord){
+ HyperlinkRecord link = (HyperlinkRecord)rec;
+ if (link.getFirstColumn() == column && link.getFirstRow() == row) {
+ return new HSSFHyperlink(link);
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get a list of Hyperlinks in this sheet
+ *
+ * @return Hyperlinks for the sheet
+ */
+ @Override
+ public List<HSSFHyperlink> getHyperlinkList() {
+ final List<HSSFHyperlink> hyperlinkList = new ArrayList<HSSFHyperlink>();
+ for (Iterator<RecordBase> it = _sheet.getRecords().iterator(); it.hasNext(); ) {
+ RecordBase rec = it.next();
+ if (rec instanceof HyperlinkRecord){
+ HyperlinkRecord link = (HyperlinkRecord)rec;
+ hyperlinkList.add(new HSSFHyperlink(link));
+ }
+ }
+ return hyperlinkList;
+ }
public HSSFSheetConditionalFormatting getSheetConditionalFormatting() {
return new HSSFSheetConditionalFormatting(this);
/**
* Represents an Excel hyperlink.
*/
-public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink {
+public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink, Cloneable {
/**
* Return the row of the first cell that contains the hyperlink
*
* @param col the 0-based column of the last cell that contains the hyperlink
*/
public void setLastColumn(int col);
+
+ /**
+ * Create a clone of this hyperlink
+ *
+ * @return clone of this Hyperlink
+ */
+ public Hyperlink clone();
}
* you take it out of them.
*/
int getColumnOutlineLevel(int columnIndex);
+
+ /**
+ * Get a Hyperlink in this sheet anchored at row, column
+ *
+ * @param row
+ * @param column
+ * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
+ */
+ public Hyperlink getHyperlink(int row, int column);
+
+ /**
+ * Get a list of Hyperlinks in this sheet
+ *
+ * @return Hyperlinks for the sheet
+ */
+ public List<? extends Hyperlink> getHyperlinkList();
}
/**
* @return hyperlink associated with this cell or <code>null</code> if not found
*/
+ @Override
public Hyperlink getHyperlink()
{
return (Hyperlink)getPropertyValue(Property.HYPERLINK);
*
* @param link hyperlink associated with this cell
*/
+ @Override
public void setHyperlink(Hyperlink link)
{
if (link == null) {
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
+import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
{
return _sh.getCellComment(row, column);
}
+
+ /**
+ * Get a Hyperlink in this sheet anchored at row, column
+ *
+ * @param row
+ * @param column
+ * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
+ */
+ @Override
+ public XSSFHyperlink getHyperlink(int row, int column) {
+ return _sh.getHyperlink(row, column);
+ }
+
+ /**
+ * Get a list of Hyperlinks in this sheet
+ *
+ * @return Hyperlinks for the sheet
+ */
+ @Override
+ public List<XSSFHyperlink> getHyperlinkList() {
+ return _sh.getHyperlinkList();
+ }
/**
* Creates the top-level drawing patriarch.
* are largely stored as relations of the sheet
*/
public class XSSFHyperlink implements Hyperlink {
- private int _type;
- private PackageRelationship _externalRel;
- private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef()
+ final private int _type;
+ final private PackageRelationship _externalRel;
+ final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef()
private String _location; //what the hyperlink refers to
/**
protected XSSFHyperlink(int type) {
_type = type;
_ctHyperlink = CTHyperlink.Factory.newInstance();
+ _externalRel = null;
}
/**
- * Create a XSSFHyperlink amd initialize it from the supplied CTHyperlink bean and package relationship
+ * Create a XSSFHyperlink and initialize it from the supplied CTHyperlink bean and package relationship
*
* @param ctHyperlink the xml bean containing xml properties
* @param hyperlinkRel the relationship in the underlying OPC package which stores the actual link's address
}
}
+
+ @Override
+ public Hyperlink clone() {
+ final XSSFHyperlink clone = new XSSFHyperlink((CTHyperlink) _ctHyperlink.copy(), _externalRel);
+ clone.setLocation(_location);
+ return clone;
+ }
/**
* @return the underlying CTHyperlink object
* @param column
* @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
*/
+ @Override
public XSSFHyperlink getHyperlink(int row, int column) {
String ref = new CellReference(row, column).formatAsString();
for(XSSFHyperlink hyperlink : hyperlinks) {
*
* @return Hyperlinks for the sheet
*/
+ @Override
public List<XSSFHyperlink> getHyperlinkList() {
return Collections.unmodifiableList(hyperlinks);
}
package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
+
+import java.util.List;
+
import org.junit.Test;
import org.apache.poi.ss.ITestDataProvider;
link = sheet.getRow(3).getCell(0).getHyperlink();
assertEquals("'Target Sheet'!A1", link.getAddress());
}
+
+ @Test
+ public void testClone() {
+ System.out.println("testClone");
+ final Workbook wb = _testDataProvider.createWorkbook();
+ final CreationHelper createHelper = wb.getCreationHelper();
+
+ final Sheet sheet = wb.createSheet("Hyperlinks");
+ final Row row = sheet.createRow(0);
+ final Cell cell1, cell2;
+ final Hyperlink link1, link2;
+
+ //URL
+ cell1 = row.createCell(0);
+ cell2 = row.createCell(1);
+ cell1.setCellValue("URL Link");
+ link1 = createHelper.createHyperlink(Hyperlink.LINK_URL);
+ link1.setAddress("http://poi.apache.org/");
+ cell1.setHyperlink(link1);
+
+ link2 = link1.clone();
+
+ // Change address (type is not changeable)
+ link2.setAddress("http://apache.org/");
+ cell2.setHyperlink(link2);
+
+ // Make sure hyperlinks were deep-copied, and modifying one does not modify the other.
+ assertNotSame(link1, link2);
+ assertNotEquals(link1, link2);
+ assertEquals("http://poi.apache.org/", link1.getAddress());
+ assertEquals("http://apache.org/", link2.getAddress());
+ assertEquals(link1, cell1.getHyperlink());
+ assertEquals(link2, cell2.getHyperlink());
+
+ // Make sure both hyperlinks were added to the sheet
+ @SuppressWarnings("unchecked")
+ final List<Hyperlink> actualHyperlinks = (List<Hyperlink>) sheet.getHyperlinkList();
+ assertEquals(2, actualHyperlinks.size());
+ assertEquals(link1, actualHyperlinks.get(0));
+ assertEquals(link2, actualHyperlinks.get(1));
+ }
}