]> source.dussan.org Git - poi.git/commitdiff
Fix bug #44070 - patch from Gian Carlo Pace
authorNick Burch <nick@apache.org>
Mon, 7 Jan 2008 14:51:37 +0000 (14:51 +0000)
committerNick Burch <nick@apache.org>
Mon, 7 Jan 2008 14:51:37 +0000 (14:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@609620 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/data/comments.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java

index b52fa1e5b20a29f24317e0aa0500cb13b2edfc13..ba9ff72a7ec94e7d4b0d86f8add37f0df499f2fe 100644 (file)
@@ -1202,6 +1202,12 @@ public class HSSFSheet
                     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 @@ public class HSSFSheet
      * @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;
     }
 
 }
diff --git a/src/testcases/org/apache/poi/hssf/data/comments.xls b/src/testcases/org/apache/poi/hssf/data/comments.xls
new file mode 100644 (file)
index 0000000..9eb0329
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/comments.xls differ
index d07a3eea5a80c2994f835f19fd1a7724ec404ee3..91876fff259e5ae316bc200d9737f6be6de20d2d 100755 (executable)
@@ -25,6 +25,8 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 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;
 
@@ -170,5 +172,70 @@ public class TestSheetShiftRows extends TestCase {
       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);        
+    }
 }