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

168 statements  

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

1# Copyright (c) 2010-2022 openpyxl 

2 

3 

4from openpyxl.descriptors.serialisable import Serialisable 

5from openpyxl.descriptors import ( 

6 Alias, 

7 Typed, 

8 Set, 

9 Float, 

10 DateTime, 

11 NoneSet, 

12 Bool, 

13 Integer, 

14 String, 

15 Sequence, 

16 MinMax, 

17) 

18from openpyxl.descriptors.excel import ExtensionList, CellRange 

19from openpyxl.descriptors.sequence import ValueSequence 

20 

21 

22class SortCondition(Serialisable): 

23 

24 tagname = "sortCondition" 

25 

26 descending = Bool(allow_none=True) 

27 sortBy = NoneSet(values=(['value', 'cellColor', 'fontColor', 'icon'])) 

28 ref = CellRange() 

29 customList = String(allow_none=True) 

30 dxfId = Integer(allow_none=True) 

31 iconSet = NoneSet(values=(['3Arrows', '3ArrowsGray', '3Flags', 

32 '3TrafficLights1', '3TrafficLights2', '3Signs', '3Symbols', '3Symbols2', 

33 '4Arrows', '4ArrowsGray', '4RedToBlack', '4Rating', '4TrafficLights', 

34 '5Arrows', '5ArrowsGray', '5Rating', '5Quarters'])) 

35 iconId = Integer(allow_none=True) 

36 

37 def __init__(self, 

38 ref=None, 

39 descending=None, 

40 sortBy=None, 

41 customList=None, 

42 dxfId=None, 

43 iconSet=None, 

44 iconId=None, 

45 ): 

46 self.descending = descending 

47 self.sortBy = sortBy 

48 self.ref = ref 

49 self.customList = customList 

50 self.dxfId = dxfId 

51 self.iconSet = iconSet 

52 self.iconId = iconId 

53 

54 

55class SortState(Serialisable): 

56 

57 tagname = "sortState" 

58 

59 columnSort = Bool(allow_none=True) 

60 caseSensitive = Bool(allow_none=True) 

61 sortMethod = NoneSet(values=(['stroke', 'pinYin'])) 

62 ref = CellRange() 

63 sortCondition = Sequence(expected_type=SortCondition, allow_none=True) 

64 extLst = Typed(expected_type=ExtensionList, allow_none=True) 

65 

66 __elements__ = ('sortCondition',) 

67 

68 def __init__(self, 

69 columnSort=None, 

70 caseSensitive=None, 

71 sortMethod=None, 

72 ref=None, 

73 sortCondition=(), 

74 extLst=None, 

75 ): 

76 self.columnSort = columnSort 

77 self.caseSensitive = caseSensitive 

78 self.sortMethod = sortMethod 

79 self.ref = ref 

80 self.sortCondition = sortCondition 

81 

82 

83 def __bool__(self): 

84 return self.ref is not None 

85 

86 

87 

88class IconFilter(Serialisable): 

89 

90 tagname = "iconFilter" 

91 

92 iconSet = Set(values=(['3Arrows', '3ArrowsGray', '3Flags', 

93 '3TrafficLights1', '3TrafficLights2', '3Signs', '3Symbols', '3Symbols2', 

94 '4Arrows', '4ArrowsGray', '4RedToBlack', '4Rating', '4TrafficLights', 

95 '5Arrows', '5ArrowsGray', '5Rating', '5Quarters'])) 

96 iconId = Integer(allow_none=True) 

97 

98 def __init__(self, 

99 iconSet=None, 

100 iconId=None, 

101 ): 

102 self.iconSet = iconSet 

103 self.iconId = iconId 

104 

105 

106class ColorFilter(Serialisable): 

107 

108 tagname = "colorFilter" 

109 

110 dxfId = Integer(allow_none=True) 

111 cellColor = Bool(allow_none=True) 

112 

113 def __init__(self, 

114 dxfId=None, 

115 cellColor=None, 

116 ): 

117 self.dxfId = dxfId 

118 self.cellColor = cellColor 

119 

120 

121class DynamicFilter(Serialisable): 

122 

