}
/**
- * Assign a hyperlink to this cell
+ * Assign a hyperlink to this cell. If the supplied hyperlink is null, the
+ * hyperlink for this cell will be removed.
*
* @param hyperlink hyperlink associated with this cell
*/
public void setHyperlink(Hyperlink hyperlink){
+ if (hyperlink == null) {
+ removeHyperlink();
+ return;
+ }
+
HSSFHyperlink link = (HSSFHyperlink)hyperlink;
link.setFirstRow(_record.getRow());
int eofLoc = records.size() - 1;
records.add( eofLoc, link.record );
}
+
+ /**
+ * Removes the hyperlink for this cell, if there is one.
+ */
+ public void removeHyperlink() {
+ 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()) {
+ it.remove();
+ return;
+ }
+ }
+ }
+ }
+
/**
* Only valid for formula cells
* @return one of ({@link #CELL_TYPE_NUMERIC}, {@link #CELL_TYPE_STRING},
*/
void setHyperlink(Hyperlink link);
+ /**
+ * Removes the hyperlink for this cell, if there is one.
+ */
+ void removeHyperlink();
+
/**
* Only valid for array formula cells
*
}
/**
- * Assign a hyperlink to this cell
+ * Assign a hyperlink to this cell. If the supplied hyperlink is null, the
+ * hyperlink for this cell will be removed.
*
* @param link hyperlink associated with this cell
*/
public void setHyperlink(Hyperlink link)
{
+ if (link == null) {
+ removeHyperlink();
+ return;
+ }
+
setProperty(Property.HYPERLINK,link);
XSSFHyperlink xssfobj = (XSSFHyperlink)link;
}
+ /**
+ * Removes the hyperlink for this cell, if there is one.
+ */
+ public void removeHyperlink()
+ {
+ removeProperty(Property.HYPERLINK);
+
+ ((SXSSFSheet) getSheet())._sh.removeHyperlink(getRowIndex(), getColumnIndex());
+ }
+
/**
* Only valid for array formula cells
*
}
/**
- * Assign a hyperlink to this cell
+ * Assign a hyperlink to this cell. If the supplied hyperlink is null, the
+ * hyperlink for this cell will be removed.
*
* @param hyperlink the hyperlink to associate with this cell
*/
@Override
public void setHyperlink(Hyperlink hyperlink) {
+ if (hyperlink == null) {
+ removeHyperlink();
+ return;
+ }
+
XSSFHyperlink link = (XSSFHyperlink)hyperlink;
// Assign to us
getSheet().addHyperlink(link);
}
+ /**
+ * Removes the hyperlink for this cell, if there is one.
+ */
+ @Override
+ public void removeHyperlink() {
+ getSheet().removeHyperlink(_row.getRowNum(), _cellNum);
+ }
+
/**
* Returns the xml bean containing information about the cell's location (reference), value,
* data type, formatting, and formula
hyperlinks.add(hyperlink);
}
+ /**
+ * Removes a hyperlink in the collection of hyperlinks on this sheet
+ *
+ * @param row row index
+ * @param column column index
+ */
+ @Internal
+ public void removeHyperlink(int row, int column) {
+ String ref = new CellReference(row, column).formatAsString();
+ for (Iterator<XSSFHyperlink> it = hyperlinks.iterator(); it.hasNext();) {
+ XSSFHyperlink hyperlink = it.next();
+ if (hyperlink.getCellRef().equals(ref)) {
+ it.remove();
+ return;
+ }
+ }
+ }
+
/**
* Return location of the active cell, e.g. <code>A1</code>.
*
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
assertEquals("some", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
assertEquals("24", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
}
+
+ public void testRemoveHyperlink(){
+ Workbook wb = _testDataProvider.createWorkbook();
+ Sheet sh = wb.createSheet("test");
+ Row row = sh.createRow(0);
+ CreationHelper helper = wb.getCreationHelper();
+
+ Cell cell1 = row.createCell(1);
+ Hyperlink link1 = helper.createHyperlink(Hyperlink.LINK_URL);
+ cell1.setHyperlink(link1);
+ assertNotNull(cell1.getHyperlink());
+ cell1.removeHyperlink();
+ assertNull(cell1.getHyperlink());
+
+ Cell cell2 = row.createCell(0);
+ Hyperlink link2 = helper.createHyperlink(Hyperlink.LINK_URL);
+ cell2.setHyperlink(link2);
+ assertNotNull(cell2.getHyperlink());
+ cell2.setHyperlink(null);
+ assertNull(cell2.getHyperlink());
+
+ _testDataProvider.writeOutAndReadBack(wb);
+ }
}
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
+import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
cell.toString();
}
}
-
+
+ public void testRemoveHyperlink() {
+ final Workbook wb = new XSSFWorkbook();
+ final Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow(0);
+
+ Cell cell1 = row.createCell(1);
+ Hyperlink link1 = new XSSFHyperlink(Hyperlink.LINK_URL);
+ cell1.setHyperlink(link1);
+ assertNotNull(cell1.getHyperlink());
+ cell1.removeHyperlink();
+ assertNull(cell1.getHyperlink());
+
+ Cell cell2 = row.createCell(0);
+ Hyperlink link2 = new XSSFHyperlink(Hyperlink.LINK_URL);
+ cell2.setHyperlink(link2);
+ assertNotNull(cell2.getHyperlink());
+ cell2.setHyperlink(null);
+ assertNull(cell2.getHyperlink());
+
+ XSSFTestDataSamples.writeOutAndReadBack(wb);
+ }
}
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.ErrorConstants;
+import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
assertEquals(1, link2.getFirstColumn());
}
+ public void testRemoveHyperlink() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet();
+ HSSFRow row = sheet.createRow(0);
+
+ HSSFCell cell1 = row.createCell(1);
+ HSSFHyperlink link1 = new HSSFHyperlink(Hyperlink.LINK_URL);
+ assertNotNull(link1);
+ cell1.removeHyperlink();
+ assertNull(cell1.getHyperlink());
+
+ HSSFCell cell2 = row.createCell(0);
+ HSSFHyperlink link2 = new HSSFHyperlink(Hyperlink.LINK_URL);
+ assertNotNull(link2);
+ cell2.setHyperlink(null);
+ assertNull(cell2.getHyperlink());
+
+ HSSFTestDataSamples.writeOutAndReadBack(wb);
+ }
+
/**
* Test to ensure we can only assign cell styles that belong
* to our workbook, and not those from other workbooks.