]> source.dussan.org Git - poi.git/commitdiff
When formatting numbers with DataFormatter, handle brackets following colours
authorNick Burch <nick@apache.org>
Mon, 14 Jun 2010 11:45:09 +0000 (11:45 +0000)
committerNick Burch <nick@apache.org>
Mon, 14 Jun 2010 11:45:09 +0000 (11:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@954416 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

index 16ca0807fc5ab50d94729f6cd322061b2bd1c091..71f08391563429b0ccc8c2bdda7f0efd57ad393a 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta2" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">When formatting numbers with DataFormatter, handle brackets following colours</action>
         </release>
         <release version="3.7-beta1" date="2010-06-16">
            <action dev="POI-DEVELOPERS" type="add">48574 - further XWPF support for tables, paragraphs, including enhanced support for adding new ones</action>
index 36ba5a1b6bbe0f50d374b69b52b9e45705c1e3cb..de586db66abe2c69e99df6816ecc749dc62c8017 100644 (file)
@@ -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()) {
index 35be8c4ff15f18b3a6cd28c437cb610f5144a6db..06abd6c5027e38cf75e8ab38fc13d433358abc86 100644 (file)
@@ -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