From: Nick Burch Date: Mon, 14 Jun 2010 11:45:09 +0000 (+0000) Subject: When formatting numbers with DataFormatter, handle brackets following colours X-Git-Tag: REL_3_7_BETA1^2~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=36c5190f57c2dd388d22f9a2b1b5ffeeecde30c9;p=poi.git When formatting numbers with DataFormatter, handle brackets following colours git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@954416 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 16ca0807fc..71f0839156 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + When formatting numbers with DataFormatter, handle brackets following colours 48574 - further XWPF support for tables, paragraphs, including enhanced support for adding new ones diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 36ba5a1b6b..de586db66a 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -233,14 +233,14 @@ public class DataFormatter { int at = formatStr.indexOf(colour); if(at == -1) break; String nFormatStr = formatStr.substring(0,at) + - formatStr.substring(at+colour.length()+1); + formatStr.substring(at+colour.length()); if(nFormatStr.equals(formatStr)) break; // Try again in case there's multiple formatStr = nFormatStr; colourM = colorPattern.matcher(formatStr); } - + // try to extract special characters like currency Matcher m = specialPatternGroup.matcher(formatStr); while(m.find()) { diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index 35be8c4ff1..06abd6c502 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -77,6 +77,44 @@ public class TestDataFormatter extends TestCase { assertEquals("[ab]12.34[x]", dfUS.formatRawCellContents(12.343, -1, "[ab]##.##[x]")); } + public void testColoursAndBrackets() { + DataFormatter dfUS = new DataFormatter(Locale.US); + + // Without currency symbols + String[] formats = new String[] { + "#,##0.00;[Blue](#,##0.00)", + }; + for(String format : formats) { + assertEquals( + "Wrong format for: " + format, + "12.34", + dfUS.formatRawCellContents(12.343, -1, format) + ); + assertEquals( + "Wrong format for: " + format, + "(12.34)", + dfUS.formatRawCellContents(-12.343, -1, format) + ); + } + + // With + formats = new String[] { + "$#,##0.00;[Red]($#,##0.00)" + }; + for(String format : formats) { + assertEquals( + "Wrong format for: " + format, + "$12.34", + dfUS.formatRawCellContents(12.343, -1, format) + ); + assertEquals( + "Wrong format for: " + format, + "($12.34)", + dfUS.formatRawCellContents(-12.343, -1, format) + ); + } + } + /** * Test how we handle negative and zeros. * Note - some tests are disabled as DecimalFormat