]> source.dussan.org Git - poi.git/commitdiff
fixed logic for matching cells and comments in HSSFCell.getCellComment()
authorYegor Kozlov <yegor@apache.org>
Thu, 29 Oct 2009 16:43:24 +0000 (16:43 +0000)
committerYegor Kozlov <yegor@apache.org>
Thu, 29 Oct 2009 16:43:24 +0000 (16:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@831025 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
test-data/spreadsheet/47924.xls [new file with mode: 0644]

index 209d8ae8d8432ec85ce92f570813a48b3ccf6c08..0891579ed856186b070ed59d1ec7a32e402b3aff 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.6-beta1" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">47924 - fixed logic for matching cells and comments in HSSFCell.getCellComment()</action>
            <action dev="POI-DEVELOPERS" type="add">47942 - added implementation of protection features to XLSX and DOCX files</action>
            <action dev="POI-DEVELOPERS" type="fix">48070 - preserve leading and trailing white spaces in XSSFRichTextString</action>
            <action dev="POI-DEVELOPERS" type="add">48044 - added implementation for CountBlank function</action>
index 999fa287dc9c0f905068bd87d079a7e6c8eff946..701b7ce74641c12755d154d74162c78792537462 100644 (file)
@@ -1054,7 +1054,8 @@ public class HSSFCell implements Cell {
     protected static HSSFComment findCellComment(Sheet sheet, int row, int column) {
         // TODO - optimise this code by searching backwards, find NoteRecord first, quit if not found. Find one TXO by id
         HSSFComment comment = null;
-        ArrayList<TextObjectRecord> noteTxo = new ArrayList<TextObjectRecord>();
+        Map<Integer, TextObjectRecord> noteTxo =
+                               new HashMap<Integer, TextObjectRecord>();
         int i = 0;
         for (Iterator<RecordBase> it = sheet.getRecords().iterator(); it.hasNext();) {
             RecordBase rec = it.next();
@@ -1062,7 +1063,7 @@ public class HSSFCell implements Cell {
                 NoteRecord note = (NoteRecord) rec;
                 if (note.getRow() == row && note.getColumn() == column) {
                     if(i < noteTxo.size()) {
-                        TextObjectRecord txo = noteTxo.get(i);
+                        TextObjectRecord txo = noteTxo.get(note.getShapeId());
                         comment = new HSSFComment(note, txo);
                         comment.setRow(note.getRow());
                         comment.setColumn((short) note.getColumn());
@@ -1081,12 +1082,12 @@ public class HSSFCell implements Cell {
                 if (sub instanceof CommonObjectDataSubRecord) {
                     CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) sub;
                     if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT) {
-                        //find the next TextObjectRecord which holds the comment's text
-                        //the order of TXO matches the order of NoteRecords: i-th TXO record corresponds to the i-th NoteRecord
+                        //map ObjectId and corresponding TextObjectRecord,
+                        //it will be used to match NoteRecord and TextObjectRecord
                         while (it.hasNext()) {
                             rec = it.next();
                             if (rec instanceof TextObjectRecord) {
-                                noteTxo.add((TextObjectRecord) rec);
+                                noteTxo.put(cmo.getObjectId(), (TextObjectRecord) rec);
                                 break;
                             }
                         }
index 7014cb51498b6a34bab6ed5db07d5fc621e59c19..bb4707b321e829e14abcf0c4a4bb7018408e39c5 100644 (file)
@@ -23,6 +23,10 @@ import java.io.IOException;
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.util.CellReference;
 
 /**
  * Tests TestHSSFCellComment.
@@ -215,4 +219,39 @@ public final class TestHSSFComment extends TestCase {
 //        wb.write(fout);
 //        fout.close();
     }
+
+    /**
+     *  HSSFCell#findCellComment should NOT rely on the order of records
+     * when matching cells and their cell comments. The correct algorithm is to map
+     */
+    public static void test47924() {
+        HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("47924.xls");
+        HSSFSheet sheet = wb.getSheetAt(0);
+        HSSFCell cell;
+        HSSFComment comment;
+
+        cell = sheet.getRow(0).getCell(0);
+        comment = cell.getCellComment();
+        assertEquals("a1", comment.getString().getString());
+
+        cell = sheet.getRow(1).getCell(0);
+        comment = cell.getCellComment();
+        assertEquals("a2", comment.getString().getString());
+
+        cell = sheet.getRow(2).getCell(0);
+        comment = cell.getCellComment();
+        assertEquals("a3", comment.getString().getString());
+
+        cell = sheet.getRow(2).getCell(2);
+        comment = cell.getCellComment();
+        assertEquals("c3", comment.getString().getString());
+
+        cell = sheet.getRow(4).getCell(1);
+        comment = cell.getCellComment();
+        assertEquals("b5", comment.getString().getString());
+
+        cell = sheet.getRow(5).getCell(2);
+        comment = cell.getCellComment();
+        assertEquals("c6", comment.getString().getString());
+    }
 }
diff --git a/test-data/spreadsheet/47924.xls b/test-data/spreadsheet/47924.xls
new file mode 100644 (file)
index 0000000..287f17b
Binary files /dev/null and b/test-data/spreadsheet/47924.xls differ