Branch data TLA Line data Source code
1 : : // Copyright (c) 2014-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #include <xlnt/cell/cell.hpp>
26 : : #include <xlnt/styles/style.hpp>
27 : : #include <xlnt/workbook/workbook.hpp>
28 : : #include <xlnt/worksheet/range.hpp>
29 : : #include <xlnt/worksheet/range_iterator.hpp>
30 : : #include <xlnt/worksheet/range_reference.hpp>
31 : : #include <xlnt/worksheet/worksheet.hpp>
32 : :
33 : : namespace xlnt {
34 : :
35 :CBC 109 : range::range(class worksheet ws, const range_reference &reference, major_order order, bool skip_null)
36 : 109 : : ws_(ws),
37 : 109 : ref_(reference),
38 : 109 : order_(order),
39 : 109 : skip_null_(skip_null)
40 : : {
41 : 109 : }
42 : :
43 : 115 : range::~range() = default;
44 : :
45 : 1 : void range::clear_cells()
46 : : {
47 [ + + + + ]: 1 : if (ref_.top_left().column() == ws_.lowest_column()
48 [ - + - - : 1 : && ref_.bottom_right().column() == ws_.highest_column())
- - - - -
+ ]
49 : : {
50 [ # # # # :UBC 0 : for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
# # ]
51 : : {
52 : 0 : ws_.clear_row(row);
53 : : }
54 : : }
55 : : else
56 : : {
57 [ + + + + :CBC 4 : for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
+ + ]
58 : : {
59 [ + + + + : 9 : for (auto column = ref_.top_left().column(); column <= ref_.bottom_right().column(); ++column)
+ + + ]
60 : : {
61 [ + + + ]: 6 : ws_.clear_cell(xlnt::cell_reference(column, row));
62 : : }
63 : : }
64 : : }
65 : 1 : }
66 : :
67 : 46 : cell_vector range::operator[](std::size_t index)
68 : : {
69 : 46 : return vector(index);
70 : : }
71 : :
72 :UBC 0 : const cell_vector range::operator[](std::size_t index) const
73 : : {
74 : 0 : return vector(index);
75 : : }
76 : :
77 :CBC 6 : const worksheet &range::target_worksheet() const
78 : : {
79 : 6 : return ws_;
80 : : }
81 : :
82 : 11 : range_reference range::reference() const
83 : : {
84 : 11 : return ref_;
85 : : }
86 : :
87 : 7 : std::size_t range::length() const
88 : : {
89 [ + + ]: 7 : if (order_ == major_order::row)
90 : : {
91 [ + + + + ]: 4 : return ref_.bottom_right().row() - ref_.top_left().row() + 1;
92 : : }
93 : :
94 [ + + + + : 3 : return (ref_.bottom_right().column() - ref_.top_left().column()).index + 1;
+ ]
95 : : }
96 : :
97 : 8 : bool range::operator==(const range &comparand) const
98 : : {
99 : 8 : return ref_ == comparand.ref_
100 [ + - ]: 7 : && ws_ == comparand.ws_
101 [ + + + - ]: 15 : && order_ == comparand.order_;
102 : : }
103 : :
104 : 46 : cell_vector range::vector(std::size_t vector_index)
105 : : {
106 [ + ]: 46 : auto cursor = ref_.top_left();
107 : :
108 [ + + ]: 46 : if (order_ == major_order::row)
109 : : {
110 [ + + ]: 37 : cursor.row(cursor.row() + static_cast<row_t>(vector_index));
111 : : }
112 : : else
113 : : {
114 [ + + + ]: 9 : cursor.column_index(cursor.column_index() + static_cast<column_t::index_t>(vector_index));
115 : : }
116 : :
117 [ + + ]: 46 : return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
118 : : }
119 : :
120 :UBC 0 : const cell_vector range::vector(std::size_t vector_index) const
121 : : {
122 [ # ]: 0 : auto cursor = ref_.top_left();
123 : :
124 [ # # ]: 0 : if (order_ == major_order::row)
125 : : {
126 [ # # ]: 0 : cursor.row(cursor.row() + static_cast<row_t>(vector_index));
127 : : }
128 : : else
129 : : {
130 [ # # # ]: 0 : cursor.column_index(cursor.column_index() + static_cast<column_t::index_t>(vector_index));
131 : : }
132 : :
133 [ # # ]: 0 : return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
134 : : }
135 : :
136 :CBC 4 : bool range::contains(const cell_reference &cell_ref)
137 : : {
138 : 4 : return ref_.contains(cell_ref);
139 : : }
140 : :
141 :UBC 0 : range range::alignment(const xlnt::alignment &new_alignment)
142 : : {
143 [ # ]: 0 : apply([&new_alignment](class cell c) { c.alignment(new_alignment); });
144 : 0 : return *this;
145 : : }
146 : :
147 : 0 : range range::border(const xlnt::border &new_border)
148 : : {
149 [ # ]: 0 : apply([&new_border](class cell c) { c.border(new_border); });
150 : 0 : return *this;
151 : : }
152 : :
153 : 0 : range range::fill(const xlnt::fill &new_fill)
154 : : {
155 [ # ]: 0 : apply([&new_fill](class cell c) { c.fill(new_fill); });
156 : 0 : return *this;
157 : : }
158 : :
159 :CBC 2 : range range::font(const xlnt::font &new_font)
160 : : {
161 [ + ]: 22 : apply([&new_font](class cell c) { c.font(new_font); });
162 : 2 : return *this;
163 : : }
164 : :
165 :UBC 0 : range range::number_format(const xlnt::number_format &new_number_format)
166 : : {
167 [ # ]: 0 : apply([&new_number_format](class cell c) { c.number_format(new_number_format); });
168 : 0 : return *this;
169 : : }
170 : :
171 : 0 : range range::protection(const xlnt::protection &new_protection)
172 : : {
173 [ # ]: 0 : apply([&new_protection](class cell c) { c.protection(new_protection); });
174 : 0 : return *this;
175 : : }
176 : :
177 : 0 : range range::style(const class style &new_style)
178 : : {
179 [ # ]: 0 : apply([&new_style](class cell c) { c.style(new_style); });
180 : 0 : return *this;
181 : : }
182 : :
183 : 0 : range range::style(const std::string &style_name)
184 : : {
185 [ # # # ]: 0 : return style(ws_.workbook().style(style_name));
186 : : }
187 : :
188 : 0 : conditional_format range::conditional_format(const condition &when)
189 : : {
190 : 0 : return ws_.conditional_format(ref_, when);
191 : : }
192 : :
193 :CBC 2 : void range::apply(std::function<void(class cell)> f)
194 : : {
195 [ + + + + : 24 : for (auto row : *this)
+ + + ]
196 : : {
197 [ + + + + : 51 : for (auto cell : row)
+ + + ]
198 : : {
199 [ + ]: 20 : f(cell);
200 : : }
201 : : }
202 : 2 : }
203 : :
204 : 1 : cell range::cell(const cell_reference &ref)
205 : : {
206 [ + + + + ]: 1 : return (*this)[ref.row() - 1][ref.column().index - 1];
207 : : }
208 : :
209 :UBC 0 : const cell range::cell(const cell_reference &ref) const
210 : : {
211 [ # # # # ]: 0 : return (*this)[ref.row() - 1][ref.column().index - 1];
212 : : }
213 : :
214 :CBC 4 : cell_vector range::front()
215 : : {
216 [ + + ]: 4 : return *begin();
217 : : }
218 : :
219 : 1 : const cell_vector range::front() const
220 : : {
221 [ + + ]: 1 : return *cbegin();
222 : : }
223 : :
224 : 4 : cell_vector range::back()
225 : : {
226 [ + + + ]: 4 : return *(--end());
227 : : }
228 : :
229 : 1 : const cell_vector range::back() const
230 : : {
231 [ + + + ]: 1 : return *(--cend());
232 : : }
233 : :
234 : 55 : range::iterator range::begin()
235 : : {
236 [ + + ]: 110 : return iterator(ws_, ref_.top_left(), ref_, order_, skip_null_);
237 : : }
238 : :
239 : 51 : range::iterator range::end()
240 : : {
241 [ + ]: 51 : auto cursor = ref_.top_left();
242 : :
243 [ + + ]: 51 : if (order_ == major_order::row)
244 : : {
245 [ + + + ]: 45 : cursor.row(ref_.bottom_right().row() + 1);
246 : : }
247 : : else
248 : : {
249 [ + + + + ]: 6 : cursor.column_index(ref_.bottom_right().column_index() + 1);
250 : : }
251 : :
252 [ + ]: 102 : return iterator(ws_, cursor, ref_, order_, skip_null_);
253 : : }
254 : :
255 : 17 : range::const_iterator range::cbegin() const
256 : : {
257 [ + + ]: 34 : return const_iterator(ws_, ref_.top_left(), ref_, order_, skip_null_);
258 : : }
259 : :
260 : 10 : range::const_iterator range::cend() const
261 : : {
262 [ + ]: 10 : auto cursor = ref_.top_left();
263 : :
264 [ + + ]: 10 : if (order_ == major_order::row)
265 : : {
266 [ + + + ]: 6 : cursor.row(ref_.bottom_right().row() + 1);
267 : : }
268 : : else
269 : : {
270 [ + + + + ]: 4 : cursor.column_index(ref_.bottom_right().column_index() + 1);
271 : : }
272 : :
273 [ + ]: 20 : return const_iterator(ws_, cursor, ref_, order_, skip_null_);
274 : : }
275 : :
276 : 1 : bool range::operator!=(const range &comparand) const
277 : : {
278 : 1 : return !(*this == comparand);
279 : : }
280 : :
281 : 4 : range::const_iterator range::begin() const
282 : : {
283 : 4 : return cbegin();
284 : : }
285 : :
286 : 3 : range::const_iterator range::end() const
287 : : {
288 : 3 : return cend();
289 : : }
290 : :
291 : 2 : range::reverse_iterator range::rbegin()
292 : : {
293 [ + + ]: 2 : return reverse_iterator(end());
294 : : }
295 : :
296 : 5 : range::reverse_iterator range::rend()
297 : : {
298 [ + + ]: 5 : return reverse_iterator(begin());
299 : : }
300 : :
301 : 4 : range::const_reverse_iterator range::crbegin() const
302 : : {
303 : 4 : return const_reverse_iterator(cend());
304 : : }
305 : :
306 : 2 : range::const_reverse_iterator range::rbegin() const
307 : : {
308 : 2 : return crbegin();
309 : : }
310 : :
311 : 9 : range::const_reverse_iterator range::crend() const
312 : : {
313 : 9 : return const_reverse_iterator(cbegin());
314 : : }
315 : :
316 : 4 : range::const_reverse_iterator range::rend() const
317 : : {
318 : 4 : return crend();
319 : : }
320 : :
321 : : } // namespace xlnt
|