From: Nick Burch Date: Mon, 28 Jul 2008 22:10:07 +0000 (+0000) Subject: Some work on bug #45466 - Partial support for removing excel comments (won't work... X-Git-Tag: REL_3_2_FINAL~226 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9bc7ef735abfa618ea449e2c1ead1b1bd81f3c46;p=poi.git 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 --- diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 30c53bb213..c1424c4d04 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45466 - Partial support for removing excel comments (won't work for all excel versions yet) 45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM 45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does 45414 - Don't add too many UncalcedRecords to sheets with charts in them diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b81e839ef4..8d8f567674 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45466 - Partial support for removing excel comments (won't work for all excel versions yet) 45437 - Detect encrypted word documents, and throw an EncryptedDocumentException instead of a OOM 45404 - New class, hssf.usermodel.HSSFDataFormatter, for formatting numbers and dates in the same way that Excel does 45414 - Don't add too many UncalcedRecords to sheets with charts in them diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 3e3dc05422..c4c9ea6190 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -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. diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java index 258f26e228..f47286879d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java @@ -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; } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java index 36e7942e1e..d3b8f5630f 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java @@ -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(); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java index f865d6e494..e24f30da61 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java @@ -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;