import org.apache.poi.hssf.model.FormulaParser;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
-import org.apache.poi.hssf.record.*;
+import org.apache.poi.hssf.record.BlankRecord;
+import org.apache.poi.hssf.record.BoolErrRecord;
+import org.apache.poi.hssf.record.CellValueRecordInterface;
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
+import org.apache.poi.hssf.record.DrawingRecord;
+import org.apache.poi.hssf.record.EOFRecord;
+import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.HyperlinkRecord;
+import org.apache.poi.hssf.record.LabelSSTRecord;
+import org.apache.poi.hssf.record.NoteRecord;
+import org.apache.poi.hssf.record.NumberRecord;
+import org.apache.poi.hssf.record.ObjRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.StringRecord;
+import org.apache.poi.hssf.record.SubRecord;
+import org.apache.poi.hssf.record.TextObjectRecord;
+import org.apache.poi.hssf.record.UnicodeString;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.Ptg;
}
/**
- * Assign a comment to this cell
+ * Assign a comment to this cell. If the supplied
+ * comment is null, the comment for this cell
+ * will be removed.
*
* @param comment comment associated with this cell
*/
public void setCellComment(HSSFComment comment){
+ if(comment == null) {
+ removeCellComment();
+ return;
+ }
+
comment.setRow((short)record.getRow());
comment.setColumn(record.getColumn());
this.comment = comment;
}
return comment;
}
+
+ /**
+ * Removes the comment for this cell, if
+ * there is one.
+ * WARNING - some versions of excel will loose
+ * all comments after performing this action!
+ */
+ public void removeCellComment() {
+ HSSFComment comment = findCellComment(sheet, record.getRow(), record.getColumn());
+ this.comment = null;
+
+ if(comment == null) {
+ // Nothing to do
+ return;
+ }
+
+ // Zap the underlying NoteRecord
+ sheet.getRecords().remove(comment.getNoteRecord());
+
+ // If we have a TextObjectRecord, is should
+ // be proceeed by:
+ // MSODRAWING with container
+ // OBJ
+ // MSODRAWING with EscherTextboxRecord
+ if(comment.getTextObjectRecord() != null) {
+ TextObjectRecord txo = comment.getTextObjectRecord();
+ int txoAt = sheet.getRecords().indexOf(txo);
+
+ if(sheet.getRecords().get(txoAt-3) instanceof DrawingRecord &&
+ sheet.getRecords().get(txoAt-2) instanceof ObjRecord &&
+ sheet.getRecords().get(txoAt-1) instanceof DrawingRecord) {
+ // Zap these, in reverse order
+ sheet.getRecords().remove(txoAt-1);
+ sheet.getRecords().remove(txoAt-2);
+ sheet.getRecords().remove(txoAt-3);
+ } else {
+ throw new IllegalStateException("Found the wrong records before the TextObjectRecord, can't remove comment");
+ }
+
+ // Now remove the text record
+ sheet.getRecords().remove(txo);
+ }
+ }
/**
* Cell comment finder.
}
}
+
+ public void testDeleteComments() throws Exception {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("SimpleWithComments.xls");
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ // Zap from rows 1 and 3
+ assertNotNull(sheet.getRow(0).getCell(1).getCellComment());
+ assertNotNull(sheet.getRow(1).getCell(1).getCellComment());
+ assertNotNull(sheet.getRow(2).getCell(1).getCellComment());
+
+ sheet.getRow(0).getCell(1).removeCellComment();
+ sheet.getRow(2).getCell(1).setCellComment(null);
+
+ // Check gone so far
+ assertNull(sheet.getRow(0).getCell(1).getCellComment());
+ assertNotNull(sheet.getRow(1).getCell(1).getCellComment());
+ assertNull(sheet.getRow(2).getCell(1).getCellComment());
+
+ // Save and re-load
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ wb.write(out);
+ out.close();
+ wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+
+ // Check
+ assertNull(sheet.getRow(0).getCell(1).getCellComment());
+ assertNotNull(sheet.getRow(1).getCell(1).getCellComment());
+ assertNull(sheet.getRow(2).getCell(1).getCellComment());
+
+// FileOutputStream fout = new FileOutputStream("/tmp/c.xls");
+// wb.write(fout);
+// fout.close();
+ }
}