1. Alex Stapleton
  2. openpyxl

Commits

Charlie Clark  committed e6a1819 Draft

Alternative implementation for fixing rule priorities.

  • Participants
  • Parent commits 5dedbcd
  • Branches CF improvements

Comments (0)

Files changed (2)

File openpyxl/formatting/__init__.py

View file
 from .rules import CellIsRule, ColorScaleRule, FormatRule
 
 
+
+def unpack_rules(cfRules):
+    for key, rules in iteritems(cfRules):
+        for idx,rule in enumerate(rules):
+            yield (key, idx, rule['priority'])
+
+
 class ConditionalFormatting(object):
     """Conditional formatting rules."""
     rule_attributes = ('aboveAverage', 'bottom', 'dxfId', 'equalAverage', 'operator', 'percent', 'priority', 'rank',
             self.cf_rules[range_string] = []
         self.cf_rules[range_string].append(rule)
 
+
+    def _fix_priorities(self):
+        rules = unpack_rules(self.cf_rules)
+        rules = sorted(rules, key=lambda x: x[2])
+        for idx, (key, rule_no, prio) in enumerate(rules):
+            self.cf_rules[key][rule_no]['priority']= idx + 1
+        self.max_priority = len(rules)
+
+
     def update(self, cfRules):
         """Set the conditional formatting rules from a dictionary.  Intended for use when loading a document.
         cfRules use the structure: {range_string: [rule1, rule2]}, eg:

File openpyxl/formatting/tests/test_formatting.py

View file
                     )
 
 
-def unpack_rules(cfRules):
-    for key, rules in iteritems(cfRules):
-        for idx,rule in enumerate(rules):
-            yield (key, idx, rule['priority'])
-
-
-
-def test_fix_priorities():
+def test_unpack_rules():
+    from openpyxl.formatting import unpack_rules
     assert list(unpack_rules(rules)) == [
         ('H1:H10', 0, 23),
         ('Q1:Q10', 0, 14),
         ('S1:S10', 0, 12),
         ('D1:D10', 0, 27),
     ]
+
+
+def test_update():
+    from openpyxl.formatting import unpack_rules
     from openpyxl.formatting import ConditionalFormatting
     cf = ConditionalFormatting()
     cf.update(rules)
         ('S1:S10', 0, 10),
         ('D1:D10', 0, 22),
     ]
+
+
+def test_fix_priorities():
+    from openpyxl.formatting import unpack_rules
+    from openpyxl.formatting import ConditionalFormatting
+    cf = ConditionalFormatting()
+    cf.cf_rules = rules
+    cf._fix_priorities()
+    assert list(unpack_rules(cf.cf_rules)) == [
+        ('H1:H10', 0, 18),
+        ('Q1:Q10', 0, 12),
+        ('G1:G10', 0, 19),
+        ('F1:F10', 0, 20),
+        ('O1:O10', 0, 14),
+        ('T1:T10', 0, 9),
+        ('X1:X10', 0, 6),
+        ('R1:R10', 0, 11),
+        ('C1:C10', 0, 23),
+        ('J1:J10', 0, 16),
+        ('E1:E10', 0, 21),
+        ('I1:I10', 0, 17),
+        ('Z1:Z10', 0, 4),
+        ('V1:V10', 0, 8),
+        ('AC1:AC10', 0, 1),
+        ('N1:N10', 0, 15),
+        ('AA1:AA10', 0, 3),
+        ('Y1:Y10', 0, 5),
+        ('B1:B10', 0, 24),
+        ('P1:P10', 0, 13),
+        ('W1:W10', 0, 7),
+        ('AB1:AB10', 0, 2),
+        ('A1:A1048576', 0, 25),
+        ('S1:S10', 0, 10),
+        ('D1:D10', 0, 22),
+    ]
+