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 2008/01/07 15:51:45 UTC

svn commit: r609620 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/HSSFSheet.java testcases/org/apache/poi/hssf/data/comments.xls testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java

Author: nick
Date: Mon Jan  7 06:51:37 2008
New Revision: 609620

URL: http://svn.apache.org/viewvc?rev=609620&view=rev
Log:
Fix bug #44070 - patch from Gian Carlo Pace

Added:
    poi/trunk/src/testcases/org/apache/poi/hssf/data/comments.xls   (with props)
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=609620&r1=609619&r2=609620&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Mon Jan  7 06:51:37 2008
@@ -1202,6 +1202,12 @@
                     row2Replace.createCellFromRecord( cellRecord );
                     sheet.addValueRecord( rowNum + n, cellRecord );
                 }
+
+                // move comments if exist (can exist even if cell is null)
+                HSSFComment comment = getCellComment(rowNum, col);
+                if (comment != null) {
+                   comment.setRow(rowNum + n);
+                }
             }
         }
         if ( endRow == lastrow || endRow + n > lastrow ) lastrow = Math.min( endRow + n, 65535 );
@@ -1671,7 +1677,21 @@
      * @return cell comment or <code>null</code> if not found
      */
      public HSSFComment getCellComment(int row, int column){
-        return HSSFCell.findCellComment(sheet, row, column);
+        // Don't call findCellComment directly, otherwise
+        //  two calls to this method will result in two
+        //  new HSSFComment instances, which is bad
+        HSSFRow r = getRow(row);
+        if(r != null) {
+            HSSFCell c = r.getCell((short)column);
+            if(c != null) {
+                return c.getCellComment();
+            } else {
+                // No cell, so you will get new
+                //  objects every time, sorry...
+                return HSSFCell.findCellComment(sheet, row, column);
+            }
+        }
+        return null;
     }
 
 }

Added: poi/trunk/src/testcases/org/apache/poi/hssf/data/comments.xls
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/data/comments.xls?rev=609620&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/src/testcases/org/apache/poi/hssf/data/comments.xls
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java?rev=609620&r1=609619&r2=609620&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java Mon Jan  7 06:51:37 2008
@@ -25,6 +25,8 @@
 import org.apache.poi.util.TempFile;
 
 import java.io.File;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 
@@ -169,6 +171,71 @@
       s.shiftRows(4, 4, 2);
       assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
       
+    }
+
+
+    public void testShiftWithComments() throws Exception {
+        String filename = System.getProperty( "HSSF.testdata.path" );
+        filename = filename + "/comments.xls";
+        FileInputStream fin = new FileInputStream( filename );
+        HSSFWorkbook wb = new HSSFWorkbook( fin );
+        fin.close();
+
+        HSSFSheet sheet = wb.getSheet("Sheet1");
+        assertEquals(3, sheet.getLastRowNum());
+        
+        // Verify comments are in the position expected
+        assertNotNull(sheet.getCellComment(0,0));
+        assertNull(sheet.getCellComment(1,0));
+        assertNotNull(sheet.getCellComment(2,0));
+        assertNotNull(sheet.getCellComment(3,0));
+        
+        String comment1 = sheet.getCellComment(0,0).getString().getString();
+        assertEquals(comment1,"comment top row1 (index0)\n");
+        String comment3 = sheet.getCellComment(2,0).getString().getString();
+        assertEquals(comment3,"comment top row3 (index2)\n");
+        String comment4 = sheet.getCellComment(3,0).getString().getString();
+        assertEquals(comment4,"comment top row4 (index3)\n");
+
+        // Shifting all but first line down to test comments shifting 
+        sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        wb.write(outputStream);
+
+        // Test that comments were shifted as expected
+        assertEquals(4, sheet.getLastRowNum());
+        assertNotNull(sheet.getCellComment(0,0));
+        assertNull(sheet.getCellComment(1,0));
+        assertNull(sheet.getCellComment(2,0));
+        assertNotNull(sheet.getCellComment(3,0));
+        assertNotNull(sheet.getCellComment(4,0));
+
+        String comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+        assertEquals(comment1,comment1_shifted);
+        String comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+        assertEquals(comment3,comment3_shifted);
+        String comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+        assertEquals(comment4,comment4_shifted);
+                
+        // Write out and read back in again
+        // Ensure that the changes were persisted
+        wb = new HSSFWorkbook( new ByteArrayInputStream(outputStream.toByteArray()) );
+        sheet = wb.getSheet("Sheet1");
+        assertEquals(4, sheet.getLastRowNum());
+
+        // Verify comments are in the position expected after the shift
+        assertNotNull(sheet.getCellComment(0,0));
+        assertNull(sheet.getCellComment(1,0));
+        assertNull(sheet.getCellComment(2,0));
+        assertNotNull(sheet.getCellComment(3,0));
+        assertNotNull(sheet.getCellComment(4,0));
+
+        comment1_shifted = sheet.getCellComment(0,0).getString().getString();
+        assertEquals(comment1,comment1_shifted);
+        comment3_shifted = sheet.getCellComment(3,0).getString().getString();
+        assertEquals(comment3,comment3_shifted);
+        comment4_shifted = sheet.getCellComment(4,0).getString().getString();
+        assertEquals(comment4,comment4_shifted);        
     }
 }
 



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