TLA Line data Source code
1 : // Copyright (c) 2014-2022 Thomas Fussell
2 : // Copyright (c) 2010-2015 openpyxl
3 : // Copyright (c) 2024-2025 xlnt-community
4 : //
5 : // Permission is hereby granted, free of charge, to any person obtaining a copy
6 : // of this software and associated documentation files (the "Software"), to deal
7 : // in the Software without restriction, including without limitation the rights
8 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : // copies of the Software, and to permit persons to whom the Software is
10 : // furnished to do so, subject to the following conditions:
11 : //
12 : // The above copyright notice and this permission notice shall be included in
13 : // all copies or substantial portions of the Software.
14 : //
15 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : // THE SOFTWARE
22 : //
23 : // @license: http://www.opensource.org/licenses/mit-license.php
24 : // @author: see AUTHORS file
25 :
26 : #pragma once
27 :
28 : #include <cstdint>
29 : #include <functional>
30 : #include <iterator>
31 : #include <memory>
32 : #include <string>
33 : #include <unordered_map>
34 : #include <vector>
35 :
36 : #include <xlnt/xlnt_config.hpp>
37 : #include <xlnt/internal/features.hpp>
38 :
39 : #if XLNT_HAS_INCLUDE(<string_view>) && XLNT_HAS_FEATURE(U8_STRING_VIEW)
40 : #include <string_view>
41 : #endif
42 :
43 : namespace xlnt {
44 :
45 : enum class calendar;
46 : enum class core_property;
47 : enum class extended_property;
48 : enum class relationship_type;
49 :
50 : class alignment;
51 : class border;
52 : class calculation_properties;
53 : class cell;
54 : class cell_style;
55 : class color;
56 : class const_worksheet_iterator;
57 : class fill;
58 : class font;
59 : class format;
60 : class rich_text;
61 : class manifest;
62 : class metadata_property;
63 : class named_range;
64 : class number_format;
65 : class path;
66 : class pattern_fill;
67 : class protection;
68 : class range;
69 : class range_reference;
70 : class relationship;
71 : class streaming_workbook_reader;
72 : class style;
73 : class style_serializer;
74 : class theme;
75 : class variant;
76 : class workbook_view;
77 : class worksheet;
78 : class worksheet_iterator;
79 : class zip_file;
80 :
81 : struct datetime;
82 :
83 : namespace detail {
84 :
85 : struct stylesheet;
86 : struct workbook_impl;
87 : struct worksheet_impl;
88 : class xlsx_consumer;
89 : class xlsx_producer;
90 :
91 : } // namespace detail
92 :
93 : /// <summary>
94 : /// workbook is the container for all other parts of the document.
95 : /// </summary>
96 : class XLNT_API workbook
97 : {
98 : public:
99 : /// <summary>
100 : /// The method for cloning workbooks.
101 : /// </summary>
102 : enum class clone_method
103 : {
104 : deep_copy,
105 : shallow_copy
106 : };
107 :
108 : /// <summary>
109 : /// typedef for the iterator used for iterating through this workbook
110 : /// (non-const) in a range-based for loop.
111 : /// </summary>
112 : using iterator = worksheet_iterator;
113 :
114 : /// <summary>
115 : /// typedef for the iterator used for iterating through this workbook
116 : /// (const) in a range-based for loop.
117 : /// </summary>
118 : using const_iterator = const_worksheet_iterator;
119 :
120 : /// <summary>
121 : /// typedef for the iterator used for iterating through this workbook
122 : /// (non-const) in a range-based for loop in reverse order using
123 : /// std::make_reverse_iterator.
124 : /// </summary>
125 : using reverse_iterator = std::reverse_iterator<iterator>;
126 :
127 : /// <summary>
128 : /// typedef for the iterator used for iterating through this workbook
129 : /// (const) in a range-based for loop in reverse order using
130 : /// std::make_reverse_iterator.
131 : /// </summary>
132 : using const_reverse_iterator = std::reverse_iterator<const_iterator>;
133 :
134 : /// <summary>
135 : /// Constructs and returns an empty workbook similar to a default.
136 : /// Excel workbook
137 : /// </summary>
138 : static workbook empty();
139 :
140 : // Constructors
141 :
142 : /// <summary>
143 : /// Default constructor. Constructs a workbook containing a single empty
144 : /// worksheet using workbook::empty().
145 : /// </summary>
146 : workbook();
147 :
148 : /// <summary>
149 : /// load the xlsx file at path
150 : /// </summary>
151 : workbook(const xlnt::path &file);
152 :
153 : /// <summary>
154 : /// load the encrpyted xlsx file at path
155 : /// </summary>
156 : workbook(const xlnt::path &file, const std::string &password);
157 :
158 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
159 : /// <summary>
160 : /// load the encrpyted xlsx file at path
161 : /// </summary>
162 : workbook(const xlnt::path &file, std::u8string_view password);
163 : #endif
164 :
165 : /// <summary>
166 : /// construct the workbook from any data stream where the data is the binary form of a workbook
167 : /// </summary>
168 : workbook(std::istream &data);
169 :
170 : /// <summary>
171 : /// construct the workbook from any data stream where the data is the binary form of an encrypted workbook
172 : /// </summary>
173 : workbook(std::istream &data, const std::string &password);
174 :
175 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
176 : /// <summary>
177 : /// construct the workbook from any data stream where the data is the binary form of an encrypted workbook
178 : /// </summary>
179 : workbook(std::istream &data, std::u8string_view password);
180 : #endif
181 :
182 : /// <summary>
183 : /// Move constructor. Constructs a workbook from existing workbook, other.
184 : /// </summary>
185 CBC 1 : workbook(workbook &&other) = default;
186 :
187 : /// <summary>
188 : /// Copy constructor. Constructs this workbook from existing workbook, other.
189 : /// Creates a shallow copy, copying the workbook's internal pointers.
190 : /// </summary>
191 2 : workbook(const workbook &other) = default;
192 :
193 : /// <summary>
194 : /// Destroys this workbook, deallocating all internal storage space. Any pimpl
195 : /// wrapper classes (e.g. cell) pointing into this workbook will be invalid
196 : /// after this is executed.
197 : /// </summary>
198 1362 : ~workbook() = default;
199 :
200 : /// <summary>
201 : /// Creates a clone of this workbook. A shallow copy will copy the workbook's internal pointers,
202 : /// while a deep copy will copy all the internal structures and create a full clone of the workbook.
203 : /// </summary>
204 : workbook clone(clone_method method) const;
205 :
206 : // Worksheets
207 :
208 : /// <summary>
209 : /// Creates and returns a sheet after the last sheet in this workbook.
210 : /// </summary>
211 : worksheet create_sheet();
212 :
213 : /// <summary>
214 : /// Creates and returns a sheet at the specified index.
215 : /// </summary>
216 : worksheet create_sheet(std::size_t index);
217 :
218 : /// <summary>
219 : /// TODO: This should be private...
220 : /// </summary>
221 : worksheet create_sheet_with_rel(const std::string &title, const relationship &rel);
222 :
223 : /// <summary>
224 : /// Creates and returns a new sheet after the last sheet initializing it
225 : /// with all of the data from the provided worksheet.
226 : /// </summary>
227 : worksheet copy_sheet(worksheet worksheet);
228 :
229 : /// <summary>
230 : /// Creates and returns a new sheet at the specified index initializing it
231 : /// with all of the data from the provided worksheet.
232 : /// </summary>
233 : worksheet copy_sheet(worksheet worksheet, std::size_t index);
234 :
235 : /// <summary>
236 : /// Returns the worksheet that is determined to be active. An active
237 : /// sheet is that which is initially shown by the spreadsheet editor.
238 : /// </summary>
239 : worksheet active_sheet();
240 :
241 : /// <summary>
242 : /// Sets the worksheet that is determined to be active. An active
243 : /// sheet is that which is initially shown by the spreadsheet editor.
244 : /// </summary>
245 : void active_sheet(std::size_t index);
246 :
247 : /// <summary>
248 : /// Returns the worksheet with the given name. This may throw an exception
249 : /// if the sheet isn't found. Use workbook::contains(const std::string &)
250 : /// to make sure the sheet exists before calling this method.
251 : /// </summary>
252 : worksheet sheet_by_title(const std::string &title);
253 :
254 : /// <summary>
255 : /// Returns the worksheet with the given name. This may throw an exception
256 : /// if the sheet isn't found. Use workbook::contains(const std::string &)
257 : /// to make sure the sheet exists before calling this method.
258 : /// </summary>
259 : const worksheet sheet_by_title(const std::string &title) const;
260 :
261 : /// <summary>
262 : /// Returns the worksheet at the given index. This will throw an exception
263 : /// if index is greater than or equal to the number of sheets in this workbook.
264 : /// </summary>
265 : worksheet sheet_by_index(std::size_t index);
266 :
267 : /// <summary>
268 : /// Returns the worksheet at the given index. This will throw an exception
269 : /// if index is greater than or equal to the number of sheets in this workbook.
270 : /// </summary>
271 : const worksheet sheet_by_index(std::size_t index) const;
272 :
273 : /// <summary>
274 : /// Returns the worksheet with a sheetId of id. Sheet IDs are arbitrary numbers
275 : /// that uniquely identify a sheet. Most users won't need this.
276 : /// </summary>
277 : worksheet sheet_by_id(std::size_t id);
278 :
279 : /// <summary>
280 : /// Returns the worksheet with a sheetId of id. Sheet IDs are arbitrary numbers
281 : /// that uniquely identify a sheet. Most users won't need this.
282 : /// </summary>
283 : const worksheet sheet_by_id(std::size_t id) const;
284 :
285 : /// <summary>
286 : /// Returns the hidden identifier of the worksheet at the given index.
287 : /// This will throw an exception if index is greater than or equal to the
288 : /// number of sheets in this workbook.
289 : /// </summary>
290 : bool sheet_hidden_by_index(std::size_t index) const;
291 :
292 : /// <summary>
293 : /// Returns true if this workbook contains a sheet with the given title.
294 : /// </summary>
295 : bool contains(const std::string &title) const;
296 :
297 : /// <summary>
298 : /// Returns the index of the given worksheet. The worksheet must be owned by this workbook.
299 : /// </summary>
300 : std::size_t index(worksheet worksheet) const;
301 :
302 : /// <summary>
303 : /// Moves a sheet to a new position defined. The worksheet must be owned by this workbook.
304 : /// </summary>
305 : void move_sheet(worksheet worksheet, std::size_t newIndex);
306 :
307 : // remove worksheets
308 :
309 : /// <summary>
310 : /// Removes the given worksheet from this workbook.
311 : /// </summary>
312 : void remove_sheet(worksheet worksheet);
313 :
314 : /// <summary>
315 : /// Sets the contents of this workbook to be equivalent to that of
316 : /// a workbook returned by workbook::empty().
317 : /// </summary>
318 : void clear();
319 :
320 : // iterators
321 :
322 : /// <summary>
323 : /// Returns an iterator to the first worksheet in this workbook.
324 : /// </summary>
325 : iterator begin();
326 :
327 : /// <summary>
328 : /// Returns an iterator to the worksheet following the last worksheet of the workbook.
329 : /// This worksheet acts as a placeholder; attempting to access it will cause an
330 : /// exception to be thrown.
331 : /// </summary>
332 : iterator end();
333 :
334 : /// <summary>
335 : /// Returns a const iterator to the first worksheet in this workbook.
336 : /// </summary>
337 : const_iterator begin() const;
338 :
339 : /// <summary>
340 : /// Returns a const iterator to the worksheet following the last worksheet of the workbook.
341 : /// This worksheet acts as a placeholder; attempting to access it will cause an
342 : /// exception to be thrown.
343 : /// </summary>
344 : const_iterator end() const;
345 :
346 : /// <summary>
347 : /// Returns an iterator to the first worksheet in this workbook.
348 : /// </summary>
349 : const_iterator cbegin() const;
350 :
351 : /// <summary>
352 : /// Returns a const iterator to the worksheet following the last worksheet of the workbook.
353 : /// This worksheet acts as a placeholder; attempting to access it will cause an
354 : /// exception to be thrown.
355 : /// </summary>
356 : const_iterator cend() const;
357 :
358 : /// <summary>
359 : /// Applies the function "f" to every non-empty cell in every worksheet in this workbook.
360 : /// </summary>
361 : void apply_to_cells(std::function<void(cell)> f);
362 :
363 : /// <summary>
364 : /// Returns a temporary vector containing the titles of each sheet in the order
365 : /// of the sheets in the workbook.
366 : /// </summary>
367 : std::vector<std::string> sheet_titles() const;
368 :
369 : /// <summary>
370 : /// Returns the number of sheets in this workbook.
371 : /// </summary>
372 : std::size_t sheet_count() const;
373 :
374 : // Metadata Properties
375 :
376 : /// <summary>
377 : /// Returns true if the workbook has the core property with the given name.
378 : /// </summary>
379 : bool has_core_property(xlnt::core_property type) const;
380 :
381 : /// <summary>
382 : /// Returns a vector of the type of each core property that is set to
383 : /// a particular value in this workbook.
384 : /// </summary>
385 : std::vector<xlnt::core_property> core_properties() const;
386 :
387 : /// <summary>
388 : /// Returns the value of the given core property.
389 : /// </summary>
390 : variant core_property(xlnt::core_property type) const;
391 :
392 : /// <summary>
393 : /// Sets the given core property to the provided value.
394 : /// </summary>
395 : void core_property(xlnt::core_property type, const variant &value);
396 :
397 : /// <summary>
398 : /// Returns true if the workbook has the extended property with the given name.
399 : /// </summary>
400 : bool has_extended_property(xlnt::extended_property type) const;
401 :
402 : /// <summary>
403 : /// Returns a vector of the type of each extended property that is set to
404 : /// a particular value in this workbook.
405 : /// </summary>
406 : std::vector<xlnt::extended_property> extended_properties() const;
407 :
408 : /// <summary>
409 : /// Returns the value of the given extended property.
410 : /// </summary>
411 : variant extended_property(xlnt::extended_property type) const;
412 :
413 : /// <summary>
414 : /// Sets the given extended property to the provided value.
415 : /// </summary>
416 : void extended_property(xlnt::extended_property type, const variant &value);
417 :
418 : /// <summary>
419 : /// Returns true if the workbook has the custom property with the given name.
420 : /// </summary>
421 : bool has_custom_property(const std::string &property_name) const;
422 :
423 : /// <summary>
424 : /// Returns a vector of the name of each custom property that is set to
425 : /// a particular value in this workbook.
426 : /// </summary>
427 : std::vector<std::string> custom_properties() const;
428 :
429 : /// <summary>
430 : /// Returns the value of the given custom property.
431 : /// </summary>
432 : variant custom_property(const std::string &property_name) const;
433 :
434 : /// <summary>
435 : /// Creates a new custom property in this workbook and sets it to the provided value.
436 : /// </summary>
437 : void custom_property(const std::string &property_name, const variant &value);
438 :
439 : /// <summary>
440 : /// Returns the base date used by this workbook. This will generally be windows_1900
441 : /// except on Apple based systems when it will default to mac_1904 unless otherwise
442 : /// set via `void workbook::base_date(calendar base_date)`.
443 : /// </summary>
444 : calendar base_date() const;
445 :
446 : /// <summary>
447 : /// Sets the base date style of this workbook. This is the date and time that
448 : /// a numeric value of 0 represents.
449 : /// </summary>
450 : void base_date(calendar base_date);
451 :
452 : /// <summary>
453 : /// Returns true if this workbook has had its title set.
454 : /// </summary>
455 : bool has_title() const;
456 :
457 : /// <summary>
458 : /// Returns the title of this workbook.
459 : /// </summary>
460 : std::string title() const;
461 :
462 : /// <summary>
463 : /// Sets the title of this workbook to title.
464 : /// </summary>
465 : void title(const std::string &title);
466 :
467 : /// <summary>
468 : /// Sets the absolute path of this workbook to path.
469 : /// </summary>
470 : void abs_path(const std::string &path);
471 :
472 : /// <summary>
473 : /// Sets the ArchID flags of this workbook to flags.
474 : /// </summary>
475 : void arch_id_flags(const std::size_t flags);
476 :
477 : // Named Ranges
478 :
479 : /// <summary>
480 : /// Returns a vector of the named ranges in this workbook.
481 : /// </summary>
482 : std::vector<xlnt::named_range> named_ranges() const;
483 :
484 : /// <summary>
485 : /// Creates a new names range.
486 : /// </summary>
487 : void create_named_range(const std::string &name, worksheet worksheet, const range_reference &reference);
488 :
489 : /// <summary>
490 : /// Creates a new names range.
491 : /// </summary>
492 : void create_named_range(const std::string &name, worksheet worksheet, const std::string &reference_string);
493 :
494 : /// <summary>
495 : /// Returns true if a named range of the given name exists in the workbook.
496 : /// </summary>
497 : bool has_named_range(const std::string &name) const;
498 :
499 : /// <summary>
500 : /// Returns the named range with the given name.
501 : /// </summary>
502 : class range named_range(const std::string &name);
503 :
504 : /// <summary>
505 : /// Deletes the named range with the given name.
506 : /// </summary>
507 : void remove_named_range(const std::string &name);
508 :
509 : // Serialization/Deserialization
510 :
511 : /// <summary>
512 : /// Serializes the workbook into an XLSX file and saves the bytes into
513 : /// byte vector data.
514 : /// </summary>
515 : void save(std::vector<std::uint8_t> &data) const;
516 :
517 : /// <summary>
518 : /// Serializes the workbook into an XLSX file encrypted with the given password
519 : /// and saves the bytes into byte vector data.
520 : /// </summary>
521 : void save(std::vector<std::uint8_t> &data, const std::string &password) const;
522 :
523 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
524 : /// <summary>
525 : /// Serializes the workbook into an XLSX file encrypted with the given password
526 : /// and saves the bytes into byte vector data.
527 : /// </summary>
528 : void save(std::vector<std::uint8_t> &data, std::u8string_view password) const;
529 : #endif
530 :
531 : /// <summary>
532 : /// Serializes the workbook into an XLSX file and saves the data into a file
533 : /// named filename.
534 : /// </summary>
535 : void save(const std::string &filename) const;
536 :
537 : /// <summary>
538 : /// Serializes the workbook into an XLSX file encrypted with the given password
539 : /// and loads the bytes into a file named filename.
540 : /// </summary>
541 : void save(const std::string &filename, const std::string &password) const;
542 :
543 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
544 : /// <summary>
545 : /// Serializes the workbook into an XLSX file and saves the data into a file
546 : /// named filename.
547 : /// </summary>
548 : void save(std::u8string_view filename) const;
549 :
550 : /// <summary>
551 : /// Serializes the workbook into an XLSX file encrypted with the given password
552 : /// and loads the bytes into a file named filename.
553 : /// </summary>
554 : void save(std::u8string_view filename, std::u8string_view password) const;
555 : #endif
556 :
557 : #ifdef _MSC_VER
558 : /// <summary>
559 : /// Serializes the workbook into an XLSX file and saves the data into a file
560 : /// named filename.
561 : /// </summary>
562 : void save(const std::wstring &filename) const;
563 :
564 : /// <summary>
565 : /// Serializes the workbook into an XLSX file encrypted with the given password
566 : /// and loads the bytes into a file named filename.
567 : /// </summary>
568 : void save(const std::wstring &filename, const std::string &password) const;
569 : #endif
570 :
571 : /// <summary>
572 : /// Serializes the workbook into an XLSX file and saves the data into a file
573 : /// named filename.
574 : /// </summary>
575 : void save(const xlnt::path &filename) const;
576 :
577 : /// <summary>
578 : /// Serializes the workbook into an XLSX file encrypted with the given password
579 : /// and loads the bytes into a file named filename.
580 : /// </summary>
581 : void save(const xlnt::path &filename, const std::string &password) const;
582 :
583 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
584 : /// <summary>
585 : /// Serializes the workbook into an XLSX file encrypted with the given password
586 : /// and loads the bytes into a file named filename.
587 : /// </summary>
588 : void save(const xlnt::path &filename, std::u8string_view password) const;
589 : #endif
590 :
591 : /// <summary>
592 : /// Serializes the workbook into an XLSX file and saves the data into stream.
593 : /// </summary>
594 : void save(std::ostream &stream) const;
595 :
596 : /// <summary>
597 : /// Serializes the workbook into an XLSX file encrypted with the given password
598 : /// and loads the bytes into the given stream.
599 : /// </summary>
600 : void save(std::ostream &stream, const std::string &password) const;
601 :
602 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
603 : /// <summary>
604 : /// Serializes the workbook into an XLSX file encrypted with the given password
605 : /// and loads the bytes into the given stream.
606 : /// </summary>
607 : void save(std::ostream &stream, std::u8string_view password) const;
608 : #endif
609 :
610 : /// <summary>
611 : /// Interprets byte vector data as an XLSX file and sets the content of this
612 : /// workbook to match that file.
613 : /// </summary>
614 : void load(const std::vector<std::uint8_t> &data);
615 :
616 : /// <summary>
617 : /// Interprets byte vector data as an XLSX file encrypted with the
618 : /// given password and sets the content of this workbook to match that file.
619 : /// </summary>
620 : void load(const std::vector<std::uint8_t> &data, const std::string &password);
621 :
622 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
623 : /// <summary>
624 : /// Interprets byte vector data as an XLSX file encrypted with the
625 : /// given password and sets the content of this workbook to match that file.
626 : /// </summary>
627 : void load(const std::vector<std::uint8_t> &data, std::u8string_view password);
628 : #endif
629 :
630 : /// <summary>
631 : /// Interprets file with the given filename as an XLSX file and sets
632 : /// the content of this workbook to match that file.
633 : /// </summary>
634 : void load(const std::string &filename);
635 :
636 : /// <summary>
637 : /// Interprets file with the given filename as an XLSX file encrypted with the
638 : /// given password and sets the content of this workbook to match that file.
639 : /// </summary>
640 : void load(const std::string &filename, const std::string &password);
641 :
642 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
643 : /// <summary>
644 : /// Interprets file with the given filename as an XLSX file and sets
645 : /// the content of this workbook to match that file.
646 : /// </summary>
647 : void load(std::u8string_view filename);
648 :
649 : /// <summary>
650 : /// Interprets file with the given filename as an XLSX file encrypted with the
651 : /// given password and sets the content of this workbook to match that file.
652 : /// </summary>
653 : void load(std::u8string_view filename, std::u8string_view password);
654 : #endif
655 :
656 :
657 : #ifdef _MSC_VER
658 : /// <summary>
659 : /// Interprets file with the given filename as an XLSX file and sets
660 : /// the content of this workbook to match that file.
661 : /// </summary>
662 : void load(const std::wstring &filename);
663 :
664 : /// <summary>
665 : /// Interprets file with the given filename as an XLSX file encrypted with the
666 : /// given password and sets the content of this workbook to match that file.
667 : /// </summary>
668 : void load(const std::wstring &filename, const std::string &password);
669 : #endif
670 :
671 : /// <summary>
672 : /// Interprets file with the given filename as an XLSX file and sets the
673 : /// content of this workbook to match that file.
674 : /// </summary>
675 : void load(const xlnt::path &filename);
676 :
677 : /// <summary>
678 : /// Interprets file with the given filename as an XLSX file encrypted with the
679 : /// given password and sets the content of this workbook to match that file.
680 : /// </summary>
681 : void load(const xlnt::path &filename, const std::string &password);
682 :
683 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
684 : /// <summary>
685 : /// Interprets file with the given filename as an XLSX file encrypted with the
686 : /// given password and sets the content of this workbook to match that file.
687 : /// </summary>
688 : void load(const xlnt::path &filename, std::u8string_view password);
689 : #endif
690 :
691 : /// <summary>
692 : /// Interprets data in stream as an XLSX file and sets the content of this
693 : /// workbook to match that file.
694 : /// </summary>
695 : void load(std::istream &stream);
696 :
697 : /// <summary>
698 : /// Interprets data in stream as an XLSX file encrypted with the given password
699 : /// and sets the content of this workbook to match that file.
700 : /// </summary>
701 : void load(std::istream &stream, const std::string &password);
702 :
703 : #if XLNT_HAS_FEATURE(U8_STRING_VIEW)
704 : /// <summary>
705 : /// Interprets data in stream as an XLSX file encrypted with the given password
706 : /// and sets the content of this workbook to match that file.
707 : /// </summary>
708 : void load(std::istream &stream, std::u8string_view password);
709 : #endif
710 :
711 : // View
712 :
713 : /// <summary>
714 : /// Returns true if this workbook has a view.
715 : /// </summary>
716 : bool has_view() const;
717 :
718 : /// <summary>
719 : /// Returns the view.
720 : /// </summary>
721 : workbook_view view() const;
722 :
723 : /// <summary>
724 : /// Sets the view to view.
725 : /// </summary>
726 : void view(const workbook_view &view);
727 :
728 : // Properties
729 :
730 : /// <summary>
731 : /// Returns true if a code name has been set for this workbook.
732 : /// </summary>
733 : bool has_code_name() const;
734 :
735 : /// <summary>
736 : /// Returns the code name that was set for this workbook.
737 : /// </summary>
738 : std::string code_name() const;
739 :
740 : /// <summary>
741 : /// Sets the code name of this workbook to code_name.
742 : /// </summary>
743 : void code_name(const std::string &code_name);
744 :
745 : /// <summary>
746 : /// Returns true if this workbook has a file version.
747 : /// </summary>
748 : bool has_file_version() const;
749 :
750 : /// <summary>
751 : /// Returns the AppName workbook file property.
752 : /// </summary>
753 : std::string app_name() const;
754 :
755 : /// <summary>
756 : /// Returns the LastEdited workbook file property.
757 : /// </summary>
758 : std::size_t last_edited() const;
759 :
760 : /// <summary>
761 : /// Returns the LowestEdited workbook file property.
762 : /// </summary>
763 : std::size_t lowest_edited() const;
764 :
765 : /// <summary>
766 : /// Returns the RupBuild workbook file property.
767 : /// </summary>
768 : std::size_t rup_build() const;
769 :
770 : // Theme
771 :
772 : /// <summary>
773 : /// Returns true if this workbook has a theme defined.
774 : /// </summary>
775 : bool has_theme() const;
776 :
777 : /// <summary>
778 : /// Returns a const reference to this workbook's theme.
779 : /// </summary>
780 : const xlnt::theme &theme() const;
781 :
782 : /// <summary>
783 : /// Sets the theme to value.
784 : /// </summary>
785 : void theme(const class theme &value);
786 :
787 : // Formats
788 :
789 : /// <summary>
790 : /// Returns the cell format at the given index. The index is the position of
791 : /// the format in xl/styles.xml.
792 : /// </summary>
793 : xlnt::format format(std::size_t format_index);
794 :
795 : /// <summary>
796 : /// Returns the cell format at the given index. The index is the position of
797 : /// the format in xl/styles.xml.
798 : /// </summary>
799 : const xlnt::format format(std::size_t format_index) const;
800 :
801 : /// <summary>
802 : /// Creates a new format and returns it.
803 : /// </summary>
804 : xlnt::format create_format(bool default_format = false);
805 :
806 : /// <summary>
807 : /// Clear all cell-level formatting and formats from the styelsheet. This leaves
808 : /// all other styling in place (e.g. named styles).
809 : /// </summary>
810 : void clear_formats();
811 :
812 : /// <summary>
813 : /// Returns the number of formats in this workbook.
814 : /// </summary>
815 : std::size_t format_count() const;
816 :
817 : // Styles
818 :
819 : /// <summary>
820 : /// Returns true if this workbook has a style with a name of name.
821 : /// </summary>
822 : bool has_style(const std::string &name) const;
823 :
824 : /// <summary>
825 : /// Returns the named style with the given name.
826 : /// </summary>
827 : class style style(const std::string &name);
828 :
829 : /// <summary>
830 : /// Returns the named style with the given name.
831 : /// </summary>
832 : const class style style(const std::string &name) const;
833 :
834 : /// <summary>
835 : /// Creates a new style and returns it.
836 : /// </summary>
837 : class style create_style(const std::string &name);
838 :
839 : /// <summary>
840 : /// Creates a new style and returns it.
841 : /// </summary>
842 : class style create_builtin_style(std::size_t builtin_id);
843 :
844 : /// <summary>
845 : /// Clear all named styles from cells and remove the styles from
846 : /// from the styelsheet. This leaves all other styling in place
847 : /// (e.g. cell formats).
848 : /// </summary>
849 : void clear_styles();
850 :
851 : /// <summary>
852 : /// Sets the default slicer style to the given value.
853 : /// </summary>
854 : void default_slicer_style(const std::string &value);
855 :
856 : /// <summary>
857 : /// Returns the default slicer style.
858 : /// </summary>
859 : std::string default_slicer_style() const;
860 :
861 : /// <summary>
862 : /// Enables knownFonts in stylesheet.
863 : /// </summary>
864 : void enable_known_fonts();
865 :
866 : /// <summary>
867 : /// Disables knownFonts in stylesheet.
868 : /// </summary>
869 : void disable_known_fonts();
870 :
871 : /// <summary>
872 : /// Returns true if knownFonts are enabled in the stylesheet.
873 : /// </summary>
874 : bool known_fonts_enabled() const;
875 :
876 : // Manifest
877 :
878 : /// <summary>
879 : /// Returns a reference to the workbook's internal manifest.
880 : /// </summary>
881 : class manifest &manifest();
882 :
883 : /// <summary>
884 : /// Returns a reference to the workbook's internal manifest.
885 : /// </summary>
886 : const class manifest &manifest() const;
887 :
888 : // shared strings
889 :
890 : /// <summary>
891 : /// Append a shared string to the shared string collection in this workbook.
892 : /// This should not generally be called unless you know what you're doing.
893 : /// If allow_duplicates is false and the string is already in the collection,
894 : /// it will not be added. Returns the index of the added string.
895 : /// </summary>
896 : std::size_t add_shared_string(const rich_text &shared, bool allow_duplicates = false);
897 :
898 : /// <summary>
899 : /// Returns a reference to the shared string related to the specified index
900 : /// </summary>
901 : const rich_text &shared_strings(std::size_t index) const;
902 :
903 : /// <summary>
904 : /// Returns a reference to the shared strings being used by cells
905 : /// in this workbook.
906 : /// </summary>
907 : std::vector<rich_text> &shared_strings();
908 :
909 : /// <summary>
910 : /// Returns a reference to the shared strings being used by cells
911 : /// in this workbook.
912 : /// </summary>
913 : const std::vector<rich_text> &shared_strings() const;
914 :
915 : // Thumbnail
916 :
917 : /// <summary>
918 : /// Sets the workbook's thumbnail to the given vector of bytes, thumbnail,
919 : /// with the given extension (e.g. jpg) and content_type (e.g. image/jpeg).
920 : /// </summary>
921 : void thumbnail(const std::vector<std::uint8_t> &thumbnail,
922 : const std::string &extension, const std::string &content_type);
923 :
924 : /// <summary>
925 : /// Returns a vector of bytes representing the workbook's thumbnail.
926 : /// </summary>
927 : const std::vector<std::uint8_t> &thumbnail() const;
928 :
929 : /// <summary>
930 : /// Returns stored binary data.
931 : /// </summary>
932 : const std::unordered_map<std::string, std::vector<std::uint8_t>>& binaries() const;
933 :
934 : // Calculation properties
935 :
936 : /// <summary>
937 : /// Returns true if this workbook has any calculation properties set.
938 : /// </summary>
939 : bool has_calculation_properties() const;
940 :
941 : /// <summary>
942 : /// Returns the calculation properties used in this workbook.
943 : /// </summary>
944 : class calculation_properties calculation_properties() const;
945 :
946 : /// <summary>
947 : /// Sets the calculation properties of this workbook to props.
948 : /// </summary>
949 : void calculation_properties(const class calculation_properties &props);
950 :
951 : /// <summary>
952 : /// Returns true if this workbook is equal to other. If compare_by_reference is true, the comparison
953 : /// will only check that both workbook instances point to the same internal workbook. Otherwise,
954 : /// if compare_by_reference is false, all workbook properties except for the abs_path are compared.
955 : /// </summary>
956 : bool compare(const workbook &other, bool compare_by_reference) const;
957 :
958 : // Operators
959 :
960 : /// <summary>
961 : /// Set the contents of this workbook to be equal to those of "other".
962 : /// Creates a shallow copy, copying the workbook's internal pointers.
963 : /// </summary>
964 2 : workbook &operator=(const workbook &other) = default;
965 :
966 : /// <summary>
967 : /// Set the contents of this workbook to be equal to those of "other"
968 : /// by consuming (moving) the "other" instance.
969 : /// </summary>
970 10 : workbook &operator=(workbook &&other) = default;
971 :
972 : /// <summary>
973 : /// Return the worksheet with a title of "name".
974 : /// </summary>
975 : worksheet operator[](const std::string &name);
976 :
977 : /// <summary>
978 : /// Return the worksheet at "index".
979 : /// </summary>
980 : worksheet operator[](std::size_t index);
981 :
982 : /// <summary>
983 : /// Return true if this workbook internal implementation points to the same
984 : /// memory as rhs's.
985 : /// </summary>
986 : bool operator==(const workbook &rhs) const;
987 :
988 : /// <summary>
989 : /// Return true if this workbook internal implementation doesn't point to the same
990 : /// memory as rhs's.
991 : /// </summary>
992 : bool operator!=(const workbook &rhs) const;
993 :
994 : private:
995 : friend class streaming_workbook_reader;
996 : friend class worksheet;
997 : friend class detail::xlsx_consumer;
998 : friend class detail::xlsx_producer;
999 : friend struct detail::worksheet_impl;
1000 :
1001 : /// <summary>
1002 : /// Private constructor. Constructs a workbook from an implementation pointer.
1003 : /// Used by static constructor to resolve circular construction dependency.
1004 : /// </summary>
1005 : workbook(std::shared_ptr<detail::workbook_impl> impl);
1006 :
1007 : /// <summary>
1008 : /// Private constructor. Constructs a workbook from an implementation pointer.
1009 : /// Used by static constructor to resolve circular construction dependency.
1010 : /// </summary>
1011 : workbook(std::weak_ptr<detail::workbook_impl> impl);
1012 :
1013 : /// <summary>
1014 : /// Internal function to set the impl.
1015 : /// </summary>
1016 : void set_impl(std::shared_ptr<detail::workbook_impl> impl);
1017 :
1018 : /// <summary>
1019 : /// load the encrpyted xlsx file at path
1020 : /// </summary>
1021 : template <typename T>
1022 : void construct(const xlnt::path &file, const T &password);
1023 :
1024 : /// <summary>
1025 : /// construct the workbook from any data stream where the data is the binary form of an encrypted workbook
1026 : /// </summary>
1027 : template <typename T>
1028 : void construct(std::istream &data, const T &password);
1029 :
1030 : /// <summary>
1031 : /// Serializes the workbook into an XLSX file encrypted with the given password
1032 : /// and saves the bytes into byte vector data.
1033 : /// </summary>
1034 : template <typename T>
1035 : void save_internal(std::vector<std::uint8_t> &data, const T &password) const;
1036 :
1037 : /// <summary>
1038 : /// Serializes the workbook into an XLSX file and saves the data into a file
1039 : /// named filename.
1040 : /// </summary>
1041 : template <typename T>
1042 : void save_internal(const T &filename) const;
1043 :
1044 : /// <summary>
1045 : /// Serializes the workbook into an XLSX file encrypted with the given password
1046 : /// and loads the bytes into a file named filename.
1047 : /// </summary>
1048 : template <typename T>
1049 : void save_internal(const T &filename, const T &password) const;
1050 :
1051 : /// <summary>
1052 : /// Serializes the workbook into an XLSX file encrypted with the given password
1053 : /// and loads the bytes into a file named filename.
1054 : /// </summary>
1055 : template <typename T>
1056 : void save_internal(const xlnt::path &filename, const T &password) const;
1057 :
1058 : /// <summary>
1059 : /// Serializes the workbook into an XLSX file encrypted with the given password
1060 : /// and loads the bytes into the given stream.
1061 : /// </summary>
1062 : template <typename T>
1063 : void save_internal(std::ostream &stream, const T &password) const;
1064 :
1065 : /// <summary>
1066 : /// Interprets byte vector data as an XLSX file encrypted with the
1067 : /// given password and sets the content of this workbook to match that file.
1068 : /// </summary>
1069 : template <typename T>
1070 : void load_internal(const std::vector<std::uint8_t> &data, const T &password);
1071 :
1072 : /// <summary>
1073 : /// Interprets file with the given filename as an XLSX file and sets
1074 : /// the content of this workbook to match that file.
1075 : /// </summary>
1076 : template <typename T>
1077 : void load_internal(const T &filename);
1078 :
1079 : /// <summary>
1080 : /// Interprets file with the given filename as an XLSX file encrypted with the
1081 : /// given password and sets the content of this workbook to match that file.
1082 : /// </summary>
1083 : template <typename T>
1084 : void load_internal(const T &filename, const T &password);
1085 :
1086 : /// <summary>
1087 : /// Interprets file with the given filename as an XLSX file encrypted with the
1088 : /// given password and sets the content of this workbook to match that file.
1089 : /// </summary>
1090 : template <typename T>
1091 : void load_internal(const xlnt::path &filename, const T &password);
1092 :
1093 : /// <summary>
1094 : /// Interprets data in stream as an XLSX file encrypted with the given password
1095 : /// and sets the content of this workbook to match that file.
1096 : /// </summary>
1097 : template <typename T>
1098 : void load_internal(std::istream &stream, const T &password);
1099 :
1100 : /// <summary>
1101 : /// Returns a reference to the workbook implementation structure. Provides
1102 : /// a nicer interface than constantly dereferencing workbook::d_.
1103 : /// </summary>
1104 : detail::workbook_impl &impl();
1105 :
1106 : /// <summary>
1107 : /// Returns a reference to the workbook implementation structure. Provides
1108 : /// a nicer interface than constantly dereferencing workbook::d_.
1109 : /// </summary>
1110 : const detail::workbook_impl &impl() const;
1111 :
1112 : /// <summary>
1113 : /// Adds a package-level part of the given type to the manifest if it doesn't
1114 : /// already exist. The part will have a path and content type of the default
1115 : /// for that particular relationship type.
1116 : /// </summary>
1117 : void register_package_part(relationship_type type);
1118 :
1119 : /// <summary>
1120 : /// Adds a workbook-level part of the given type to the manifest if it doesn't
1121 : /// already exist. The part will have a path and content type of the default
1122 : /// for that particular relationship type. It will be a relationship target
1123 : /// of this workbook.
1124 : /// </summary>
1125 : void register_workbook_part(relationship_type type);
1126 :
1127 : /// <summary>
1128 : /// Adds a worksheet-level part of the given type to the manifest if it doesn't
1129 : /// already exist. The part will have a path and content type of the default
1130 : /// for that particular relationship type. It will be a relationship target
1131 : /// of the given worksheet, ws.
1132 : /// </summary>
1133 : void register_worksheet_part(worksheet ws, relationship_type type);
1134 :
1135 : /// <summary>
1136 : /// Removes calcChain part from manifest if no formulae remain in workbook.
1137 : /// </summary>
1138 : void garbage_collect_formulae();
1139 :
1140 : /// <summary>
1141 : /// Update extended workbook properties titlesOfParts and headingPairs when sheets change.
1142 : /// </summary>
1143 : void update_sheet_properties();
1144 :
1145 : /// <summary>
1146 : /// Swaps the data held in this workbook with workbook other.
1147 : /// </summary>
1148 : void swap(workbook &other);
1149 :
1150 : /// <summary>
1151 : /// Sheet 1 should be rId1, sheet 2 should be rId2, etc.
1152 : /// </summary>
1153 : void reorder_relationships();
1154 :
1155 : /// <summary>
1156 : /// Sets the default format
1157 : /// </summary>
1158 : void default_format(const xlnt::format& format);
1159 :
1160 : /// <summary>
1161 : /// An opaque pointer to a structure that holds all of the data relating to this workbook.
1162 : /// </summary>
1163 : std::shared_ptr<detail::workbook_impl> d_;
1164 : };
1165 :
1166 : } // namespace xlnt
|