Some work on bug #45466 - Partial support for removing excel comments (won't work for all excel versions yet)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@680530 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-07-28 22:10:07 +00:00
parent bc0461c5ed
commit 7fd95e217f
6 changed files with 114 additions and 2 deletions

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">45466 - Partial support for removing excel comments (won't work for all excel versions yet)</action>
<action dev="POI-DEVELOPERS" type="fix">45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM</action>
<action dev="POI-DEVELOPERS" type="add">45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does</action>
<action dev="POI-DEVELOPERS" type="fix">45414 - Don't add too many UncalcedRecords to sheets with charts in them</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1.1-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="add">45466 - Partial support for removing excel comments (won't work for all excel versions yet)</action>
<action dev="POI-DEVELOPERS" type="fix">45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM</action>
<action dev="POI-DEVELOPERS" type="add">45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does</action>
<action dev="POI-DEVELOPERS" type="fix">45414 - Don't add too many UncalcedRecords to sheets with charts in them</action>

View File

@ -34,7 +34,24 @@ import java.util.Iterator;
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;
@ -1045,11 +1062,18 @@ public class HSSFCell
}
/**
* 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;
@ -1066,6 +1090,49 @@ public class HSSFCell
}
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.

View File

@ -159,4 +159,13 @@ public class HSSFComment extends HSSFTextbox {
}
super.setString(string);
}
/**
* Returns the underlying Note record
*/
protected NoteRecord getNoteRecord() { return note; }
/**
* Returns the underlying Text record
*/
protected TextObjectRecord getTextObjectRecord() { return txo; }
}

View File

@ -156,4 +156,37 @@ public final class TestHSSFComment extends TestCase {
}
}
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();
}
}

View File

@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;
import java.util.Iterator;
import junit.framework.TestCase;