From fd6ccbdf14cf5c24f956ae69fcf3d363445edf13 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 30 Dec 2021 23:03:40 +0000 Subject: [PATCH] Bug 65312: Make RecordType.byName work for type "formula" Also avoid NullPointerException when parsing unexpected record types and check for bounds in byId() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896553 13f79535-47bb-0310-9956-ffa450edef68 --- .../ConditionalFormattingThreshold.java | 24 +++++++++++++++++-- .../BaseTestConditionalFormatting.java | 15 ++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java b/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java index 69cc1887c5..00b8f89ddc 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java @@ -19,7 +19,6 @@ package org.apache.poi.ss.usermodel; - /** * The Threshold / CFVO / Conditional Formatting Value Object. *

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 ConditionalFormattingThreshold { } 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; } diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index eec63048e6..e58138c663 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -1385,4 +1385,19 @@ public abstract class BaseTestConditionalFormatting { } } } + + @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 -- 2.39.5