fixed logic for matching cells and comments in HSSFCell.getCellComment()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@831025 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-10-29 16:43:24 +00:00
parent f3e6137e01
commit 55b31d213d
4 changed files with 46 additions and 5 deletions

View 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>

View 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;
}
}

View 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());
}
}

Binary file not shown.