Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/openpyxl/formatting/formatting.py: 42%

60 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# Copyright (c) 2010-2022 openpyxl 

2 

3from collections import OrderedDict 

4 

5from openpyxl.descriptors import ( 

6 Bool, 

7 String, 

8 Sequence, 

9 Alias, 

10 Convertible, 

11) 

12from openpyxl.descriptors.excel import ExtensionList 

13from openpyxl.descriptors.serialisable import Serialisable 

14 

15from .rule import Rule 

16 

17from openpyxl.worksheet.cell_range import MultiCellRange 

18 

19class ConditionalFormatting(Serialisable): 

20 

21 tagname = "conditionalFormatting" 

22 

23 sqref = Convertible(expected_type=MultiCellRange) 

24 cells = Alias("sqref") 

25 pivot = Bool(allow_none=True) 

26 cfRule = Sequence(expected_type=Rule) 

27 rules = Alias("cfRule") 

28 

29 

30 def __init__(self, sqref=(), pivot=None, cfRule=(), extLst=None): 

31 self.sqref = sqref 

32 self.pivot = pivot 

33 self.cfRule = cfRule 

34 

35 

36 def __eq__(self, other): 

37 if not isinstance(other, self.__class__): 

38 return False 

39 return self.sqref == other.sqref 

40 

41 

42 def __hash__(self): 

43 return hash(str(self.sqref)) 

44 

45 

46 def __repr__(self): 

47 return "<{cls} {cells}>".format(cls=self.__class__.__name__, cells=self.sqref) 

48 

49 

50 def __contains__(self, coord): 

51 """ 

52 Check whether a certain cell is affected by the formatting 

53 """ 

54 return coord in self.sqref 

55 

56 

57class ConditionalFormattingList(object): 

58 """Conditional formatting rules.""" 

59 

60 

61 def __init__(self): 

62 self._cf_rules = OrderedDict() 

63 self.max_priority = 0 

64 

65 

66 def add(self, range_string, cfRule): 

67 """Add a rule such as ColorScaleRule, FormulaRule or CellIsRule 

68 

69 The priority will be added automatically. 

70 """ 

71 cf = range_string 

72 if isinstance(range_string, str): 

73 cf = ConditionalFormatting(range_string) 

74 if not isinstance(cfRule, Rule): 

75 raise ValueError("Only instances of openpyxl.formatting.rule.Rule may be added") 

76 rule = cfRule 

77 self.max_priority += 1 

78 if not rule.priority: 

79 rule.priority = self.max_priority 

80 

81 self._cf_rules.setdefault(cf, []).append(rule) 

82 

83 

84 def __bool__(self): 

85 return bool(self._cf_rules) 

86 

87 __nonzero = __bool__ 

88 

89 

90 def __len__(self): 

91 return len(self._cf_rules) 

92 

93 

94 def __iter__(self): 

95 for cf, rules in self._cf_rules.items(): 

96 cf.rules = rules 

97 yield cf 

98 

99 

100 def __getitem__(self, key): 

101 """ 

102 Get the rules for a cell range 

103 """ 

104 if isinstance(key, str): 

105 key = ConditionalFormatting(sqref=key) 

106 return self._cf_rules[key] 

107 

108 

109 def __delitem__(self, key): 

110 key = ConditionalFormatting(sqref=key) 

111 del self._cf_rules[key] 

112 

113 

114 def __setitem__(self, key, rule): 

115 """ 

116 Add a rule for a cell range 

117 """ 

118 self.add(key, rule)