View | Details | Raw Unified | Return to bug 44070
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-1 / +21 lines)
Lines 1202-1207 Link Here
1202
                    row2Replace.createCellFromRecord( cellRecord );
1202
                    row2Replace.createCellFromRecord( cellRecord );
1203
                    sheet.addValueRecord( rowNum + n, cellRecord );
1203
                    sheet.addValueRecord( rowNum + n, cellRecord );
1204
                }
1204
                }
1205
1206
                // move comments if exist (can exist even if cell is null)
1207
                HSSFComment comment = getCellComment(rowNum, col);
1208
                if (comment != null) {
1209
                   comment.setRow(rowNum + n);
1210
                }
1205
            }
1211
            }
1206
        }
1212
        }
1207
        if ( endRow == lastrow || endRow + n > lastrow ) lastrow = Math.min( endRow + n, 65535 );
1213
        if ( endRow == lastrow || endRow + n > lastrow ) lastrow = Math.min( endRow + n, 65535 );
Lines 1671-1677 Link Here
1671
     * @return cell comment or <code>null</code> if not found
1677
     * @return cell comment or <code>null</code> if not found
1672
     */
1678
     */
1673
     public HSSFComment getCellComment(int row, int column){
1679
     public HSSFComment getCellComment(int row, int column){
1674
        return HSSFCell.findCellComment(sheet, row, column);
1680
        // Don't call findCellComment directly, otherwise
1681
        //  two calls to this method will result in two
1682
        //  new HSSFComment instances, which is bad
1683
        HSSFRow r = getRow(row);
1684
        if(r != null) {
1685
            HSSFCell c = r.getCell((short)column);
1686
            if(c != null) {
1687
                return c.getCellComment();
1688
            } else {
1689
                // No cell, so you will get new
1690
                //  objects every time, sorry...
1691
                return HSSFCell.findCellComment(sheet, row, column);
1692
            }
1693
        }
1694
        return null;
1675
    }
1695
    }
1676
1696
1677
}
1697
}
(-)src/testcases/org/apache/poi/hssf/usermodel/TestSheetShiftRows.java (-3 / +66 lines)
Lines 24-32 Link Here
24
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
24
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
25
import org.apache.poi.util.TempFile;
25
import org.apache.poi.util.TempFile;
26
26
27
import java.io.File;
27
import java.io.*;
28
import java.io.FileInputStream;
29
import java.io.FileOutputStream;
30
28
31
/**
29
/**
32
 * Tests row shifting capabilities.
30
 * Tests row shifting capabilities.
Lines 170-174 Link Here
170
      assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
168
      assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
171
      
169
      
172
    }
170
    }
171
172
173
    public void testShiftWithComments() throws Exception {
174
        String filename = System.getProperty( "HSSF.testdata.path" );
175
        filename = filename + "/comments.xls";
176
        FileInputStream fin = new FileInputStream( filename );
177
        HSSFWorkbook wb = new HSSFWorkbook( fin );
178
        fin.close();
179
180
        HSSFSheet sheet = wb.getSheet("Sheet1");
181
        assertEquals(3, sheet.getLastRowNum());
182
        
183
        // Verify comments are in the position expected
184
        assertNotNull(sheet.getCellComment(0,0));
185
        assertNull(sheet.getCellComment(1,0));
186
        assertNotNull(sheet.getCellComment(2,0));
187
        assertNotNull(sheet.getCellComment(3,0));
188
        
189
        String comment1 = sheet.getCellComment(0,0).getString().getString();
190
        assertEquals(comment1,"comment top row1 (index0)\n");
191
        String comment3 = sheet.getCellComment(2,0).getString().getString();
192
        assertEquals(comment3,"comment top row3 (index2)\n");
193
        String comment4 = sheet.getCellComment(3,0).getString().getString();
194
        assertEquals(comment4,"comment top row4 (index3)\n");
195
196
        // Shifting all but first line down to test comments shifting 
197
        sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);
198
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
199
        wb.write(outputStream);
200
201
        // Test that comments were shifted as expected
202
        assertEquals(4, sheet.getLastRowNum());
203
        assertNotNull(sheet.getCellComment(0,0));
204
        assertNull(sheet.getCellComment(1,0));
205
        assertNull(sheet.getCellComment(2,0));
206
        assertNotNull(sheet.getCellComment(3,0));
207
        assertNotNull(sheet.getCellComment(4,0));
208
209
        String comment1_shifted = sheet.getCellComment(0,0).getString().getString();
210
        assertEquals(comment1,comment1_shifted);
211
        String comment3_shifted = sheet.getCellComment(3,0).getString().getString();
212
        assertEquals(comment3,comment3_shifted);
213
        String comment4_shifted = sheet.getCellComment(4,0).getString().getString();
214
        assertEquals(comment4,comment4_shifted);
215
                
216
        // Write out and read back in again
217
        // Ensure that the changes were persisted
218
        wb = new HSSFWorkbook( new ByteArrayInputStream(outputStream.toByteArray()) );
219
        sheet = wb.getSheet("Sheet1");
220
        assertEquals(4, sheet.getLastRowNum());
221
222
        // Verify comments are in the position expected after the shift
223
        assertNotNull(sheet.getCellComment(0,0));
224
        assertNull(sheet.getCellComment(1,0));
225
        assertNull(sheet.getCellComment(2,0));
226
        assertNotNull(sheet.getCellComment(3,0));
227
        assertNotNull(sheet.getCellComment(4,0));
228
229
        comment1_shifted = sheet.getCellComment(0,0).getString().getString();
230
        assertEquals(comment1,comment1_shifted);
231
        comment3_shifted = sheet.getCellComment(3,0).getString().getString();
232
        assertEquals(comment3,comment3_shifted);
233
        comment4_shifted = sheet.getCellComment(4,0).getString().getString();
234
        assertEquals(comment4,comment4_shifted);        
235
    }
173
}
236
}
174
237

Return to bug 44070