From f601b0899aee3b5fe256675120f86ad62d447b1d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 23 May 2008 15:22:18 +0000 Subject: [PATCH] Patch from Yury, plus tests, from bug #45043 - Support for getting excel cell comments when extracting text git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659572 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/extractor/ExcelExtractor.java | 17 ++++++++++++++ .../hssf/extractor/TestExcelExtractor.java | 22 +++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 320be03d3..21a6f6702 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45043 - Support for getting excel cell comments when extracting text 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 45025 - improved FormulaParser parse error messages 45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5bd48a94a..456ee3ebc 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45043 - Support for getting excel cell comments when extracting text 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 45025 - improved FormulaParser parse error messages 45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable diff --git a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index 2a9c455ca..75a73c654 100644 --- a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -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"); diff --git a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java index 63d67ee77..9bb137ff6 100644 --- a/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java +++ b/src/testcases/org/apache/poi/hssf/extractor/TestExcelExtractor.java @@ -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