You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2014/11/08 14:46:30 UTC

svn commit: r1637562 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/streaming/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/streaming/ ooxml/tes...

Author: nick
Date: Sat Nov  8 13:46:30 2014
New Revision: 1637562

URL: http://svn.apache.org/r1637562
Log:
Patch from hishidama to add Cell.removeHyperlink(). This closes #13 from github

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Sat Nov  8 13:46:30 2014
@@ -1062,11 +1062,17 @@ public class HSSFCell implements Cell {
     }
 
     /**
-     * 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());
@@ -1091,6 +1097,23 @@ public class HSSFCell implements Cell {
         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},

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Cell.java Sat Nov  8 13:46:30 2014
@@ -381,6 +381,11 @@ public interface Cell {
     void setHyperlink(Hyperlink link);
 
     /**
+     * Removes the hyperlink for this cell, if there is one.
+     */
+    void removeHyperlink();
+
+    /**
      * Only valid for array formula cells
      *
      * @return range of the array formula group that the cell belongs to.

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java Sat Nov  8 13:46:30 2014
@@ -572,12 +572,18 @@ public class SXSSFCell implements Cell 
     }
 
     /**
-     * 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;
@@ -591,6 +597,16 @@ public class SXSSFCell implements Cell 
     }
 
     /**
+     * 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
      *
      * @return range of the array formula group that the cell belongs to.

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Sat Nov  8 13:46:30 2014
@@ -947,12 +947,18 @@ public final class XSSFCell implements C
     }
 
     /**
-     * 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
@@ -963,6 +969,14 @@ public final class XSSFCell implements C
     }
 
     /**
+     * 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
      *

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Sat Nov  8 13:46:30 2014
@@ -2743,6 +2743,24 @@ public class XSSFSheet extends POIXMLDoc
     }
 
     /**
+     * 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>.
      *
      * @return the location of the active cell.

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFCell.java Sat Nov  8 13:46:30 2014
@@ -25,6 +25,8 @@ import javax.xml.namespace.QName;
 
 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;
@@ -168,4 +170,27 @@ public class TestSXSSFCell extends BaseT
         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);
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java Sat Nov  8 13:46:30 2014
@@ -22,6 +22,7 @@ import java.io.IOException;
 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;
@@ -371,5 +372,26 @@ public final class TestXSSFCell extends 
             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);
+    }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java?rev=1637562&r1=1637561&r2=1637562&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Sat Nov  8 13:46:30 2014
@@ -34,6 +34,7 @@ import org.apache.poi.ss.SpreadsheetVers
 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;
@@ -253,6 +254,26 @@ public final class TestHSSFCell extends 
 		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.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org