poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r680530 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/hssf/usermodel/ testcases/org/apache/poi/hssf/usermodel/
Date Mon, 28 Jul 2008 22:10:09 GMT
Author: nick
Date: Mon Jul 28 15:10:07 2008
New Revision: 680530

URL: http://svn.apache.org/viewvc?rev=680530&view=rev
Log:
Some work on bug #45466 - Partial support for removing excel comments (won't work for all
excel versions yet)

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

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Mon Jul 28 15:10:07 2008
@@ -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>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Jul 28 15:10:07 2008
@@ -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>

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Mon Jul 28 15:10:07 2008
@@ -34,7 +34,24 @@
 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 @@
     }
 
     /**
-     * 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 @@
         }
         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.

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java Mon Jul 28 15:10:07
2008
@@ -159,4 +159,13 @@
         }
         super.setString(string);
     }
+    
+    /**
+     * Returns the underlying Note record
+     */
+    protected NoteRecord getNoteRecord() { return note; }
+    /**
+     * Returns the underlying Text record
+     */
+    protected TextObjectRecord getTextObjectRecord() { return txo; }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java Mon Jul 28
15:10:07 2008
@@ -156,4 +156,37 @@
         }
 
      }
+    
+    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();
+    }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java?rev=680530&r1=680529&r2=680530&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java Mon Jul
28 15:10:07 2008
@@ -19,6 +19,7 @@
 
 import java.text.DecimalFormat;
 import java.text.Format;
+import java.util.Date;
 import java.util.Iterator;
 
 import junit.framework.TestCase;



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message