]> source.dussan.org Git - poi.git/commitdiff
Bug 56914 - XSSFRowShifter.updateConditionalFormatting throws IOOBE when there are...
authorAndreas Beeker <kiwiwings@apache.org>
Fri, 5 Sep 2014 19:07:32 +0000 (19:07 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Fri, 5 Sep 2014 19:07:32 +0000 (19:07 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1622759 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheetConditionalFormatting.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index a9be492c4c030dfbcfb63359b61c89796683bbdf..8c83f6f164e3b21c93f48bcd1f453e4ea9e8cd85 100644 (file)
@@ -131,19 +131,13 @@ public class XSSFSheetConditionalFormatting implements SheetConditionalFormattin
         if (cfRules.length > 3) {\r
             throw new IllegalArgumentException("Number of rules must not exceed 3");\r
         }\r
-        XSSFConditionalFormattingRule[] hfRules;\r
-        if(cfRules instanceof XSSFConditionalFormattingRule[]) hfRules = (XSSFConditionalFormattingRule[])cfRules;\r
-        else {\r
-            hfRules = new XSSFConditionalFormattingRule[cfRules.length];\r
-            System.arraycopy(cfRules, 0, hfRules, 0, hfRules.length);\r
-        }\r
+\r
         CellRangeAddress[] mergeCellRanges = CellRangeUtil.mergeCellRanges(regions);\r
         CTConditionalFormatting cf = _sheet.getCTWorksheet().addNewConditionalFormatting();\r
         List<String> refs = new ArrayList<String>();\r
         for(CellRangeAddress a : mergeCellRanges) refs.add(a.formatAsString());\r
         cf.setSqref(refs);\r
 \r
-\r
         int priority = 1;\r
         for(CTConditionalFormatting c : _sheet.getCTWorksheet().getConditionalFormattingArray()){\r
             priority += c.sizeOfCfRuleArray();\r
index 9ec69d179526318868748cf4843329df674d1603..cfa0a77771ae8e2ad6729b8df30b0528ee9f1ef9 100644 (file)
@@ -226,12 +226,12 @@ public final class XSSFRowShifter {
         XSSFWorkbook wb = sheet.getWorkbook();
         int sheetIndex = wb.getSheetIndex(sheet);
 
-
         XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
         CTWorksheet ctWorksheet = sheet.getCTWorksheet();
-        int cfCount = ctWorksheet.sizeOfConditionalFormattingArray();
-        for(int j = 0; j< cfCount; j++){
-            CTConditionalFormatting cf = ctWorksheet.getConditionalFormattingArray(j);
+        CTConditionalFormatting[] conditionalFormattingArray = ctWorksheet.getConditionalFormattingArray();
+        // iterate backwards due to possible calls to ctWorksheet.removeConditionalFormatting(j)
+        for (int j = conditionalFormattingArray.length - 1; j >= 0; j--) {
+            CTConditionalFormatting cf = conditionalFormattingArray[j];
 
             ArrayList<CellRangeAddress> cellRanges = new ArrayList<CellRangeAddress>();
             for (Object stRef : cf.getSqref()) {
@@ -267,9 +267,9 @@ public final class XSSFRowShifter {
             }
 
             for(CTCfRule cfRule : cf.getCfRuleArray()){
-                int formulaCount = cfRule.sizeOfFormulaArray();
-                for (int i = 0; i < formulaCount; i++) {
-                    String formula = cfRule.getFormulaArray(i);
+                String[] formulaArray = cfRule.getFormulaArray();
+                for (int i = 0; i < formulaArray.length; i++) {
+                    String formula = formulaArray[i];
                     Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex);
                     if (shifter.adjustFormula(ptgs, sheetIndex)) {
                         String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
index 5c6af7b2611de6d02f2560a99bbaa0463c23a2d2..b6e6ca932e7285f5d24aaed7562380425b36a912 100644 (file)
@@ -390,7 +390,6 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
     }\r
 \r
     public void testShiftRows() {\r
-\r
         Workbook wb = _testDataProvider.createWorkbook();\r
         Sheet sheet = wb.createSheet();\r
 \r
@@ -403,30 +402,42 @@ public abstract class BaseTestConditionalFormatting extends TestCase {
 \r
         PatternFormatting patternFmt = rule1.createPatternFormatting();\r
         patternFmt.setFillBackgroundColor(IndexedColors.YELLOW.index);\r
-        ConditionalFormattingRule [] cfRules = { rule1, };\r
+\r
+        ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule(\r
+                ComparisonOperator.BETWEEN, "SUM(A10:A15)", "1+SUM(B16:B30)");\r
+        BorderFormatting borderFmt = rule2.createBorderFormatting();\r
+        borderFmt.setBorderDiagonal((short) 2);\r
 \r
         CellRangeAddress [] regions = {\r
             new CellRangeAddress(2, 4, 0, 0), // A3:A5\r
         };\r
-        sheetCF.addConditionalFormatting(regions, cfRules);\r
+        sheetCF.addConditionalFormatting(regions, rule1);\r
+        sheetCF.addConditionalFormatting(regions, rule2);\r
 \r
         // This row-shift should destroy the CF region\r
         sheet.shiftRows(10, 20, -9);\r
         assertEquals(0, sheetCF.getNumConditionalFormattings());\r
 \r
         // re-add the CF\r
-        sheetCF.addConditionalFormatting(regions, cfRules);\r
+        sheetCF.addConditionalFormatting(regions, rule1);\r
+        sheetCF.addConditionalFormatting(regions, rule2);\r
 \r
         // This row shift should only affect the formulas\r
         sheet.shiftRows(14, 17, 8);\r
-        ConditionalFormatting cf = sheetCF.getConditionalFormattingAt(0);\r
-        assertEquals("SUM(A10:A23)", cf.getRule(0).getFormula1());\r
-        assertEquals("1+SUM(B24:B30)", cf.getRule(0).getFormula2());\r
+        ConditionalFormatting cf1 = sheetCF.getConditionalFormattingAt(0);\r
+        assertEquals("SUM(A10:A23)", cf1.getRule(0).getFormula1());\r
+        assertEquals("1+SUM(B24:B30)", cf1.getRule(0).getFormula2());\r
+        ConditionalFormatting cf2 = sheetCF.getConditionalFormattingAt(1);\r
+        assertEquals("SUM(A10:A23)", cf2.getRule(0).getFormula1());\r
+        assertEquals("1+SUM(B24:B30)", cf2.getRule(0).getFormula2());\r
 \r
         sheet.shiftRows(0, 8, 21);\r
-        cf = sheetCF.getConditionalFormattingAt(0);\r
-        assertEquals("SUM(A10:A21)", cf.getRule(0).getFormula1());\r
-        assertEquals("1+SUM(#REF!)", cf.getRule(0).getFormula2());\r
+        cf1 = sheetCF.getConditionalFormattingAt(0);\r
+        assertEquals("SUM(A10:A21)", cf1.getRule(0).getFormula1());\r
+        assertEquals("1+SUM(#REF!)", cf1.getRule(0).getFormula2());\r
+        cf2 = sheetCF.getConditionalFormattingAt(1);\r
+        assertEquals("SUM(A10:A21)", cf2.getRule(0).getFormula1());\r
+        assertEquals("1+SUM(#REF!)", cf2.getRule(0).getFormula2());\r
     }\r
 \r
     protected void testRead(String filename){\r