123 tagname = "dynamicFilter" 

124 

125 type = Set(values=(['null', 'aboveAverage', 'belowAverage', 'tomorrow', 

126 'today', 'yesterday', 'nextWeek', 'thisWeek', 'lastWeek', 'nextMonth', 

127 'thisMonth', 'lastMonth', 'nextQuarter', 'thisQuarter', 'lastQuarter', 

128 'nextYear', 'thisYear', 'lastYear', 'yearToDate', 'Q1', 'Q2', 'Q3', 'Q4', 

129 'M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10', 'M11', 

130 'M12'])) 

131 val = Float(allow_none=True) 

132 valIso = DateTime(allow_none=True) 

133 maxVal = Float(allow_none=True) 

134 maxValIso = DateTime(allow_none=True) 

135 

136 def __init__(self, 

137 type=None, 

138 val=None, 

139 valIso=None, 

140 maxVal=None, 

141 maxValIso=None, 

142 ): 

143 self.type = type 

144 self.val = val 

145 self.valIso = valIso 

146 self.maxVal = maxVal 

147 self.maxValIso = maxValIso 

148 

149 

150class CustomFilter(Serialisable): 

151 

152 tagname = "customFilter" 

153 

154 operator = NoneSet(values=(['equal', 'lessThan', 'lessThanOrEqual', 

155 'notEqual', 'greaterThanOrEqual', 'greaterThan'])) 

156 val = String() 

157 

158 def __init__(self, 

159 operator=None, 

160 val=None, 

161 ): 

162 self.operator = operator 

163 self.val = val 

164 

165 

166class CustomFilters(Serialisable): 

167 

168 tagname = "customFilters" 

169 

170 _and = Bool(allow_none=True) 

171 customFilter = Sequence(expected_type=CustomFilter) # min 1, max 2 

172 

173 __elements__ = ('customFilter',) 

174 

175 def __init__(self, 

176 _and=None, 

177 customFilter=(), 

178 ): 

179 self._and = _and 

180 self.customFilter = customFilter 

181 

182 

183class Top10(Serialisable): 

184 

185 tagname = "top10" 

186 

187 top = Bool(allow_none=True) 

188 percent = Bool(allow_none=True) 

189 val = Float() 

190 filterVal = Float(allow_none=True) 

191 

192 def __init__(self, 

193 top=None, 

194 percent=None, 

195 val=None, 

196 filterVal=None, 

197 ): 

198 self.top = top 

199 self.percent = percent 

200 self.val = val 

201 self.filterVal = filterVal 

202 

203 

204class DateGroupItem(Serialisable): 

205 

206 tagname = "dateGroupItem" 

207 

208 year = Integer() 

209 month = MinMax(min=1, max=12, allow_none=True) 

210 day = MinMax(min=1, max=31, allow_none=True) 

211 hour = MinMax(min=0, max=23, allow_none=True) 

212 minute = MinMax(min=0, max=59, allow_none=True) 

213 second = Integer(min=0, max=59, allow_none=True) 

214 dateTimeGrouping = Set(values=(['year', 'month', 'day', 'hour', 'minute', 

215 'second'])) 

216 

217 def __init__(self, 

218 year=None, 

219 month=None, 

220 day=None, 

221 hour=None, 

222 minute=None, 

223 second=None, 

224 dateTimeGrouping=None, 

225 ): 

226 self.year = year 

227 self.month = month 

228 self.day = day 

229 self.hour = hour 

230 self.minute = minute 

231 self.second = second 

232 self.dateTimeGrouping = dateTimeGrouping 

233 

234 

235class Filters(Serialisable): 

236 

237 tagname = "filters" 

238 

239 blank = Bool(allow_none=True) 

240 calendarType = NoneSet(values=["gregorian","gregorianUs", 

241 "gregorianMeFrench","gregorianArabic", "hijri","hebrew", 

242 "taiwan","japan", "thai","korea", 

243 "saka","gregorianXlitEnglish","gregorianXlitFrench"]) 

244 filter = ValueSequence(expected_type=str) 

245 dateGroupItem = Sequence(expected_type=DateGroupItem, allow_none=True) 

