You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2015/12/29 01:12:04 UTC

svn commit: r1722043 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/model/StylesTable.java java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java

Author: onealj
Date: Tue Dec 29 00:12:04 2015
New Revision: 1722043

URL: http://svn.apache.org/viewvc?rev=1722043&view=rev
Log:
bug 58778: override a built-in number format, such as using £ instead of $ for currency

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java?rev=1722043&r1=1722042&r2=1722043&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java Tue Dec 29 00:12:04 2015
@@ -234,7 +234,7 @@ public class StylesTable extends POIXMLD
             throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
         }
         
-        if (numberFormats.size() > MAXIMUM_NUMBER_OF_DATA_FORMATS) {
+        if (numberFormats.size() >= MAXIMUM_NUMBER_OF_DATA_FORMATS) {
             throw new IllegalStateException("The maximum number of Data Formats was exceeded. " +
                     "You can define up to " + MAXIMUM_NUMBER_OF_DATA_FORMATS + " formats in a .xlsx Workbook.");
         }
@@ -256,6 +256,20 @@ public class StylesTable extends POIXMLD
         numberFormats.put(formatIndex, fmt);
         return formatIndex;
     }
+    
+    
+    /**
+     * Add a number format with a specific ID into the numberFormats map.
+     * If a format with the same ID already exists, overwrite the format code
+     * with <code>fmt</code>
+     * This may be used to override built-in number formats.
+     *
+     * @param index the number format ID
+     * @param fmt the number format code
+     */
+    public void putNumberFormat(short index, String fmt) {
+        numberFormats.put((int)index, fmt);
+    }
 
     public XSSFFont getFontAt(int idx) {
         return fonts.get(idx);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java?rev=1722043&r1=1722042&r2=1722043&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDataFormat.java Tue Dec 29 00:12:04 2015
@@ -78,4 +78,17 @@ public class XSSFDataFormat implements D
         if(fmt == null) fmt = BuiltinFormats.getBuiltinFormat(index);
         return fmt;
     }
+    
+    /**
+     * Add a number format with a specific ID into the number format style table.
+     * If a format with the same ID already exists, overwrite the format code
+     * with <code>fmt</code>
+     * This may be used to override built-in number formats.
+     *
+     * @param index the number format ID
+     * @param format the number format code
+     */
+    public void putFormat(short index, String format) {
+        stylesSource.putNumberFormat(index, format);
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java?rev=1722043&r1=1722042&r2=1722043&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDataFormat.java Tue Dec 29 00:12:04 2015
@@ -17,8 +17,12 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.io.IOException;
+
 import org.apache.poi.ss.usermodel.BaseTestDataFormat;
 import org.apache.poi.ss.usermodel.BuiltinFormats;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.DataFormat;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -62,4 +66,31 @@ public final class TestXSSFDataFormat ex
         XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("FormatKM.xlsx");
         doTest58532Core(wb);
     }
+    
+    public void test58778() throws IOException {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        Cell cell = wb.createSheet("bug58778").createRow(0).createCell(0);
+        cell.setCellValue(5.25);
+        CellStyle style = wb.createCellStyle();
+        
+        XSSFDataFormat dataFormat = wb.createDataFormat();
+        
+        short poundFmtIdx = 6;
+        dataFormat.putFormat(poundFmtIdx, poundFmt);
+        style.setDataFormat(poundFmtIdx);
+        cell.setCellStyle(style);
+        // Cell should appear as "<poundsymbol>5"
+        
+        wb = XSSFTestDataSamples.writeOutCloseAndReadBack(wb);
+        cell = wb.getSheet("bug58778").getRow(0).getCell(0);
+        assertEquals(5.25, cell.getNumericCellValue());
+        
+        style = cell.getCellStyle();
+        assertEquals(poundFmt, style.getDataFormatString());
+        assertEquals(poundFmtIdx, style.getDataFormat());
+        
+        // manually check the file to make sure the cell is rendered as "<poundsymbol>5"
+        // Verified with LibreOffice 4.2.8.2 on 2015-12-28
+        wb.close();
+    }
 }



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