]> source.dussan.org Git - poi.git/commitdiff
bug 61474, github #81: implement updateConditionalFormatting for ColumnShifter
authorJaven O'Neal <onealj@apache.org>
Sat, 4 Nov 2017 07:58:41 +0000 (07:58 +0000)
committerJaven O'Neal <onealj@apache.org>
Sat, 4 Nov 2017 07:58:41 +0000 (07:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814259 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFColumnShifter.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowColShifter.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java

index b6643541ae532fa28783ca5e519fe81f22cf968f..b6c23b6c867d8e69dd02f1668b53e80118c2040b 100644 (file)
@@ -61,10 +61,9 @@ public final class XSSFColumnShifter extends ColumnShifter {
         throw new NotImplementedException("updateSheetFormulas");
     }
 
-    @NotImplemented
     @Override
     public void updateConditionalFormatting(FormulaShifter formulaShifter) {
-        throw new NotImplementedException("updateConditionalformatting");
+        XSSFRowColShifter.updateConditionalFormatting(sheet, formulaShifter);
     }
     
     /**
index 399363185c1e8043c0dab76ace5b10dee12f3dfd..4b165ba6233dedbceb7b2cc17efcf5bb46c00ae7 100644 (file)
@@ -26,7 +26,11 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.usermodel.*;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -57,6 +61,67 @@ import java.util.List;
         }
     }
 
+    /*package*/ static void updateConditionalFormatting(Sheet sheet, FormulaShifter formulaShifter) {
+        XSSFSheet xsheet = (XSSFSheet) sheet;
+        XSSFWorkbook wb = xsheet.getWorkbook();
+        int sheetIndex = wb.getSheetIndex(sheet);
+        final int rowIndex = -1; //don't care, structured references not allowed in conditional formatting
+
+        XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
+        CTWorksheet ctWorksheet = xsheet.getCTWorksheet();
+        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<>();
+            for (Object stRef : cf.getSqref()) {
+                String[] regions = stRef.toString().split(" ");
+                for (String region : regions) {
+                    cellRanges.add(CellRangeAddress.valueOf(region));
+                }
+            }
+
+            boolean changed = false;
+            List<CellRangeAddress> temp = new ArrayList<>();
+            for (CellRangeAddress craOld : cellRanges) {
+                CellRangeAddress craNew = shiftRange(formulaShifter, craOld, sheetIndex);
+                if (craNew == null) {
+                    changed = true;
+                    continue;
+                }
+                temp.add(craNew);
+                if (craNew != craOld) {
+                    changed = true;
+                }
+            }
+
+            if (changed) {
+                int nRanges = temp.size();
+                if (nRanges == 0) {
+                    ctWorksheet.removeConditionalFormatting(j);
+                    continue;
+                }
+                List<String> refs = new ArrayList<>();
+                for(CellRangeAddress a : temp) refs.add(a.formatAsString());
+                cf.setSqref(refs);
+            }
+
+            for(CTCfRule cfRule : cf.getCfRuleArray()){
+                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, rowIndex);
+                    if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
+                        String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
+                        cfRule.setFormulaArray(i, shiftedFmla);
+                    }
+                }
+            }
+        }
+    }
+
+
     /*package*/ static void updateHyperlinks(Sheet sheet, FormulaShifter formulaShifter) {
         int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
         List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList();
index 402bd612e8e84bf19a53c62616d12a214c25c0a2..c32c98db0efb8422aa7dc3c28b5171ecda9c7a66 100644 (file)
@@ -169,63 +169,7 @@ public final class XSSFRowShifter extends RowShifter {
 
     @Override
     public void updateConditionalFormatting(FormulaShifter formulaShifter) {
-        XSSFSheet xsheet = (XSSFSheet) sheet;
-        XSSFWorkbook wb = xsheet.getWorkbook();
-        int sheetIndex = wb.getSheetIndex(sheet);
-        final int rowIndex = -1; //don't care, structured references not allowed in conditional formatting
-
-        XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
-        CTWorksheet ctWorksheet = xsheet.getCTWorksheet();
-        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<>();
-            for (Object stRef : cf.getSqref()) {
-                String[] regions = stRef.toString().split(" ");
-                for (String region : regions) {
-                    cellRanges.add(CellRangeAddress.valueOf(region));
-                }
-            }
-
-            boolean changed = false;
-            List<CellRangeAddress> temp = new ArrayList<>();
-            for (CellRangeAddress craOld : cellRanges) {
-                CellRangeAddress craNew = shiftRange(formulaShifter, craOld, sheetIndex);
-                if (craNew == null) {
-                    changed = true;
-                    continue;
-                }
-                temp.add(craNew);
-                if (craNew != craOld) {
-                    changed = true;
-                }
-            }
-
-            if (changed) {
-                int nRanges = temp.size();
-                if (nRanges == 0) {
-                    ctWorksheet.removeConditionalFormatting(j);
-                    continue;
-                }
-                List<String> refs = new ArrayList<>();
-                for(CellRangeAddress a : temp) refs.add(a.formatAsString());
-                cf.setSqref(refs);
-            }
-
-            for(CTCfRule cfRule : cf.getCfRuleArray()){
-                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, rowIndex);
-                    if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
-                        String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
-                        cfRule.setFormulaArray(i, shiftedFmla);
-                    }
-                }
-            }
-        }
+        XSSFRowColShifter.updateConditionalFormatting(sheet, formulaShifter);
     }
     
     /**