git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@831025 13f79535-47bb-0310-9956-ffa450edef68tags/REL_3_6
<changes> | <changes> | ||||
<release version="3.6-beta1" date="2009-??-??"> | <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="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="fix">48070 - preserve leading and trailing white spaces in XSSFRichTextString</action> | ||||
<action dev="POI-DEVELOPERS" type="add">48044 - added implementation for CountBlank function</action> | <action dev="POI-DEVELOPERS" type="add">48044 - added implementation for CountBlank function</action> |
protected static HSSFComment findCellComment(Sheet sheet, int row, int column) { | 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 | // TODO - optimise this code by searching backwards, find NoteRecord first, quit if not found. Find one TXO by id | ||||
HSSFComment comment = null; | HSSFComment comment = null; | ||||
ArrayList<TextObjectRecord> noteTxo = new ArrayList<TextObjectRecord>(); | |||||
Map<Integer, TextObjectRecord> noteTxo = | |||||
new HashMap<Integer, TextObjectRecord>(); | |||||
int i = 0; | int i = 0; | ||||
for (Iterator<RecordBase> it = sheet.getRecords().iterator(); it.hasNext();) { | for (Iterator<RecordBase> it = sheet.getRecords().iterator(); it.hasNext();) { | ||||
RecordBase rec = it.next(); | RecordBase rec = it.next(); | ||||
NoteRecord note = (NoteRecord) rec; | NoteRecord note = (NoteRecord) rec; | ||||
if (note.getRow() == row && note.getColumn() == column) { | if (note.getRow() == row && note.getColumn() == column) { | ||||
if(i < noteTxo.size()) { | if(i < noteTxo.size()) { | ||||
TextObjectRecord txo = noteTxo.get(i); | |||||
TextObjectRecord txo = noteTxo.get(note.getShapeId()); | |||||
comment = new HSSFComment(note, txo); | comment = new HSSFComment(note, txo); | ||||
comment.setRow(note.getRow()); | comment.setRow(note.getRow()); | ||||
comment.setColumn((short) note.getColumn()); | comment.setColumn((short) note.getColumn()); | ||||
if (sub instanceof CommonObjectDataSubRecord) { | if (sub instanceof CommonObjectDataSubRecord) { | ||||
CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) sub; | CommonObjectDataSubRecord cmo = (CommonObjectDataSubRecord) sub; | ||||
if (cmo.getObjectType() == CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT) { | 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()) { | while (it.hasNext()) { | ||||
rec = it.next(); | rec = it.next(); | ||||
if (rec instanceof TextObjectRecord) { | if (rec instanceof TextObjectRecord) { | ||||
noteTxo.add((TextObjectRecord) rec); | |||||
noteTxo.put(cmo.getObjectId(), (TextObjectRecord) rec); | |||||
break; | break; | ||||
} | } | ||||
} | } |
import junit.framework.TestCase; | import junit.framework.TestCase; | ||||
import org.apache.poi.hssf.HSSFTestDataSamples; | 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. | * Tests TestHSSFCellComment. | ||||
// wb.write(fout); | // wb.write(fout); | ||||
// fout.close(); | // 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()); | |||||
} | |||||
} | } |