]> source.dussan.org Git - poi.git/commitdiff
Patch from Yury, plus tests, from bug #45043 - Support for getting excel cell comment...
authorNick Burch <nick@apache.org>
Fri, 23 May 2008 15:22:18 +0000 (15:22 +0000)
committerNick Burch <nick@apache.org>
Fri, 23 May 2008 15:22:18 +0000 (15:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659572 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/changes.xml
src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java

index 320be03d33176d1db7c1506d7bceb3ae5c4c1f8d..21a6f6702c599c5a47cc29a4f1a730bd1847c172 100644 (file)
@@ -37,6 +37,7 @@
 
                <!-- Don't forget to update status.xml too! -->
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="add">45043 - Support for getting excel cell comments when extracting text</action>
            <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
            <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
            <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
index 5bd48a94a4c6d5bd2fdf1d2a66e75a689838da69..456ee3ebcae73f5468b871f82751be6f92dfbfe3 100644 (file)
@@ -34,6 +34,7 @@
        <!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="add">45043 - Support for getting excel cell comments when extracting text</action>
            <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
            <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
            <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
index 2a9c455caccd975696e7e98485a0878ea455e1d3..75a73c654d5c0fef29715de2ed5293a0e81616a7 100644 (file)
@@ -20,6 +20,7 @@ import java.io.IOException;
 
 import org.apache.poi.POIOLE2TextExtractor;
 import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFComment;
 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 import org.apache.poi.hssf.usermodel.HSSFRow;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
@@ -39,6 +40,7 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
        private HSSFWorkbook wb;
        private boolean includeSheetNames = true;
        private boolean formulasNotResults = false;
+       private boolean includeCellComments = false;
        
        public ExcelExtractor(HSSFWorkbook wb) {
                super(wb);
@@ -62,6 +64,12 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
        public void setFormulasNotResults(boolean formulasNotResults) {
                this.formulasNotResults = formulasNotResults;
        }
+       /**
+     * Should cell comments be included? Default is true
+     */
+    public void setIncludeCellComments(boolean includeCellComments) {
+        this.includeCellComments = includeCellComments;
+    }
        
        /**
         * Retreives the text contents of the file
@@ -128,6 +136,15 @@ public class ExcelExtractor extends POIOLE2TextExtractor {
                                                        break;
                                        }
                                        
+                                       // Output the comment, if requested and exists
+                                   HSSFComment comment = cell.getCellComment();
+                                       if(includeCellComments && comment != null) {
+                                           // Replace any newlines with spaces, otherwise it
+                                           //  breaks the output
+                                           String commentText = comment.getString().getString().replace('\n', ' ');
+                                           text.append(" Comment by "+comment.getAuthor()+": "+commentText);
+                                       }
+                                       
                                        // Output a tab if we're not on the last cell
                                        if(outputContents && k < (lastCell-1)) {
                                                text.append("\t");
index 63d67ee7716b21b499a85bede9e3e7480fe9a701..9bb137ff69cd1b9959344381a546a4508c6e18aa 100644 (file)
@@ -165,6 +165,28 @@ public final class TestExcelExtractor extends TestCase {
                );
        }
        
+       public void testWithComments() throws Exception {
+               ExcelExtractor extractor = createExtractor("SimpleWithComments.xls");
+               extractor.setIncludeSheetNames(false);
+
+               // Check without comments
+               assertEquals(
+                               "1.0\tone\n" +
+                               "2.0\ttwo\n" + 
+                               "3.0\tthree\n", 
+                               extractor.getText()
+               );
+               
+               // Now with
+               extractor.setIncludeCellComments(true);
+               assertEquals(
+                               "1.0\tone Comment by Yegor Kozlov: Yegor Kozlov: first cell\n" +
+                               "2.0\ttwo Comment by Yegor Kozlov: Yegor Kozlov: second cell\n" + 
+                               "3.0\tthree Comment by Yegor Kozlov: Yegor Kozlov: third cell\n", 
+                               extractor.getText()
+               );
+       }
+       
        
        /**
         * Embded in a non-excel file