1###############################################################################
2#
3# Chartsheet - A class for writing the Excel XLSX Worksheet file.
4#
5# SPDX-License-Identifier: BSD-2-Clause
6#
7# Copyright (c) 2013-2025, John McNamara, jmcnamara@cpan.org
8#
9
10from . import worksheet
11from .drawing import Drawing
12
13
14class Chartsheet(worksheet.Worksheet):
15 """
16 A class for writing the Excel XLSX Chartsheet file.
17
18
19 """
20
21 ###########################################################################
22 #
23 # Public API.
24 #
25 ###########################################################################
26
27 def __init__(self):
28 """
29 Constructor.
30
31 """
32
33 super().__init__()
34
35 self.is_chartsheet = True
36 self.drawing = None
37 self.chart = None
38 self.charts = []
39 self.zoom_scale_normal = 0
40 self.orientation = 0
41 self.protection = False
42
43 def set_chart(self, chart):
44 """
45 Set the chart object for the chartsheet.
46 Args:
47 chart: Chart object.
48 Returns:
49 chart: A reference to the chart object.
50 """
51 chart.embedded = False
52 chart.protection = self.protection
53 self.chart = chart
54 self.charts.append([0, 0, chart, 0, 0, 1, 1])
55 return chart
56
57 def protect(self, password="", options=None):
58 """
59 Set the password and protection options of the worksheet.
60
61 Args:
62 password: An optional password string.
63 options: A dictionary of worksheet objects to protect.
64
65 Returns:
66 Nothing.
67
68 """
69 # This method is overridden from parent worksheet class.
70
71 # Chartsheets only allow a reduced set of protect options.
72 copy = {}
73
74 if not options:
75 options = {}
76
77 if options.get("objects") is None:
78 copy["objects"] = False
79 else:
80 # Objects are default on for chartsheets, so reverse state.
81 copy["objects"] = not options["objects"]
82
83 if options.get("content") is None:
84 copy["content"] = True
85 else:
86 copy["content"] = options["content"]
87
88 copy["sheet"] = False
89 copy["scenarios"] = True
90
91 # If objects and content are both off then the chartsheet isn't
92 # protected, unless it has a password.
93 if password == "" and copy["objects"] and not copy["content"]:
94 return
95
96 if self.chart:
97 self.chart.protection = True
98 else:
99 self.protection = True
100
101 # Call the parent method.
102 super().protect(password, copy)
103
104 ###########################################################################
105 #
106 # Private API.
107 #
108 ###########################################################################
109 def _assemble_xml_file(self):
110 # Assemble and write the XML file.
111
112 # Write the XML declaration.
113 self._xml_declaration()
114
115 # Write the root worksheet element.
116 self._write_chartsheet()
117
118 # Write the worksheet properties.
119 self._write_sheet_pr()
120
121 # Write the sheet view properties.
122 self._write_sheet_views()
123
124 # Write the sheetProtection element.
125 self._write_sheet_protection()
126
127 # Write the printOptions element.
128 self._write_print_options()
129
130 # Write the worksheet page_margins.
131 self._write_page_margins()
132
133 # Write the worksheet page setup.
134 self._write_page_setup()
135
136 # Write the headerFooter element.
137 self._write_header_footer()
138
139 # Write the drawing element.
140 self._write_drawings()
141
142 # Write the legacyDrawingHF element.
143 self._write_legacy_drawing_hf()
144
145 # Close the worksheet tag.
146 self._xml_end_tag("chartsheet")
147
148 # Close the file.
149 self._xml_close()
150
151 def _prepare_chart(self, index, chart_id, drawing_id):
152 # Set up chart/drawings.
153
154 self.chart.id = chart_id - 1
155
156 self.drawing = Drawing()
157 self.drawing.orientation = self.orientation
158
159 self.external_drawing_links.append(
160 ["/drawing", "../drawings/drawing" + str(drawing_id) + ".xml"]
161 )
162
163 self.drawing_links.append(
164 ["/chart", "../charts/chart" + str(chart_id) + ".xml"]
165 )
166
167 ###########################################################################
168 #
169 # XML methods.
170 #
171 ###########################################################################
172
173 def _write_chartsheet(self):
174 # Write the <worksheet> element. This is the root element.
175
176 schema = "http://schemas.openxmlformats.org/"
177 xmlns = schema + "spreadsheetml/2006/main"
178 xmlns_r = schema + "officeDocument/2006/relationships"
179
180 attributes = [("xmlns", xmlns), ("xmlns:r", xmlns_r)]
181
182 self._xml_start_tag("chartsheet", attributes)
183
184 def _write_sheet_pr(self):
185 # Write the <sheetPr> element for Sheet level properties.
186 attributes = []
187
188 if self.filter_on:
189 attributes.append(("filterMode", 1))
190
191 if self.fit_page or self.tab_color:
192 self._xml_start_tag("sheetPr", attributes)
193 self._write_tab_color()
194 self._write_page_set_up_pr()
195 self._xml_end_tag("sheetPr")
196 else:
197 self._xml_empty_tag("sheetPr", attributes)