Source code

Download source archive ↓

src/openpyxl/worksheet/views.py

1# Copyright (c) 2010-2024 openpyxl
2
3from openpyxl.descriptors import (
4    Bool,
5    Integer,
6    String,
7    Set,
8    Float,
9    Typed,
10    NoneSet,
11    Sequence,
12)
13from openpyxl.descriptors.excel import ExtensionList
14from openpyxl.descriptors.serialisable import Serialisable
15
16
17class Pane(Serialisable):
18    xSplit = Float(allow_none=True)
19    ySplit = Float(allow_none=True)
20    topLeftCell = String(allow_none=True)
21    activePane = Set(values=("bottomRight", "topRight", "bottomLeft", "topLeft"))
22    state = Set(values=("split", "frozen", "frozenSplit"))
23
24    def __init__(self,
25                 xSplit=None,
26                 ySplit=None,
27                 topLeftCell=None,
28                 activePane="topLeft",
29                 state="split"):
30        self.xSplit = xSplit
31        self.ySplit = ySplit
32        self.topLeftCell = topLeftCell
33        self.activePane = activePane
34        self.state = state
35
36
37class Selection(Serialisable):
38    pane = NoneSet(values=("bottomRight", "topRight", "bottomLeft", "topLeft"))
39    activeCell = String(allow_none=True)
40    activeCellId = Integer(allow_none=True)
41    sqref = String(allow_none=True)
42
43    def __init__(self,
44                 pane=None,
45                 activeCell="A1",
46                 activeCellId=None,
47                 sqref="A1"):
48        self.pane = pane
49        self.activeCell = activeCell
50        self.activeCellId = activeCellId
51        self.sqref = sqref
52
53
54class SheetView(Serialisable):
55
56    """Information about the visible portions of this sheet."""
57
58    tagname = "sheetView"
59
60    windowProtection = Bool(allow_none=True)
61    showFormulas = Bool(allow_none=True)
62    showGridLines = Bool(allow_none=True)
63    showRowColHeaders = Bool(allow_none=True)
64    showZeros = Bool(allow_none=True)
65    rightToLeft = Bool(allow_none=True)
66    tabSelected = Bool(allow_none=True)
67    showRuler = Bool(allow_none=True)
68    showOutlineSymbols = Bool(allow_none=True)
69    defaultGridColor = Bool(allow_none=True)
70    showWhiteSpace = Bool(allow_none=True)
71    view = NoneSet(values=("normal", "pageBreakPreview", "pageLayout"))
72    topLeftCell = String(allow_none=True)
73    colorId = Integer(allow_none=True)
74    zoomScale = Integer(allow_none=True)
75    zoomScaleNormal = Integer(allow_none=True)
76    zoomScaleSheetLayoutView = Integer(allow_none=True)
77    zoomScalePageLayoutView = Integer(allow_none=True)
78    zoomToFit = Bool(allow_none=True) # Chart sheets only
79    workbookViewId = Integer()
80    selection = Sequence(expected_type=Selection)
81    pane = Typed(expected_type=Pane, allow_none=True)
82
83    def __init__(self,
84                 windowProtection=None,
85                 showFormulas=None,
86                 showGridLines=None,
87                 showRowColHeaders=None,
88                 showZeros=None,
89                 rightToLeft=None,
90                 tabSelected=None,
91                 showRuler=None,
92                 showOutlineSymbols=None,
93                 defaultGridColor=None,
94                 showWhiteSpace=None,
95                 view=None,
96                 topLeftCell=None,
97                 colorId=None,
98                 zoomScale=None,
99                 zoomScaleNormal=None,
100                 zoomScaleSheetLayoutView=None,
101                 zoomScalePageLayoutView=None,
102                 zoomToFit=None,
103                 workbookViewId=0,
104                 selection=None,
105                 pane=None,):
106        self.windowProtection = windowProtection
107        self.showFormulas = showFormulas
108        self.showGridLines = showGridLines
109        self.showRowColHeaders = showRowColHeaders
110        self.showZeros = showZeros
111        self.rightToLeft = rightToLeft
112        self.tabSelected = tabSelected
113        self.showRuler = showRuler
114        self.showOutlineSymbols = showOutlineSymbols
115        self.defaultGridColor = defaultGridColor
116        self.showWhiteSpace = showWhiteSpace
117        self.view = view
118        self.topLeftCell = topLeftCell
119        self.colorId = colorId
120        self.zoomScale = zoomScale
121        self.zoomScaleNormal = zoomScaleNormal
122        self.zoomScaleSheetLayoutView = zoomScaleSheetLayoutView
123        self.zoomScalePageLayoutView = zoomScalePageLayoutView
124        self.zoomToFit = zoomToFit
125        self.workbookViewId = workbookViewId
126        self.pane = pane
127        if selection is None:
128            selection = (Selection(), )
129        self.selection = selection
130
131
132class SheetViewList(Serialisable):
133
134    tagname = "sheetViews"
135
136    sheetView = Sequence(expected_type=SheetView, )
137    extLst = Typed(expected_type=ExtensionList, allow_none=True)
138
139    __elements__ = ('sheetView',)
140
141    def __init__(self,
142                 sheetView=None,
143                 extLst=None,
144                ):
145        if sheetView is None:
146            sheetView = [SheetView()]
147        self.sheetView = sheetView
148
149
150    @property
151    def active(self):
152        """
153        Returns the first sheet view which is assumed to be active
154        """
155        return self.sheetView[0]