From 1bf21ef3f93280b669c331d16be5003a77011893 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 2 May 2014 15:49:23 +0000 Subject: [PATCH] Excel 2007 and later allows for more than 3 Conditional Formatting rules per sheet, so change our hard limit to logging a compatibility warning. Test file from Pau Coma Ramirez from bug #56482 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1591959 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/aggregates/CFRecordsAggregate.java | 31 +++++++++++------- .../apache/poi/hssf/usermodel/TestBugs.java | 14 ++++++++ test-data/spreadsheet/56482.xls | Bin 0 -> 10240 bytes 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 test-data/spreadsheet/56482.xls diff --git a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java index 03f9787571..d9d64123a6 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java @@ -29,15 +29,21 @@ import org.apache.poi.ss.formula.ptg.AreaErrPtg; import org.apache.poi.ss.formula.ptg.AreaPtg; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** - * CFRecordsAggregate - aggregates Conditional Formatting records CFHeaderRecord - * and number of up to three CFRuleRecord records together to simplify - * access to them. + *

CFRecordsAggregate - aggregates Conditional Formatting records CFHeaderRecord + * and number of up CFRuleRecord records together to simplify access to them.

+ *

Note that Excel versions before 2007 can only cope with a maximum of 3 + * Conditional Formatting rules per sheet. Excel 2007 or newer can cope with + * unlimited numbers, as can Apache OpenOffice. This is an Excel limitation, + * not a file format one.

*/ public final class CFRecordsAggregate extends RecordAggregate { - /** Excel allows up to 3 conditional formating rules */ - private static final int MAX_CONDTIONAL_FORMAT_RULES = 3; + /** Excel 97-2003 allows up to 3 conditional formating rules */ + private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3; + private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class); private final CFHeaderRecord header; @@ -51,9 +57,11 @@ public final class CFRecordsAggregate extends RecordAggregate { if(pRules == null) { throw new IllegalArgumentException("rules must not be null"); } - if(pRules.length > MAX_CONDTIONAL_FORMAT_RULES) { - throw new IllegalArgumentException("No more than " - + MAX_CONDTIONAL_FORMAT_RULES + " rules may be specified"); + if(pRules.length > MAX_97_2003_CONDTIONAL_FORMAT_RULES) { + logger.log(POILogger.WARN, "Excel versions before 2007 require that " + + "No more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES + + " rules may be specified, " + pRules.length + " were found," + + " this file will cause problems with old Excel versions"); } if (pRules.length != pHeader.getNumberOfConditionalFormats()) { throw new RuntimeException("Mismatch number of rules"); @@ -134,9 +142,10 @@ public final class CFRecordsAggregate extends RecordAggregate { if (r == null) { throw new IllegalArgumentException("r must not be null"); } - if(rules.size() >= MAX_CONDTIONAL_FORMAT_RULES) { - throw new IllegalStateException("Cannot have more than " - + MAX_CONDTIONAL_FORMAT_RULES + " conditional format rules"); + if(rules.size() >= MAX_97_2003_CONDTIONAL_FORMAT_RULES) { + logger.log(POILogger.WARN, "Excel versions before 2007 cannot cope with" + + " any more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES + + " - this file will cause problems with old Excel versions"); } rules.add(r); header.setNumberOfConditionalFormats(rules.size()); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 16d8e208b6..197531965d 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -2565,4 +2565,18 @@ public final class TestBugs extends BaseTestBugzillaIssues { } assertEquals(0, comments); } + + /** + * Files initially created with Excel 2010 can have >3 CF rules + */ + @Test + public void bug56482() { + HSSFWorkbook wb = openSample("56482.xls"); + assertEquals(1, wb.getNumberOfSheets()); + + HSSFSheet sheet = wb.getSheetAt(0); + HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting(); + + assertEquals(5, cf.getNumConditionalFormattings()); + } } diff --git a/test-data/spreadsheet/56482.xls b/test-data/spreadsheet/56482.xls new file mode 100644 index 0000000000000000000000000000000000000000..466aa19de9f51adc69aeadb7fb5dd894e71a5474 GIT binary patch literal 10240 zcmeHNTWnNC82)E(-O|!-3rn>`vNpQhii&GujRMQ1Duzo_FcL$e+j6#4wq3H6Ac@8W zB=Nx}C{nTsi6P;|OO!^VF`Br<7arDNOi0iN1HKV`Sd2zVfaCYgIm@0dyRuxOE$*EB zbNOcGpMUoF0r~d2xaO6I(d^tTwlh?L?PF!-_a{{r* z&!Ek+oxWjZEYTnOx55f7LrhVIEN(JOkW+F5Xp?-b(XVv8MCg4Gq7v%T86}5MV82lv zcj&kYv69bqpC2`{SlZ#H*HL{|`z`5>ir&2Dhtrj;B$M(Z4>`y{@}= z`I@sb6WouDL%gf z-dO=(Pyw$mi*wzzJiOKFkPRJl!Pe4ZG7Y5NI&De&blRHs=yXQ9PNy@|Uh{3(sI~Lq z1wKq46c5Iq9Q zXaW{NUjQ#^%iRy_Kfogj{M3f6hB31EP4P$vt`o`+64^Ma$dBO4H%(t&ptiC>pBiY z7%UpX`DJq*`FY6iTXbgcqBHqr^Bwu4SipKrQR!>1bgE8O=wC>cJPY2_dIjLG8m8ABJC5#XvB@eUo!6WGFVd?tho#pD1)^^s|_V%%zz}N7;jUh824@|HmeK< z)6LxB0>$Yj=FW0 zSVw)ZjykqVze*3?MgD0Za20vF2EDD&v{~Hwhfr=7Pkx@p>+_bqpx_9wHQkTw>FSoc zTIDl%G?6GHJKRHlUflraCtltGc*=pIGs3hKkEiT#T=8*%l2B$#CM!3t#zeW<%lxM6 z5nXvp(Mh|vKH4E)kcVDEYoZKy2k!a8+vc^L`nY-rl5ygbL$W8lZEoIl#?>Ln#WVZl zkZZ25&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