]> source.dussan.org Git - poi.git/commitdiff
Some work on bug #45466 - Partial support for removing excel comments (won't work...
authorNick Burch <nick@apache.org>
Mon, 28 Jul 2008 22:10:07 +0000 (22:10 +0000)
committerNick Burch <nick@apache.org>
Mon, 28 Jul 2008 22:10:07 +0000 (22:10 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@680530 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java

index 30c53bb2132359f1a94994dec5c92cff1db40705..c1424c4d0497a4a187b4e1c31e52b3e78ee4b0b7 100644 (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>
index b81e839ef469a4c7fe1477342917c753bc36e4b7..8d8f567674f15f0dd0d52f97c40a6448b382fb93 100644 (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>
index 3e3dc05422158431ff1eb6a7c395bfa3a17d13bf..c4c9ea6190e46d8291388136da4afe985a77f284 100644 (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.
index 258f26e228dbe4769d51ef61bc76b62b685afdf3..f47286879d23b6aebcb829382902e5b1e10f41ae 100644 (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; }
 }
index 36e7942e1e45b8b5f00c68a5b87386db50b453a5..d3b8f5630f911cbb0dda3d8dcccb18724b6a5a08 100644 (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();
+    }
 }
index f865d6e494ed9f5c2833de32d55b18f0e84c819e..e24f30da6189447274a89e6c64e8db00e7fc3bd0 100644 (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;