This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new ba58012d27 FormatTrackingHSSFListener: optionally use file FORMAT 
records for built-in indexes (#1413)
ba58012d27 is described below

commit ba58012d2771b97c4c0117be2009dc9915a3fd77
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 16 19:33:04 2026 +0100

    FormatTrackingHSSFListener: optionally use file FORMAT records for built-in 
indexes (#1413)
    
    Adds setUseFileFormatsForBuiltinIndexes(boolean), off by default. When
    enabled, getFormatString returns the file's FORMAT record for a built-in
    index (e.g. locale-specific currency formats), as HSSFDataFormat does.
    
    Fixes #1409
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../eventusermodel/FormatTrackingHSSFListener.java | 43 +++++++++++++++++++++-
 .../TestFormatTrackingHSSFListener.java            | 22 +++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git 
a/poi/src/main/java/org/apache/poi/hssf/eventusermodel/FormatTrackingHSSFListener.java
 
b/poi/src/main/java/org/apache/poi/hssf/eventusermodel/FormatTrackingHSSFListener.java
index 7388fcfb39..598b9f33cf 100644
--- 
a/poi/src/main/java/org/apache/poi/hssf/eventusermodel/FormatTrackingHSSFListener.java
+++ 
b/poi/src/main/java/org/apache/poi/hssf/eventusermodel/FormatTrackingHSSFListener.java
@@ -49,6 +49,7 @@ public class FormatTrackingHSSFListener implements 
HSSFListener {
     private final NumberFormat _defaultFormat;
     private final Map<Integer, FormatRecord> _customFormatRecords = new 
HashMap<>();
     private final List<ExtendedFormatRecord> _xfRecords = new ArrayList<>();
+    private boolean _useFileFormatsForBuiltinIndexes;
 
     /**
      * Creates a format tracking wrapper around the given listener, using
@@ -74,6 +75,42 @@ public class FormatTrackingHSSFListener implements 
HSSFListener {
         _defaultFormat = NumberFormat.getInstance(locale);
     }
 
+    /**
+     * Whether format strings for built-in format indexes are taken from the
+     * FORMAT records in the file, where the file defines them.
+     *
+     * @return {@code true} if the file's FORMAT records take precedence over
+     * the built-in format strings
+     * @see #setUseFileFormatsForBuiltinIndexes(boolean)
+     * @since 6.0.0
+     */
+    public boolean isUseFileFormatsForBuiltinIndexes() {
+        return _useFileFormatsForBuiltinIndexes;
+    }
+
+    /**
+     * Sets whether format strings for built-in format indexes are taken from
+     * the FORMAT records in the file, where the file defines them.
+     * <p>
+     * Excel writes FORMAT records for some built-in indexes, and their content
+     * depends on the locale that Excel ran in (e.g. the currency formats 5-8,
+     * 42 and 44 use that locale's currency symbol). With this enabled,
+     * {@link #getFormatString(int)} returns the file's format string for such
+     * indexes, as {@link HSSFDataFormat#getFormat(short)} does. It falls back
+     * to {@link HSSFDataFormat#getBuiltinFormat(short)} when the file defines
+     * nothing for the index.
+     * <p>
+     * Defaults to {@code false}, which always returns the built-in format
+     * string for built-in indexes.
+     *
+     * @param useFileFormatsForBuiltinIndexes {@code true} to prefer the file's
+     * FORMAT records for built-in indexes
+     * @since 6.0.0
+     */
+    public void setUseFileFormatsForBuiltinIndexes(boolean 
useFileFormatsForBuiltinIndexes) {
+        _useFileFormatsForBuiltinIndexes = useFileFormatsForBuiltinIndexes;
+    }
+
     protected int getNumberOfCustomFormats() {
         return _customFormatRecords.size();
     }
@@ -145,6 +182,8 @@ public class FormatTrackingHSSFListener implements 
HSSFListener {
 
     /**
      * Returns the format string, eg $##.##, for the given number format index.
+     * <p>
+     * For built-in indexes, see {@link 
#setUseFileFormatsForBuiltinIndexes(boolean)}.
      *
      * @param formatIndex the format index
      *
@@ -160,7 +199,9 @@ public class FormatTrackingHSSFListener implements 
HSSFListener {
                 format = tfr.getFormatString();
             }
         } else {
-            format = HSSFDataFormat.getBuiltinFormat((short) formatIndex);
+            FormatRecord tfr = _useFileFormatsForBuiltinIndexes
+                    ? _customFormatRecords.get(Integer.valueOf(formatIndex)) : 
null;
+            format = tfr != null ? tfr.getFormatString() : 
HSSFDataFormat.getBuiltinFormat((short) formatIndex);
         }
         return format;
     }
diff --git 
a/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestFormatTrackingHSSFListener.java
 
b/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestFormatTrackingHSSFListener.java
index 3ef75d7f3d..c66ea2bbba 100644
--- 
a/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestFormatTrackingHSSFListener.java
+++ 
b/poi/src/test/java/org/apache/poi/hssf/eventusermodel/TestFormatTrackingHSSFListener.java
@@ -18,6 +18,7 @@
 package org.apache.poi.hssf.eventusermodel;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -28,8 +29,10 @@ import java.util.List;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.record.CellValueRecordInterface;
+import org.apache.poi.hssf.record.FormatRecord;
 import org.apache.poi.hssf.record.FormulaRecord;
 import org.apache.poi.hssf.record.NumberRecord;
+import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.junit.jupiter.api.Test;
 
@@ -63,6 +66,25 @@ final class TestFormatTrackingHSSFListener {
         assertEquals("_(\"$\"* #,##0.00_);_(\"$\"* (#,##0.00);_(\"$\"* 
\"-\"??_);_(@_)", listener.getFormatString(44));
     }
 
+    @Test
+    void testFileFormatsForBuiltinIndexes() {
+        listener = new FormatTrackingHSSFListener(new MockHSSFListener());
+        String yuan = "\"\uFFE5\"#,##0.00;\\-\"\uFFE5\"#,##0.00";
+        listener.processRecord(new FormatRecord(7, yuan));
+        listener.processRecord(new FormatRecord(23, "0.0%"));
+
+        assertFalse(listener.isUseFileFormatsForBuiltinIndexes());
+        assertEquals(HSSFDataFormat.getBuiltinFormat((short) 7), 
listener.getFormatString(7));
+        assertEquals(HSSFDataFormat.getBuiltinFormat((short) 23), 
listener.getFormatString(23));
+
+        listener.setUseFileFormatsForBuiltinIndexes(true);
+        assertTrue(listener.isUseFileFormatsForBuiltinIndexes());
+        assertEquals(yuan, listener.getFormatString(7));
+        assertEquals("0.0%", listener.getFormatString(23));
+        // no FORMAT record in the file, so the built-in format is used
+        assertEquals(HSSFDataFormat.getBuiltinFormat((short) 14), 
listener.getFormatString(14));
+    }
+
     /**
      * Ensure that all number and formula records can be
      *  turned into strings without problems.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to