Author: centic
Date: Thu Dec 30 23:03:40 2021
New Revision: 1896553

URL: http://svn.apache.org/viewvc?rev=1896553&view=rev
Log:
Bug 65312: Make RecordType.byName work for type "formula"

Also avoid NullPointerException when parsing unexpected record types
and check for bounds in byId()

Modified:
    
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java
    
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

Modified: 
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java?rev=1896553&r1=1896552&r2=1896553&view=diff
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java
 (original)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java
 Thu Dec 30 23:03:40 2021
@@ -19,7 +19,6 @@
 
 package org.apache.poi.ss.usermodel;
 
-
 /**
  * The Threshold / CFVO / Conditional Formatting Value Object.
  * <p>This defines how to calculate the ranges for a conditional
@@ -28,17 +27,27 @@ package org.apache.poi.ss.usermodel;
  */
 public interface ConditionalFormattingThreshold {
     enum RangeType {
+        // IDs should match the values documented
+        // in the spec for HSSF, e.g. at
+        // 
https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/3cc68999-c2fc-4a57-92a5-94c0720779e9
+
         /** Number / Parameter */
         NUMBER(1, "num"),
+
         /** The minimum value from the range */
         MIN(2, "min"),
+
         /** The maximum value from the range */
         MAX(3, "max"),
+
         /** Percent of the way from the mi to the max value in the range */
         PERCENT(4, "percent"),
+
         /** The minimum value of the cell that is in X percentile of the range 
*/
         PERCENTILE(5, "percentile"),
+
         UNALLOCATED(6, null),
+
         /** Formula result */
         FORMULA(7, "formula");
 
@@ -52,12 +61,23 @@ public interface ConditionalFormattingTh
         }
 
         public static RangeType byId(int id) {
+            // id is mapped to ordinal()+1
+            if (id <= 0 || id > values().length) {
+                return null;
+            }
+
             return values()[id-1]; // 1-based IDs
         }
+
         public static RangeType byName(String name) {
             for (RangeType t : values()) {
-                if (t.name.equals(name)) return t;
+                if (t.name == null && name == null) {
+                    return t;
+                } else if (t.name != null && t.name.equals(name)) {
+                    return t;
+                }
             }
+
             return null;
         }
 

Modified: 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java?rev=1896553&r1=1896552&r2=1896553&view=diff
==============================================================================
--- 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java
 (original)
+++ 
poi/trunk/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java
 Thu Dec 30 23:03:40 2021
@@ -1385,4 +1385,19 @@ public abstract class BaseTestConditiona
             }
         }
     }
+
+    @Test
+    void testRangeType() {
+        for (RangeType rangeType : RangeType.values()) {
+            assertEquals(rangeType, RangeType.byName(rangeType.name));
+            assertEquals(rangeType, RangeType.byId(rangeType.id));
+        }
+
+        assertEquals(RangeType.UNALLOCATED, RangeType.byName(null));
+        assertNull(RangeType.byName("some other name"));
+
+        assertNull(RangeType.byId(-1));
+        assertNull(RangeType.byId(0));
+        assertNull(RangeType.byId(99));
+    }
 }
\ No newline at end of file



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

Reply via email to