Source code

Download source archive ↓

src/openpyxl/worksheet/scenario.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors.serialisable import Serialisable
4from openpyxl.descriptors import (
5    String,
6    Integer,
7    Bool,
8    Sequence,
9    Convertible,
10)
11from .cell_range import MultiCellRange
12
13
14class InputCells(Serialisable):
15
16    tagname = "inputCells"
17
18    r = String()
19    deleted = Bool(allow_none=True)
20    undone = Bool(allow_none=True)
21    val = String()
22    numFmtId = Integer(allow_none=True)
23
24    def __init__(self,
25                 r=None,
26                 deleted=False,
27                 undone=False,
28                 val=None,
29                 numFmtId=None,
30                ):
31        self.r = r
32        self.deleted = deleted
33        self.undone = undone
34        self.val = val
35        self.numFmtId = numFmtId
36
37
38class Scenario(Serialisable):
39
40    tagname = "scenario"
41
42    inputCells = Sequence(expected_type=InputCells)
43    name = String()
44    locked = Bool(allow_none=True)
45    hidden = Bool(allow_none=True)
46    user = String(allow_none=True)
47    comment = String(allow_none=True)
48
49    __elements__ = ('inputCells',)
50    __attrs__ = ('name', 'locked', 'hidden', 'user', 'comment', 'count')
51
52    def __init__(self,
53                 inputCells=(),
54                 name=None,
55                 locked=False,
56                 hidden=False,
57                 count=None,
58                 user=None,
59                 comment=None,
60                ):
61        self.inputCells = inputCells
62        self.name = name
63        self.locked = locked
64        self.hidden = hidden
65        self.user = user
66        self.comment = comment
67
68
69    @property
70    def count(self):
71        return len(self.inputCells)
72
73
74class ScenarioList(Serialisable):
75
76    tagname = "scenarios"
77
78    scenario = Sequence(expected_type=Scenario)
79    current = Integer(allow_none=True)
80    show = Integer(allow_none=True)
81    sqref = Convertible(expected_type=MultiCellRange, allow_none=True)
82
83    __elements__ = ('scenario',)
84
85    def __init__(self,
86                 scenario=(),
87                 current=None,
88                 show=None,
89                 sqref=None,
90                ):
91        self.scenario = scenario
92        self.current = current
93        self.show = show
94        self.sqref = sqref
95
96
97    def append(self, scenario):
98        s = self.scenario
99        s.append(scenario)
100        self.scenario = s
101
102
103    def __bool__(self):
104        return bool(self.scenario)
105