246 

247 __elements__ = ('filter', 'dateGroupItem') 

248 

249 def __init__(self, 

250 blank=None, 

251 calendarType=None, 

252 filter=(), 

253 dateGroupItem=(), 

254 ): 

255 self.blank = blank 

256 self.calendarType = calendarType 

257 self.filter = filter 

258 self.dateGroupItem = dateGroupItem 

259 

260 

261class FilterColumn(Serialisable): 

262 

263 tagname = "filterColumn" 

264 

265 colId = Integer() 

266 col_id = Alias('colId') 

267 hiddenButton = Bool(allow_none=True) 

268 showButton = Bool(allow_none=True) 

269 # some elements are choice 

270 filters = Typed(expected_type=Filters, allow_none=True) 

271 top10 = Typed(expected_type=Top10, allow_none=True) 

272 customFilters = Typed(expected_type=CustomFilters, allow_none=True) 

273 dynamicFilter = Typed(expected_type=DynamicFilter, allow_none=True) 

274 colorFilter = Typed(expected_type=ColorFilter, allow_none=True) 

275 iconFilter = Typed(expected_type=IconFilter, allow_none=True) 

276 extLst = Typed(expected_type=ExtensionList, allow_none=True) 

277 

278 __elements__ = ('filters', 'top10', 'customFilters', 'dynamicFilter', 

279 'colorFilter', 'iconFilter') 

280 

281 def __init__(self, 

282 colId=None, 

283 hiddenButton=None, 

284 showButton=None, 

285 filters=None, 

286 top10=None, 

287 customFilters=None, 

288 dynamicFilter=None, 

289 colorFilter=None, 

290 iconFilter=None, 

291 extLst=None, 

292 blank=None, 

293 vals=None, 

294 ): 

295 self.colId = colId 

296 self.hiddenButton = hiddenButton 

297 self.showButton = showButton 

298 self.filters = filters 

299 self.top10 = top10 

300 self.customFilters = customFilters 

301 self.dynamicFilter = dynamicFilter 

302 self.colorFilter = colorFilter 

303 self.iconFilter = iconFilter 

304 if blank is not None and self.filters: 

305 self.filters.blank = blank 

306 if vals is not None and self.filters: 

307 self.filters.filter = vals 

308 

309 

310class AutoFilter(Serialisable): 

311 

312 tagname = "autoFilter" 

313 

314 ref = CellRange() 

315 filterColumn = Sequence(expected_type=FilterColumn, allow_none=True) 

316 sortState = Typed(expected_type=SortState, allow_none=True) 

317 extLst = Typed(expected_type=ExtensionList, allow_none=True) 

318 

319 __elements__ = ('filterColumn', 'sortState') 

320 

321 def __init__(self, 

322 ref=None, 

323 filterColumn=(), 

324 sortState=None, 

325 extLst=None, 

326 ): 

327 self.ref = ref 

328 self.filterColumn = filterColumn 

329 self.sortState = sortState 

330 

331 

332 def __bool__(self): 

333 return self.ref is not None 

334 

335 

336 

337 def add_filter_column(self, col_id, vals, blank=False): 

338 """ 

339 Add row filter for specified column. 

340 

341 :param col_id: Zero-origin column id. 0 means first column. 

342 :type col_id: int 

343 :param vals: Value list to show. 

344 :type vals: str[] 

345 :param blank: Show rows that have blank cell if True (default=``False``) 

346 :type blank: bool 

347 """ 

348 self.filterColumn.append(FilterColumn(colId=col_id, filters=Filters(blank=blank, filter=vals))) 

349 

350 

351 def add_sort_condition(self, ref, descending=False): 

352 """ 

353 Add sort condition for cpecified range of cells. 

354 

355 :param ref: range of the cells (e.g. 'A2:A150') 

356 :type ref: string, is the same as that of the filter 

357 :param descending: Descending sort order (default=``False``) 

358 :type descending: bool 

359 """ 

360 cond = SortCondition(ref, descending) 

361 if self.sortState is None: 

362 self.sortState = SortState(ref=self.ref) 

363 self.sortState.sortCondition.append(cond)