• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xlnt-community / xlnt / 1d4ee6f6-21a4-4379-9731-a97ba8c57175

25 Jan 2026 09:50AM UTC coverage: 83.928% (+1.1%) from 82.793%
1d4ee6f6-21a4-4379-9731-a97ba8c57175

Pull #87

circleci

doomlaur
Improve docuentation on exceptions thrown for invalid worksheet titles
Pull Request #87: Improve documentation when exceptions are thrown (fixes #81)

15263 of 19954 branches covered (76.49%)

722 of 901 new or added lines in 37 files covered. (80.13%)

15 existing lines in 5 files now uncovered.

12491 of 14883 relevant lines covered (83.93%)

12216.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.93
./source/workbook/workbook.cpp
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
#include <algorithm>
27
#include <fstream>
28
#include <functional>
29

30
#include <xlnt/cell/cell.hpp>
31
#include <xlnt/packaging/manifest.hpp>
32
#include <xlnt/packaging/relationship.hpp>
33
#include <xlnt/styles/alignment.hpp>
34
#include <xlnt/styles/border.hpp>
35
#include <xlnt/styles/fill.hpp>
36
#include <xlnt/styles/font.hpp>
37
#include <xlnt/styles/format.hpp>
38
#include <xlnt/styles/number_format.hpp>
39
#include <xlnt/styles/protection.hpp>
40
#include <xlnt/styles/style.hpp>
41
#include <xlnt/utils/exceptions.hpp>
42
#include <xlnt/utils/path.hpp>
43
#include <xlnt/utils/variant.hpp>
44
#include <xlnt/workbook/metadata_property.hpp>
45
#include <xlnt/workbook/named_range.hpp>
46
#include <xlnt/workbook/theme.hpp>
47
#include <xlnt/workbook/workbook.hpp>
48
#include <xlnt/workbook/workbook_view.hpp>
49
#include <xlnt/workbook/worksheet_iterator.hpp>
50
#include <xlnt/worksheet/header_footer.hpp>
51
#include <xlnt/worksheet/range.hpp>
52
#include <xlnt/worksheet/worksheet.hpp>
53
#include <detail/constants.hpp>
54
#include <detail/default_case.hpp>
55
#include <detail/implementations/cell_impl.hpp>
56
#include <detail/implementations/workbook_impl.hpp>
57
#include <detail/implementations/worksheet_impl.hpp>
58
#include <detail/serialization/excel_thumbnail.hpp>
59
#include <detail/serialization/open_stream.hpp>
60
#include <detail/serialization/parsers.hpp>
61
#include <detail/serialization/vector_streambuf.hpp>
62
#include <detail/serialization/xlsx_consumer.hpp>
63
#include <detail/serialization/xlsx_producer.hpp>
64

65
namespace {
66

67
using xlnt::detail::open_stream;
68

69
template <typename T>
70
std::vector<T> keys(const std::vector<std::pair<T, xlnt::variant>> &container)
86✔
71
{
72
    auto result = std::vector<T>();
86✔
73
    result.reserve(container.size());
86✔
74
    auto iter = container.begin();
86✔
75

76
    while (iter != container.end())
679✔
77
    {
78
        result.push_back((iter++)->first);
593✔
79
    }
80

81
    return result;
86✔
82
}
×
83

84
template <typename T>
85
bool contains(const std::vector<std::pair<T, xlnt::variant>> &container, const T key)
879✔
86
{
87
    for (const auto &iter : container)
3,931✔
88
    {
89
        if (iter.first == key)
3,924✔
90
        {
91
            return true;
872✔
92
        }
93
    }
94

95
    return false;
7✔
96
}
97

98
xlnt::path default_path(xlnt::relationship_type type, std::size_t index = 0)
3,520✔
99
{
100
    using xlnt::path;
101
    using xlnt::relationship_type;
102

103
    switch (type)
3,520!
104
    {
105
    case relationship_type::calculation_chain:
12✔
106
        return path("/xl/calcChain.xml");
24✔
107
    case relationship_type::chartsheet:
×
108
        return path("/xl/sheets/.xml");
×
109
    case relationship_type::comments:
×
110
        return path("/xl/comments.xml");
×
111
    case relationship_type::connections:
×
112
        return path("/xl/connections.xml");
×
113
    case relationship_type::core_properties:
678✔
114
        return path("/docProps/core.xml");
1,356✔
115
    case relationship_type::custom_properties:
2✔
116
        return path("/docProps/custom.xml");
4✔
117
    case relationship_type::custom_property:
×
118
        return path("/xl/customProperty.xml");
×
119
    case relationship_type::custom_xml_mappings:
×
120
        return path("/xl/customXmlMappings.xml");
×
121
    case relationship_type::dialogsheet:
×
122
        return path("/xl/dialogsheets/sheet.xml");
×
123
    case relationship_type::drawings:
×
124
        return path("/xl/drawings/drawing.xml");
×
125
    case relationship_type::extended_properties:
678✔
126
        return path("/docProps/app.xml");
1,356✔
127
    case relationship_type::external_workbook_references:
×
128
        return path("/xl/external.xml");
×
129
    case relationship_type::hyperlink:
×
130
        return path("/xl/hyperlink.xml");
×
131
    case relationship_type::image:
×
132
        return path("?");
×
133
    case relationship_type::office_document:
678✔
134
        return path("/xl/workbook.xml");
1,356✔
135
    case relationship_type::pivot_table:
×
136
        return path("/xl/pivotTable.xml");
×
137
    case relationship_type::pivot_table_cache_definition:
×
138
        return path("?");
×
139
    case relationship_type::pivot_table_cache_records:
×
140
        return path("?");
×
141
    case relationship_type::printer_settings:
×
142
        return path("/xl/printerSettings.xml");
×
143
    case relationship_type::query_table:
×
144
        return path("/xl/queryTable.xml");
×
145
    case relationship_type::revision_log:
×
146
        return path("/xl/revisionLog.xml");
×
147
    case relationship_type::shared_string_table:
116✔
148
        return path("/xl/sharedStrings.xml");
232✔
149
    case relationship_type::shared_workbook:
×
150
        return path("/xl/sharedWorkbook.xml");
×
151
    case relationship_type::shared_workbook_revision_headers:
×
152
        return path("?");
×
153
    case relationship_type::shared_workbook_user_data:
×
154
        return path("?");
×
155
    case relationship_type::single_cell_table_definitions:
×
156
        return path("?");
×
157
    case relationship_type::stylesheet:
678✔
158
        return path("/xl/styles.xml");
1,356✔
159
    case relationship_type::table_definition:
×
160
        return path("/xl/tableDefinition.xml");
×
161
    case relationship_type::theme:
678✔
162
        return path("/xl/theme/theme1.xml");
1,356✔
163
    case relationship_type::thumbnail:
×
164
        return path("/docProps/thumbnail.jpg");
×
165
    case relationship_type::unknown:
×
166
        return path("/xl/unknown.xml");
×
167
    case relationship_type::vml_drawing:
×
168
        return path("/xl/vmlDrawing.xml");
×
169
    case relationship_type::volatile_dependencies:
×
170
        return path("/xl/volatileDependencies.xml");
×
171
    case relationship_type::vbaproject:
×
172
        return path("/xl/vbaProject.bin");
×
173
    case relationship_type::worksheet:
×
174
        return path("/xl/worksheets/sheet" + std::to_string(index) + ".xml");
×
175
    }
176

177
    default_case(path("/xl/unknownPart.xml"));
×
178
}
179

180
std::string content_type(xlnt::relationship_type type)
1,760✔
181
{
182
    using xlnt::relationship_type;
183

184
    switch (type)
1,760!
185
    {
186
    case relationship_type::calculation_chain:
6✔
187
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml";
12✔
188
    case relationship_type::chartsheet:
×
189
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.chartsheet+xml";
×
190
    case relationship_type::comments:
×
191
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml";
×
192
    case relationship_type::connections:
×
193
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.connections+xml";
×
194
    case relationship_type::core_properties:
339✔
195
        return "application/vnd.openxmlformats-package.core-properties+xml";
678✔
196
    case relationship_type::custom_properties:
1✔
197
        return "application/vnd.openxmlformats-officedocument.custom-properties+xml";
2✔
198
    case relationship_type::custom_property:
×
NEW
199
        throw xlnt::unhandled_switch_case("xlnt::relationship_type::custom_property");
×
200
    case relationship_type::custom_xml_mappings:
×
201
        return "application/xml";
×
202
    case relationship_type::dialogsheet:
×
203
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.dialogsheet+xml";
×
204
    case relationship_type::drawings:
×
205
        return "application/vnd.openxmlformats-officedocument.drawing+xml";
×
206
    case relationship_type::extended_properties:
339✔
207
        return "application/vnd.openxmlformats-officedocument.extended-properties+xml";
678✔
208
    case relationship_type::external_workbook_references:
×
209
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml";
×
210
    case relationship_type::hyperlink:
×
NEW
211
        throw xlnt::unhandled_switch_case("xlnt::relationship_type::hyperlink");
×
212
    case relationship_type::image:
×
NEW
213
        throw xlnt::unhandled_switch_case("xlnt::relationship_type::image");
×
214
    case relationship_type::office_document:
339✔
215
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
678✔
216
    case relationship_type::pivot_table:
×
217
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml";
×
218
    case relationship_type::pivot_table_cache_definition:
×
219
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml";
×
220
    case relationship_type::pivot_table_cache_records:
×
221
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml";
×
222
    case relationship_type::printer_settings:
×
223
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings";
×
224
    case relationship_type::query_table:
×
225
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.queryTable+xml";
×
226
    case relationship_type::revision_log:
×
227
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionLog+xml";
×
228
    case relationship_type::shared_string_table:
58✔
229
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml";
116✔
230
    case relationship_type::shared_workbook:
×
NEW
231
        throw xlnt::unhandled_switch_case("xlnt::relationship_type::shared_workbook");
×
232
    case relationship_type::shared_workbook_revision_headers:
×
233
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.revisionHeaders+xml";
×
234
    case relationship_type::shared_workbook_user_data:
×
235
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.userNames+xml";
×
236
    case relationship_type::single_cell_table_definitions:
×
237
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.tableSingleCells+xml";
×
238
    case relationship_type::stylesheet:
339✔
239
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml";
678✔
240
    case relationship_type::table_definition:
×
241
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml";
×
242
    case relationship_type::theme:
339✔
243
        return "application/vnd.openxmlformats-officedocument.theme+xml";
678✔
244
    case relationship_type::thumbnail:
×
245
        return "image/jpeg";
×
246
    case relationship_type::unknown:
×
247
        return "";
×
248
    case relationship_type::vml_drawing:
×
249
        return "application/vnd.openxmlformats-officedocument.vmlDrawing";
×
250
    case relationship_type::volatile_dependencies:
×
251
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.volatileDependencies+xml";
×
252
    case relationship_type::vbaproject:
×
253
        return "application/vnd.ms-office.vbaProject";
×
254
    case relationship_type::worksheet:
×
255
        return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
×
256
    }
257

258
    default_case("application/xml");
×
259
}
260

261
} // namespace
262

263
namespace xlnt {
264

265
bool workbook::has_core_property(xlnt::core_property type) const
2✔
266
{
267
    return ::contains(d_->core_properties_, type);
2✔
268
}
269

270
std::vector<xlnt::core_property> workbook::core_properties() const
42✔
271
{
272
    return keys(d_->core_properties_);
42✔
273
}
274

275
variant workbook::core_property(xlnt::core_property type) const
185✔
276
{
277
    for (auto iter : d_->core_properties_)
563✔
278
    {
279
        if (iter.first == type)
562✔
280
        {
281
            return iter.second;
184✔
282
        }
283
    }
562✔
284

285
    return {};
1✔
286
}
287

288
void workbook::core_property(xlnt::core_property type, const variant &value)
1,784✔
289
{
290
    register_package_part(relationship_type::core_properties);
1,784✔
291

292
    for (auto &iter : d_->core_properties_)
4,858✔
293
    {
294
        if (iter.first == type)
3,080✔
295
        {
296
            iter.second = value;
6✔
297
            return;
6✔
298
        }
299
    }
300

301
    d_->core_properties_.push_back({type, value});
1,778✔
302
}
303

304
bool workbook::has_extended_property(xlnt::extended_property type) const
874✔
305
{
306
    return ::contains(d_->extended_properties_, type);
874✔
307
}
308

309
std::vector<xlnt::extended_property> workbook::extended_properties() const
42✔
310
{
311
    return keys(d_->extended_properties_);
42✔
312
}
313

314
void workbook::extended_property(xlnt::extended_property type, const variant &value)
4,963✔
315
{
316
    register_package_part(relationship_type::extended_properties);
4,963✔
317

318
    for (auto &iter : d_->extended_properties_)
26,443✔
319
    {
320
        if (iter.first == type)
22,308✔
321
        {
322
            iter.second = value;
828✔
323
            return;
828✔
324
        }
325
    }
326

327
    d_->extended_properties_.push_back({type, value});
4,135✔
328
}
329

330
variant workbook::extended_property(xlnt::extended_property type) const
410✔
331
{
332
    for (auto iter : d_->extended_properties_)
2,290✔
333
    {
334
        if (iter.first == type)
2,289✔
335
        {
336
            return iter.second;
409✔
337
        }
338
    }
2,289✔
339

340
    return {};
1✔
341
}
342

343
bool workbook::has_custom_property(const std::string &property_name) const
3✔
344
{
345
    return ::contains(d_->custom_properties_, property_name);
3✔
346
}
347

348
std::vector<std::string> workbook::custom_properties() const
2✔
349
{
350
    return keys(d_->custom_properties_);
2✔
351
}
352

353
void workbook::custom_property(const std::string &property_name, const variant &value)
8✔
354
{
355
    register_package_part(relationship_type::custom_properties);
8✔
356

357
    for (auto &iter : d_->custom_properties_)
9✔
358
    {
359
        if (iter.first == property_name)
1!
360
        {
361
            iter.second = value;
×
362
            return;
×
363
        }
364
    }
365

366
    d_->custom_properties_.push_back({property_name, value});
8✔
367
}
368

369
variant workbook::custom_property(const std::string &property_name) const
5✔
370
{
371
    for (auto iter : d_->custom_properties_)
5✔
372
    {
373
        if (iter.first == property_name)
4!
374
        {
375
            return iter.second;
4✔
376
        }
377
    }
4!
378

379
    return {};
1✔
380
}
381

382
void workbook::abs_path(const std::string &path)
×
383
{
384
    d_->abs_path_ = path;
×
385
}
×
386

387
void workbook::arch_id_flags(const std::size_t flags)
×
388
{
389
    d_->arch_id_flags_ = flags;
×
390
}
×
391

392
workbook workbook::empty()
339✔
393
{
394
    workbook wb(std::make_shared<detail::workbook_impl>());
339✔
395

396
    wb.register_package_part(relationship_type::office_document);
339✔
397

398
    wb.d_->manifest_.register_default_type("rels",
1,356✔
399
        "application/vnd.openxmlformats-package.relationships+xml");
400
    wb.d_->manifest_.register_default_type("xml",
1,695✔
401
        "application/xml");
402

403
    wb.thumbnail(excel_thumbnail(), "jpeg", "image/jpeg");
1,017✔
404

405
    wb.core_property(xlnt::core_property::creator, "Microsoft Office User");
339✔
406
    wb.core_property(xlnt::core_property::last_modified_by, "Microsoft Office User");
339✔
407
    wb.core_property(xlnt::core_property::created, datetime(2016, 8, 12, 3, 16, 56));
339✔
408
    wb.core_property(xlnt::core_property::modified, datetime(2016, 8, 12, 3, 17, 16));
339✔
409

410
    wb.extended_property(xlnt::extended_property::application, "Microsoft Macintosh Excel");
339✔
411
    wb.extended_property(xlnt::extended_property::doc_security, 0);
339✔
412
    wb.extended_property(xlnt::extended_property::scale_crop, false);
339✔
413
    wb.extended_property(xlnt::extended_property::heading_pairs, std::vector<variant>{variant("Worksheets"), variant(1)});
1,356!
414
    wb.extended_property(xlnt::extended_property::titles_of_parts, {"Sheet1"});
339✔
415
    wb.extended_property(xlnt::extended_property::company, "");
339✔
416
    wb.extended_property(xlnt::extended_property::links_up_to_date, false);
339✔
417
    wb.extended_property(xlnt::extended_property::shared_doc, false);
339✔
418
    wb.extended_property(xlnt::extended_property::hyperlinks_changed, false);
339✔
419
    wb.extended_property(xlnt::extended_property::app_version, "15.0300");
339✔
420

421
    wb.d_->file_version_ = detail::workbook_impl::file_version_t();
339✔
422
    auto &file_version = wb.d_->file_version_.get();
339✔
423
    file_version.app_name = "xl";
339✔
424
    file_version.last_edited = "6";
339✔
425
    file_version.lowest_edited = "6";
339✔
426
    file_version.rup_build = "26709";
339✔
427

428
    xlnt::workbook_view wb_view;
339✔
429
    wb_view.active_tab = 0;
339✔
430
    wb_view.x_window = 0;
339✔
431
    wb_view.y_window = 460;
339✔
432
    wb_view.window_width = 28800;
339✔
433
    wb_view.window_height = 17460;
339✔
434
    wb_view.tab_ratio = 500;
339✔
435
    wb.view(wb_view);
339✔
436

437
    auto ws = wb.create_sheet();
339✔
438

439
    page_margins margins;
339✔
440
    margins.left(0.7);
339✔
441
    margins.right(0.7);
339✔
442
    margins.top(0.75);
339✔
443
    margins.bottom(0.75);
339✔
444
    margins.header(0.3);
339✔
445
    margins.footer(0.3);
339✔
446
    ws.page_margins(margins);
339✔
447

448
    sheet_view view;
339✔
449
    ws.add_view(view);
339✔
450

451
    auto &format_properties = ws.d_->format_properties_;
339✔
452
    format_properties.base_col_width = 10.0;
339✔
453
    format_properties.default_row_height = 16.0;
339✔
454

455
    wb.theme(xlnt::theme());
339✔
456

457
    wb.d_->stylesheet_ = detail::stylesheet();
339✔
458
    auto &stylesheet = wb.d_->stylesheet_.get();
339✔
459
    stylesheet.parent = wb.d_;
339✔
460

461
    auto default_border = border()
339✔
462
                              .side(border_side::bottom, border::border_property())
678✔
463
                              .side(border_side::top, border::border_property())
678✔
464
                              .side(border_side::start, border::border_property())
678✔
465
                              .side(border_side::end, border::border_property())
678✔
466
                              .side(border_side::diagonal, border::border_property());
339✔
467
    wb.d_->stylesheet_.get().borders.push_back(default_border);
339✔
468

469
    auto default_fill = fill(pattern_fill()
678✔
470
                                 .type(pattern_fill_type::none));
339✔
471
    stylesheet.fills.push_back(default_fill);
339✔
472
    auto gray125_fill = pattern_fill()
678✔
473
                            .type(pattern_fill_type::gray125);
339✔
474
    stylesheet.fills.push_back(gray125_fill);
339✔
475

476
    auto default_font = font()
339✔
477
                            .name("Calibri")
678✔
478
                            .size(12)
339✔
479
                            .scheme("minor")
678✔
480
                            .family(2)
339✔
481
                            .color(theme_color(1));
339✔
482
    stylesheet.fonts.push_back(default_font);
339✔
483

484
    wb.create_builtin_style(0)
339✔
485
        .border(default_border)
339✔
486
        .fill(default_fill)
678✔
487
        .font(default_font)
678✔
488
        .number_format(xlnt::number_format::general());
339✔
489

490
    auto format = wb.create_format()
339✔
491
        .border(default_border)
678✔
492
        .fill(default_fill)
678✔
493
        .font(default_font)
678✔
494
        .number_format(xlnt::number_format::general())
678✔
495
        .style("Normal");
678✔
496
    wb.default_format(format);
339✔
497

498
    xlnt::calculation_properties calc_props;
339✔
499
    calc_props.calc_id = 150000;
339✔
500
    wb.calculation_properties(calc_props);
339✔
501

502
    return wb;
339✔
503
}
678!
504

505
workbook::workbook()
325✔
506
{
507
    auto wb_template = empty();
325✔
508
    swap(wb_template);
325✔
509
}
325✔
510

511
workbook::workbook(const xlnt::path &file)
5✔
512
{
513
    *this = empty();
5✔
514
    load(file);
5✔
515
}
5✔
516

517
template <typename T>
518
void workbook::construct(const xlnt::path &file, const T &password)
4✔
519
{
520
    *this = empty();
4✔
521
    load(file, password);
4✔
522
}
2✔
523

524
workbook::workbook(const xlnt::path &file, const std::string &password)
1✔
525
{
526
    construct(file, password);
1!
527
}
1✔
528

529
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
530
workbook::workbook(const xlnt::path &file, std::u8string_view password)
3✔
531
{
532
    construct(file, password);
3✔
533
}
3✔
534
#endif
535

536
workbook::workbook(std::istream &data)
3✔
537
{
538
    *this = empty();
3✔
539
    load(data);
3✔
540
}
3✔
541

542
template <typename T>
543
void workbook::construct(std::istream &data, const T &password)
2✔
544
{
545
    *this = empty();
2✔
546
    load(data, password);
2✔
547
}
1✔
548

549
workbook::workbook(std::istream &data, const std::string &password)
×
550
{
551
    construct(data, password);
×
552
}
×
553

554
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
555
workbook::workbook(std::istream &data, std::u8string_view password)
2✔
556
{
557
    construct(data, password);
2✔
558
}
2✔
559
#endif
560

561
workbook::workbook(std::shared_ptr<detail::workbook_impl> impl)
340✔
562
{
563
    set_impl(std::move(impl));
340✔
564
}
340✔
565

566
workbook::workbook(std::weak_ptr<detail::workbook_impl> impl)
1,057✔
567
{
568
    set_impl(impl.lock());
1,057✔
569
}
1,057✔
570

571
void workbook::set_impl(std::shared_ptr<detail::workbook_impl> impl)
1,397✔
572
{
573
    if (impl == nullptr)
1,397!
574
    {
575
        throw xlnt::invalid_parameter("invalid workbook pointer");
×
576
    }
577

578
    d_ = std::move(impl);
1,397✔
579
}
1,397✔
580

581
void workbook::register_package_part(relationship_type type)
7,094✔
582
{
583
    if (!manifest().has_relationship(path("/"), type))
21,282✔
584
    {
585
        manifest().register_override_type(default_path(type), content_type(type));
1,018✔
586
        manifest().register_relationship(uri("/"), type,
2,036✔
587
            uri(default_path(type).relative_to(path("/")).string()),
4,072✔
588
            target_mode::internal);
589
    }
590
}
7,094✔
591

592
void workbook::register_workbook_part(relationship_type type)
1,913✔
593
{
594
    auto wb_rel = manifest().relationship(path("/"), relationship_type::office_document);
3,826✔
595
    auto wb_path = manifest().canonicalize({wb_rel});
7,652!
596

597
    if (!manifest().has_relationship(wb_path, type))
1,913✔
598
    {
599
        manifest().register_override_type(default_path(type), content_type(type));
742✔
600
        manifest().register_relationship(uri(wb_path.string()), type,
1,484✔
601
            uri(default_path(type).relative_to(wb_path.resolve(path("/"))).string()),
2,968✔
602
            target_mode::internal);
603
    }
604
}
3,826!
605

606
void workbook::register_worksheet_part(worksheet ws, relationship_type type)
34✔
607
{
608
    auto wb_rel = manifest().relationship(path("/"),
102✔
609
        relationship_type::office_document);
34✔
610
    auto ws_rel = manifest().relationship(wb_rel.target().path(),
34✔
611
        d_->sheet_title_rel_id_map_.at(ws.title()));
34✔
612
    path ws_path(ws_rel.source().path().parent().append(ws_rel.target().path()));
34✔
613

614
    if (type == relationship_type::comments)
34!
615
    {
616
        if (!manifest().has_relationship(ws_path, relationship_type::vml_drawing))
34✔
617
        {
618
            std::size_t file_number = 1;
3✔
619
            path filename("vmlDrawing1.vml");
3✔
620
            bool filename_exists = true;
3✔
621

622
            while (filename_exists)
7✔
623
            {
624
                filename_exists = false;
4✔
625

626
                for (auto current_ws_rel :
4✔
627
                    manifest().relationships(wb_rel.target().path(), xlnt::relationship_type::worksheet))
14✔
628
                {
629
                    path current_ws_path(current_ws_rel.source().path().parent().append(current_ws_rel.target().path()));
6✔
630
                    if (!manifest().has_relationship(current_ws_path, xlnt::relationship_type::vml_drawing)) continue;
6✔
631

632
                    for (auto current_ws_child_rel :
2✔
633
                        manifest().relationships(current_ws_path, xlnt::relationship_type::vml_drawing))
5✔
634
                    {
635
                        if (current_ws_child_rel.target().path() == path("../drawings").append(filename))
4✔
636
                        {
637
                            filename_exists = true;
1✔
638
                            break;
1✔
639
                        }
640
                    }
3✔
641
                }
14✔
642

643
                if (filename_exists)
4✔
644
                {
645
                    file_number++;
1✔
646
                    filename = path("vmlDrawing" + std::to_string(file_number) + ".vml");
1✔
647
                }
648
            }
649

650
            manifest().register_default_type("vml", "application/vnd.openxmlformats-officedocument.vmlDrawing");
15✔
651

652
            const path relative_path(path("../drawings").append(filename));
3✔
653
            manifest().register_relationship(
6✔
654
                uri(ws_path.string()), relationship_type::vml_drawing, uri(relative_path.string()), target_mode::internal);
6✔
655
        }
3✔
656

657
        if (!manifest().has_relationship(ws_path, relationship_type::comments))
34✔
658
        {
659
            std::size_t file_number = 1;
3✔
660
            path filename("comments1.xml");
6✔
661

662
            while (true)
663
            {
664
                if (!manifest().has_override_type(constants::package_xl().append(filename))) break;
4✔
665

666
                file_number++;
1✔
667
                filename = path("comments" + std::to_string(file_number) + ".xml");
1✔
668
            }
669

670
            const path absolute_path(constants::package_xl().append(filename));
3✔
671
            manifest().register_override_type(
9✔
672
                absolute_path, "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml");
673

674
            const path relative_path(path("..").append(filename));
3✔
675
            manifest().register_relationship(
6✔
676
                uri(ws_path.string()), relationship_type::comments, uri(relative_path.string()), target_mode::internal);
6✔
677
        }
3✔
678
    }
679
}
34✔
680

681
const worksheet workbook::sheet_by_title(const std::string &title) const
56✔
682
{
683
    for (auto &impl : d_->worksheets_)
70✔
684
    {
685
        if (impl.title_ == title)
69✔
686
        {
687
            return worksheet(&impl);
55✔
688
        }
689
    }
690

691
    throw key_not_found(title);
1✔
692
}
693

694
worksheet workbook::sheet_by_title(const std::string &title)
26✔
695
{
696
    for (auto &impl : d_->worksheets_)
49✔
697
    {
698
        if (impl.title_ == title)
44✔
699
        {
700
            return worksheet(&impl);
21✔
701
        }
702
    }
703

704
    throw key_not_found(title);
5✔
705
}
706

707
worksheet workbook::sheet_by_index(std::size_t index)
368✔
708
{
709
    if (index >= d_->worksheets_.size())
368✔
710
    {
711
        throw invalid_parameter("invalid worksheet index " + std::to_string(index) + " (workbook contains " + std::to_string(d_->worksheets_.size()) + " worksheets)");
3✔
712
    }
713

714
    auto iter = d_->worksheets_.begin();
365✔
715

716
    for (std::size_t i = 0; i < index; ++i)
473✔
717
    {
718
        ++iter;
108✔
719
    }
720

721
    return worksheet(&*iter);
730✔
722
}
723

724
const worksheet workbook::sheet_by_index(std::size_t index) const
1,413✔
725
{
726
    if (index >= d_->worksheets_.size())
1,413✔
727
    {
728
        throw invalid_parameter("invalid worksheet index " + std::to_string(index) + " (workbook contains " + std::to_string(d_->worksheets_.size()) + " worksheets)");
3✔
729
    }
730

731
    auto iter = d_->worksheets_.begin();
1,410✔
732

733
    for (std::size_t i = 0; i < index; ++i, ++iter)
1,834✔
734
    {
735
    }
736

737
    return worksheet(&*iter);
2,820✔
738
}
739

740
bool workbook::has_sheet_id(std::size_t id) const
4✔
741
{
742
    for (auto &impl : d_->worksheets_)
10✔
743
    {
744
        if (impl.id_ == id)
8✔
745
        {
746
            return true;
2✔
747
        }
748
    }
749

750
    return false;
2✔
751
}
752

753
worksheet workbook::sheet_by_id(std::size_t id)
2✔
754
{
755
    for (auto &impl : d_->worksheets_)
5✔
756
    {
757
        if (impl.id_ == id)
4✔
758
        {
759
            return worksheet(&impl);
1✔
760
        }
761
    }
762

763
    throw key_not_found(std::to_string(id));
1✔
764
}
765

766
const worksheet workbook::sheet_by_id(std::size_t id) const
2✔
767
{
768
    for (auto &impl : d_->worksheets_)
5✔
769
    {
770
        if (impl.id_ == id)
4✔
771
        {
772
            return worksheet(&impl);
1✔
773
        }
774
    }
775

776
    throw key_not_found(std::to_string(id));
1✔
777
}
778

779
bool workbook::sheet_hidden_by_index(std::size_t index) const
2✔
780
{
781
    if (index >= d_->sheet_hidden_.size())
2✔
782
    {
783
        throw invalid_parameter("invalid hidden worksheet index " + std::to_string(index) + " (workbook contains " + std::to_string(d_->sheet_hidden_.size()) + " hidden worksheets)");
1✔
784
    }
785

786
    return d_->sheet_hidden_.at(index);
1✔
787
}
788

789
worksheet workbook::active_sheet()
197✔
790
{
791
    return sheet_by_index(d_->active_sheet_index_.is_set() ? d_->active_sheet_index_.get() : 0);
197✔
792
}
793
void workbook::active_sheet(std::size_t index)
1✔
794
{
795
    d_->active_sheet_index_.set(index);
1✔
796
    d_->view_.get().active_tab = index;
1✔
797
}
1✔
798

799
bool workbook::has_named_range(const std::string &name) const
13✔
800
{
801
    for (auto worksheet : *this)
41✔
802
    {
803
        if (worksheet.has_named_range(name))
20✔
804
        {
805
            return true;
6✔
806
        }
807
    }
808
    return false;
7✔
809
}
810

811
worksheet workbook::create_sheet()
386✔
812
{
813
    std::string title = "Sheet1";
386✔
814
    int index = 1;
386✔
815

816
    // make a unique sheet name. Sheet<1...n>
817
    while (contains(title))
451✔
818
    {
819
        title = "Sheet" + std::to_string(++index);
65✔
820
    }
821
    // unique sheet id
822
    size_t sheet_id = 1;
386✔
823
    for (const auto ws : *this)
455✔
824
    {
825
        sheet_id = std::max(sheet_id, ws.id() + 1);
69✔
826
    }
827
    d_->worksheets_.push_back(detail::worksheet_impl(this, sheet_id, title));
386✔
828
    // unique sheet file name
829
    auto workbook_rel = d_->manifest_.relationship(path("/"), relationship_type::office_document);
772✔
830
    auto workbook_files = d_->manifest_.relationships(workbook_rel.target().path());
386✔
831
    auto rel_vec_contains = [&workbook_files](const xlnt::path &new_file_id) {
387✔
832
        return workbook_files.end() != std::find_if(workbook_files.begin(), workbook_files.end(), [&new_file_id](const xlnt::relationship &rel) {
774✔
833
            return rel.target().path() == new_file_id;
167✔
834
        });
774✔
835
    };
386✔
836

837
    size_t file_id = sheet_id;
386✔
838
    xlnt::path sheet_relative_path;
386✔
839
    do
840
    {
841
        sheet_relative_path = path("worksheets").append("sheet" + std::to_string(file_id++) + ".xml");
387✔
842
    } while (rel_vec_contains(sheet_relative_path));
387✔
843

844
    uri relative_sheet_uri(sheet_relative_path.string());
386✔
845
    auto absolute_sheet_path = path("/xl").append(relative_sheet_uri.path());
386✔
846
    d_->manifest_.register_override_type(
772✔
847
        absolute_sheet_path, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
848
    auto ws_rel = d_->manifest_.register_relationship(
386✔
849
        workbook_rel.target(), relationship_type::worksheet, relative_sheet_uri, target_mode::internal);
386✔
850
    d_->sheet_title_rel_id_map_[title] = ws_rel;
386✔
851

852
    update_sheet_properties();
386✔
853
    reorder_relationships();
386✔
854

855
    return worksheet(&d_->worksheets_.back());
772✔
856
}
386✔
857

858
worksheet workbook::copy_sheet(worksheet to_copy)
3✔
859
{
860
    if (to_copy.d_->parent_.lock() != d_) throw invalid_parameter("the worksheets do not have the same parent workbook");
5✔
861

862
    detail::worksheet_impl impl(*to_copy.d_);
2✔
863
    auto new_sheet = create_sheet();
2✔
864
    impl.title_ = new_sheet.title();
2✔
865
    impl.id_ = new_sheet.id();
2✔
866
    *new_sheet.d_ = impl;
2✔
867

868
    return new_sheet;
4✔
869
}
2✔
870

871
worksheet workbook::copy_sheet(worksheet to_copy, std::size_t index)
1✔
872
{
873
    copy_sheet(to_copy);
1✔
874

875
    if (index != d_->worksheets_.size() - 1)
1!
876
    {
877
        auto iter = d_->worksheets_.begin();
1✔
878

879
        for (std::size_t i = 0; i < index; ++i, ++iter)
1!
880
        {
881
        }
882

883
        d_->worksheets_.insert(iter, d_->worksheets_.back());
1✔
884
        d_->worksheets_.pop_back();
1✔
885
    }
886

887
    return sheet_by_index(index);
1✔
888
}
889

890
std::size_t workbook::index(worksheet ws) const
6✔
891
{
892
    auto match = std::find(begin(), end(), ws);
6✔
893

894
    if (match == end())
6✔
895
    {
896
        throw invalid_parameter("worksheet not found in workbook");
3✔
897
    }
898

899
    return static_cast<std::size_t>(std::distance(begin(), match));
15✔
900
}
901

902
void workbook::move_sheet(worksheet worksheet, std::size_t newIndex)
5✔
903
{
904
    if(newIndex >= sheet_count())
5✔
905
        throw invalid_parameter("invalid new worksheet index " + std::to_string(newIndex) + " (workbook contains " + std::to_string(sheet_count()) + " worksheets)");
1✔
906

907
    auto sourcePosition = d_->worksheets_.end();
4✔
908
    auto targetPosition = d_->worksheets_.begin();
4✔
909
    size_t currentIndex = 0;
4✔
910
    size_t sourceIndex  = sheet_count();
4✔
911

912
    for(auto iter = d_->worksheets_.begin(); iter != d_->worksheets_.end(); ++iter)
28✔
913
    {
914
        if(worksheet.d_ == (&*iter))
24✔
915
        {
916
            sourcePosition = iter;
3✔
917
            sourceIndex    = currentIndex;
3✔
918
        }
919

920
        if(currentIndex == newIndex)
24✔
921
            targetPosition = iter;
4✔
922

923
        ++currentIndex;
24✔
924
    }
925

926
    if(sourcePosition == d_->worksheets_.end())
4✔
927
        throw invalid_parameter("cannot move worksheet: it does not exist in this workbook");
3✔
928

929
    if(sourceIndex < newIndex)
3✔
930
        ++targetPosition;
2✔
931

932
    d_->worksheets_.splice(targetPosition, d_->worksheets_, sourcePosition);
3✔
933
}
3✔
934

935
void workbook::create_named_range(const std::string &name, worksheet range_owner, const std::string &reference_string)
8✔
936
{
937
    create_named_range(name, range_owner, range_reference(reference_string));
8✔
938
}
7✔
939

940
void workbook::create_named_range(const std::string &name, worksheet range_owner, const range_reference &reference)
8✔
941
{
942
    sheet_by_title(range_owner.title()).create_named_range(name, reference);
9✔
943
}
7✔
944

945
void workbook::remove_named_range(const std::string &name)
3✔
946
{
947
    for (auto ws : *this)
13✔
948
    {
949
        if (ws.has_named_range(name))
6✔
950
        {
951
            ws.remove_named_range(name);
1✔
952
            return;
1✔
953
        }
954
    }
955

956
    throw key_not_found(name);
2✔
957
}
958

959
range workbook::named_range(const std::string &name)
2✔
960
{
961
    for (auto ws : *this)
8✔
962
    {
963
        if (ws.has_named_range(name))
4✔
964
        {
965
            return ws.named_range(name);
1✔
966
        }
967
    }
968

969
    throw key_not_found(name);
1✔
970
}
971

972
void workbook::load(std::istream &stream)
88✔
973
{
974
    clear();
88✔
975
    detail::xlsx_consumer consumer(*this);
88✔
976

977
    try
978
    {
979
        consumer.read(stream);
88✔
980
    }
981
    catch (const xlnt::invalid_password &e)
9✔
982
    {
983
        stream.seekg(0, std::ios::beg);
6✔
984
        consumer.read(stream, "VelvetSweatshop");
12!
985
    }
6!
986
}
88✔
987

988
void workbook::load(const std::vector<std::uint8_t> &data)
28✔
989
{
990
    if (data.size() < 22) // the shortest ZIP file is 22 bytes
28✔
991
    {
992
        throw xlnt::invalid_file("file is empty or malformed (data size " + std::to_string(data.size()) + " bytes)");
4✔
993
    }
994

995
    xlnt::detail::vector_istreambuf data_buffer(data);
24✔
996
    std::istream data_stream(&data_buffer);
24✔
997
    load(data_stream);
24✔
998
}
25✔
999

1000
template <typename T>
1001
void workbook::load_internal(const T &filename)
16✔
1002
{
1003
    return load(path(filename));
16✔
1004
}
1005

1006
template <typename T>
1007
void workbook::load_internal(const T &filename, const T &password)
3✔
1008
{
1009
    return load(path(filename), password);
3✔
1010
}
1011

1012
void workbook::load(const std::string &filename)
12✔
1013
{
1014
    return load_internal(filename);
12✔
1015
}
1016

1017
void workbook::load(const path &filename)
63✔
1018
{
1019
    std::ifstream file_stream;
63✔
1020
    open_stream(file_stream, filename.string());
63✔
1021

1022
    if (!file_stream.good())
63✔
1023
    {
1024
        throw xlnt::invalid_file(filename.string());
5✔
1025
    }
1026

1027
    load(file_stream);
58✔
1028
}
63✔
1029

1030
void workbook::load(const std::string &filename, const std::string &password)
×
1031
{
1032
    return load_internal(filename, password);
×
1033
}
1034

1035
template <typename T>
1036
void workbook::load_internal(const xlnt::path &filename, const T &password)
18✔
1037
{
1038
    std::ifstream file_stream;
18✔
1039
    open_stream(file_stream, filename.string());
18✔
1040

1041
    if (!file_stream.good())
18✔
1042
    {
1043
        throw xlnt::invalid_file(filename.string());
2✔
1044
    }
1045

1046
    return load(file_stream, password);
24✔
1047
}
18✔
1048

1049
void workbook::load(const path &filename, const std::string &password)
8✔
1050
{
1051
    load_internal(filename, password);
8✔
1052
}
3✔
1053

1054
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1055
void workbook::load(const xlnt::path &filename, std::u8string_view password)
10✔
1056
{
1057
    load_internal(filename, password);
10✔
1058
}
5✔
1059
#endif
1060

1061
template <typename T>
1062
void workbook::load_internal(const std::vector<std::uint8_t> &data, const T &password)
10✔
1063
{
1064
    if (data.size() < 22) // the shortest ZIP file is 22 bytes
10✔
1065
    {
1066
        throw xlnt::invalid_file("file is empty or malformed (data size " + std::to_string(data.size()) + " bytes)");
4✔
1067
    }
1068

1069
    xlnt::detail::vector_istreambuf data_buffer(data);
6✔
1070
    std::istream data_stream(&data_buffer);
6✔
1071
    load(data_stream, password);
6✔
1072
}
7✔
1073

1074
void workbook::load(const std::vector<std::uint8_t> &data, const std::string &password)
3✔
1075
{
1076
    load_internal(data, password);
3✔
1077
}
3✔
1078

1079
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1080
void workbook::load(const std::vector<std::uint8_t> &data, std::u8string_view password)
7✔
1081
{
1082
    load_internal(data, password);
7✔
1083
}
2✔
1084
#endif
1085

1086
template <typename T>
1087
void workbook::load_internal(std::istream &stream, const T &password)
27✔
1088
{
1089
    clear();
27✔
1090
    detail::xlsx_consumer consumer(*this);
27✔
1091
    consumer.read(stream, password);
27✔
1092
}
27✔
1093

1094
void workbook::load(std::istream &stream, const std::string &password)
11✔
1095
{
1096
    load_internal(stream, password);
11✔
1097
}
6✔
1098

1099
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1100
void workbook::load(std::istream &stream, std::u8string_view password)
16✔
1101
{
1102
    load_internal(stream, password);
16✔
1103
}
9✔
1104
#endif
1105

1106
void workbook::save(std::vector<std::uint8_t> &data) const
13✔
1107
{
1108
    xlnt::detail::vector_ostreambuf data_buffer(data);
13✔
1109
    std::ostream data_stream(&data_buffer);
13✔
1110
    save(data_stream);
13✔
1111
}
13✔
1112

1113
template <typename T>
1114
void workbook::save_internal(std::vector<std::uint8_t> &data, const T &password) const
×
1115
{
1116
    xlnt::detail::vector_ostreambuf data_buffer(data);
×
1117
    std::ostream data_stream(&data_buffer);
×
1118
    save(data_stream, password);
×
1119
}
×
1120

1121
void workbook::save(std::vector<std::uint8_t> &data, const std::string &password) const
×
1122
{
1123
    save_internal(data, password);
×
1124
}
×
1125

1126
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1127
void workbook::save(std::vector<std::uint8_t> &data, std::u8string_view password) const
×
1128
{
1129
    save_internal(data, password);
×
1130
}
×
1131
#endif
1132

1133
template <typename T>
1134
void workbook::save_internal(const T &filename) const
27✔
1135
{
1136
    save(path(filename));
27✔
1137
}
27✔
1138

1139
template <typename T>
1140
void workbook::save_internal(const T &filename, const T &password) const
×
1141
{
1142
    save(path(filename), password);
×
1143
}
×
1144

1145
void workbook::save(const std::string &filename) const
26✔
1146
{
1147
    save_internal(filename);
26✔
1148
}
26✔
1149

1150
void workbook::save(const std::string &filename, const std::string &password) const
×
1151
{
1152
    save_internal(filename, password);
×
1153
}
×
1154

1155
void workbook::save(const path &filename) const
27✔
1156
{
1157
    std::ofstream file_stream;
27✔
1158
    open_stream(file_stream, filename.string());
27✔
1159
    save(file_stream);
27✔
1160
}
27✔
1161

1162
template <typename T>
1163
void workbook::save_internal(const xlnt::path &filename, const T &password) const
4✔
1164
{
1165
    std::ofstream file_stream;
4✔
1166
    open_stream(file_stream, filename.string());
4✔
1167
    save(file_stream, password);
4✔
1168
}
4✔
1169

1170
void workbook::save(const path &filename, const std::string &password) const
3✔
1171
{
1172
    save_internal(filename, password);
3✔
1173
}
3✔
1174

1175
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1176
void workbook::save(const xlnt::path &filename, std::u8string_view password) const
1✔
1177
{
1178
    save_internal(filename, password);
1✔
1179
}
1✔
1180
#endif
1181

1182
void workbook::save(std::ostream &stream) const
40✔
1183
{
1184
    detail::xlsx_producer producer(*this);
40✔
1185
    producer.write(stream);
40✔
1186
}
40✔
1187

1188
template <typename T>
1189
void workbook::save_internal(std::ostream &stream, const T &password) const
4✔
1190
{
1191
    detail::xlsx_producer producer(*this);
4✔
1192
    producer.write(stream, password);
4✔
1193
}
4✔
1194

1195
void workbook::save(std::ostream &stream, const std::string &password) const
3✔
1196
{
1197
    save_internal(stream, password);
3✔
1198
}
3✔
1199

1200
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1201
void workbook::save(std::ostream &stream, std::u8string_view password) const
1✔
1202
{
1203
    save_internal(stream, password);
1✔
1204
}
1✔
1205
#endif
1206

1207
#ifdef _MSC_VER
1208
void workbook::save(const std::wstring &filename) const
1209
{
1210
    std::ofstream file_stream;
1211
    open_stream(file_stream, filename);
1212
    save(file_stream);
1213
}
1214

1215
void workbook::save(const std::wstring &filename, const std::string &password) const
1216
{
1217
    std::ofstream file_stream;
1218
    open_stream(file_stream, filename);
1219
    save(file_stream, password);
1220
}
1221

1222
void workbook::load(const std::wstring &filename)
1223
{
1224
    std::ifstream file_stream;
1225
    open_stream(file_stream, filename);
1226
    load(file_stream);
1227
}
1228

1229
void workbook::load(const std::wstring &filename, const std::string &password)
1230
{
1231
    std::ifstream file_stream;
1232
    open_stream(file_stream, filename);
1233
    load(file_stream, password);
1234
}
1235
#endif
1236

1237
#if XLNT_HAS_FEATURE(U8_STRING_VIEW)
1238
void workbook::save(std::u8string_view filename) const
1✔
1239
{
1240
    save_internal(filename);
1✔
1241
}
1✔
1242

1243
void workbook::save(std::u8string_view filename, std::u8string_view password) const
×
1244
{
1245
    save_internal(filename, password);
×
1246
}
×
1247

1248
void workbook::load(std::u8string_view filename)
4✔
1249
{
1250
    load_internal(filename);
4✔
1251
}
1✔
1252

1253
void workbook::load(std::u8string_view filename, std::u8string_view password)
3✔
1254
{
1255
    load_internal(filename, password);
3✔
1256
}
1✔
1257
#endif
1258

1259
void workbook::remove_sheet(worksheet ws)
10✔
1260
{
1261
    auto match_iter = std::find_if(d_->worksheets_.begin(), d_->worksheets_.end(),
10✔
1262
        [=](detail::worksheet_impl &comp) { return &comp == ws.d_; });
27✔
1263

1264
    if (match_iter == d_->worksheets_.end())
10✔
1265
    {
1266
        throw invalid_parameter("cannot remove worksheet: it does not exist in this workbook");
3✔
1267
    }
1268

1269
    auto ws_rel_id = d_->sheet_title_rel_id_map_.at(ws.title());
9✔
1270
    auto wb_rel = d_->manifest_.relationship(path("/"), xlnt::relationship_type::office_document);
18✔
1271
    auto ws_rel = d_->manifest_.relationship(wb_rel.target().path(), ws_rel_id);
9✔
1272
    auto ws_part = d_->manifest_.canonicalize({wb_rel, ws_rel}).resolve(path("/"));
63!
1273
    d_->manifest_.unregister_override_type(ws_part);
9✔
1274
    auto rel_id_map = d_->manifest_.unregister_relationship(wb_rel.target(), ws_rel_id);
9✔
1275
    d_->sheet_title_rel_id_map_.erase(ws.title());
9✔
1276
    d_->worksheets_.erase(match_iter);
9✔
1277

1278
    // Shift sheet title->ID mappings down as a result of manifest::unregister_relationship above.
1279
    for (auto &title_rel_id_pair : d_->sheet_title_rel_id_map_)
34✔
1280
    {
1281
        auto rel_id = rel_id_map.find(title_rel_id_pair.second);
25✔
1282

1283
        if (rel_id != rel_id_map.end())
25✔
1284
        {
1285
            title_rel_id_pair.second = rel_id->second;
19✔
1286
        }
1287
    }
1288

1289
    update_sheet_properties();
9✔
1290
}
18!
1291

1292
worksheet workbook::create_sheet(std::size_t index)
2✔
1293
{
1294
    create_sheet();
2✔
1295

1296
    if (index != d_->worksheets_.size() - 1)
2✔
1297
    {
1298
        auto iter = d_->worksheets_.begin();
1✔
1299

1300
        for (std::size_t i = 0; i < index; ++i, ++iter)
1!
1301
        {
1302
        }
1303

1304
        d_->worksheets_.insert(iter, d_->worksheets_.back());
1✔
1305
        d_->worksheets_.pop_back();
1✔
1306
    }
1307

1308
    return sheet_by_index(index);
2✔
1309
}
1310

1311
worksheet workbook::create_sheet_with_rel(const std::string &title, const relationship &rel)
×
1312
{
1313
    auto sheet_id = d_->worksheets_.size() + 1;
×
1314
    d_->worksheets_.push_back(detail::worksheet_impl(this, sheet_id, title));
×
1315

1316
    auto workbook_rel = d_->manifest_.relationship(path("/"), relationship_type::office_document);
×
1317
    auto sheet_absoulute_path = workbook_rel.target().path().parent().append(rel.target().path());
×
1318
    d_->manifest_.register_override_type(sheet_absoulute_path,
×
1319
        "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
1320
    auto ws_rel = d_->manifest_.register_relationship(workbook_rel.target(),
×
1321
        relationship_type::worksheet, rel.target(), target_mode::internal);
×
1322
    d_->sheet_title_rel_id_map_[title] = ws_rel;
×
1323

1324
    update_sheet_properties();
×
1325

1326
    return worksheet(&d_->worksheets_.back());
×
1327
}
×
1328

1329
workbook::iterator workbook::begin()
423✔
1330
{
1331
    return iterator(*this, 0);
423✔
1332
}
1333

1334
workbook::iterator workbook::end()
417✔
1335
{
1336
    return iterator(*this, d_->worksheets_.size());
417✔
1337
}
1338

1339
workbook::const_iterator workbook::begin() const
1,462✔
1340
{
1341
    return cbegin();
1,462✔
1342
}
1343

1344
workbook::const_iterator workbook::end() const
1,464✔
1345
{
1346
    return cend();
1,464✔
1347
}
1348

1349
workbook::const_iterator workbook::cbegin() const
1,472✔
1350
{
1351
    return const_iterator(*this, 0);
1,472✔
1352
}
1353

1354
workbook::const_iterator workbook::cend() const
1,466✔
1355
{
1356
    return const_iterator(*this, d_->worksheets_.size());
1,466✔
1357
}
1358

1359
std::vector<std::string> workbook::sheet_titles() const
812✔
1360
{
1361
    std::vector<std::string> names;
812✔
1362

1363
    for (auto ws : *this)
1,797✔
1364
    {
1365
        names.push_back(ws.title());
985✔
1366
    }
1367

1368
    return names;
812✔
1369
}
×
1370

1371
std::size_t workbook::sheet_count() const
439✔
1372
{
1373
    return d_->worksheets_.size();
439✔
1374
}
1375

1376
worksheet workbook::operator[](const std::string &name)
2✔
1377
{
1378
    return sheet_by_title(name);
2✔
1379
}
1380

1381
worksheet workbook::operator[](std::size_t index)
154✔
1382
{
1383
    return sheet_by_index(index);
154✔
1384
}
1385

1386
void workbook::clear()
214✔
1387
{
1388
    *d_ = detail::workbook_impl();
214✔
1389
    d_->stylesheet_.clear();
214✔
1390
}
214✔
1391

1392
bool workbook::compare(const workbook &other, bool compare_by_reference) const
28✔
1393
{
1394
    if (compare_by_reference)
28✔
1395
    {
1396
        return d_ == other.d_;
16✔
1397
    }
1398
    else
1399
    {
1400
        return *d_ == *other.d_;
12✔
1401
    }
1402
}
1403

1404
bool workbook::operator==(const workbook &rhs) const
14✔
1405
{
1406
    return compare(rhs, true);
14✔
1407
}
1408

1409
bool workbook::operator!=(const workbook &rhs) const
11✔
1410
{
1411
    return !operator==(rhs);
11✔
1412
}
1413

1414
void workbook::swap(workbook &right)
325✔
1415
{
1416
    std::swap(d_, right.d_);
325✔
1417
}
325✔
1418

1419
workbook workbook::clone(clone_method method) const
3✔
1420
{
1421
    switch (method)
3✔
1422
    {
1423
    case clone_method::deep_copy:
1✔
1424
    {
1425
        workbook wb;
1!
1426
        *wb.d_ = *d_;
1✔
1427

1428
        for (auto ws : wb)
5✔
1429
        {
1430
            ws.parent(wb);
2✔
1431
        }
1432

1433
        wb.d_->stylesheet_.get().parent = wb.d_;
1✔
1434

1435
        return wb;
1!
1436
    }
×
1437
    case clone_method::shallow_copy:
1✔
1438
    {
1439
        return workbook(d_);
1✔
1440
    }
1441
    default:
1✔
1442
        throw xlnt::invalid_parameter("clone method " + std::to_string(static_cast<int>(method)) + " not supported");
1✔
1443
    }
1444
}
1445

1446
bool workbook::has_theme() const
1✔
1447
{
1448
    return d_->theme_.is_set();
1✔
1449
}
1450

1451
const theme &workbook::theme() const
1✔
1452
{
1453
    if (!d_->theme_.is_set())
1!
1454
    {
NEW
1455
        throw xlnt::invalid_attribute("the workbook has no theme");
×
1456
    }
1457
    return d_->theme_.get();
1✔
1458
}
1459

1460
void workbook::theme(const class theme &value)
414✔
1461
{
1462
    d_->theme_ = value;
414✔
1463
    register_workbook_part(relationship_type::theme);
414✔
1464
}
414✔
1465

1466
std::vector<named_range> workbook::named_ranges() const
45✔
1467
{
1468
    std::vector<xlnt::named_range> named_ranges;
45✔
1469

1470
    for (auto ws : *this)
153✔
1471
    {
1472
        for (auto &ws_named_range : ws.d_->named_ranges_)
54!
1473
        {
1474
            named_ranges.push_back(ws_named_range.second);
×
1475
        }
1476
    }
1477

1478
    return named_ranges;
45✔
1479
}
×
1480

1481
format workbook::create_format(bool default_format)
413✔
1482
{
1483
    register_workbook_part(relationship_type::stylesheet);
413✔
1484
    return d_->stylesheet_.get().create_format(default_format);
413✔
1485
}
1486

1487
format workbook::clone_format_from(const xlnt::format &source_format)
2✔
1488
{
1489
    // Start with a blank format from this workbook (not default format)
1490
    auto cloned_format = create_format();
2✔
1491

1492
    // Copy all format properties using public API
1493
    if (source_format.alignment_applied())
2✔
1494
    {
1495
        cloned_format.alignment(source_format.alignment(), true);
1✔
1496
    }
1497

1498
    if (source_format.border_applied())
2✔
1499
    {
1500
        cloned_format.border(source_format.border(), true);
1✔
1501
    }
1502

1503
    if (source_format.fill_applied())
2✔
1504
    {
1505
        cloned_format.fill(source_format.fill(), true);
1✔
1506
    }
1507

1508
    if (source_format.font_applied())
2!
1509
    {
1510
        cloned_format.font(source_format.font(), true);
2✔
1511
    }
1512

1513
    if (source_format.number_format_applied())
2✔
1514
    {
1515
        cloned_format.number_format(source_format.number_format(), true);
1✔
1516
    }
1517

1518
    if (source_format.protection_applied())
2✔
1519
    {
1520
        cloned_format.protection(source_format.protection(), true);
1✔
1521
    }
1522

1523
    // Copy other properties (no "applied" flag for these)
1524
    cloned_format.pivot_button(source_format.pivot_button());
2✔
1525
    cloned_format.quote_prefix(source_format.quote_prefix());
2✔
1526

1527
    if (source_format.has_style())
2✔
1528
    {
1529
        // Style assignment is automatically deduplicating:
1530
        // xlnt reuses existing style objects if identical, or creates new ones if needed.
1531
        // This prevents duplicate styles in the destination workbook.
1532
        // This function is only copying the name using the format pattern from "cloned_format".
1533
        cloned_format.style(source_format.style());
1✔
1534
    }
1535

1536
    return cloned_format;
2✔
1537
}
×
1538

1539
bool workbook::owns_format(const class format &fmt) const
115✔
1540
{
1541
    if (!d_->stylesheet_.is_set())
115!
1542
    {
1543
        return false;
×
1544
    }
1545

1546
    return fmt.d_->parent == &d_->stylesheet_.get();
115✔
1547
}
1548

1549
void workbook::default_format(const class format &format)
339✔
1550
{
1551
    d_->stylesheet_.get().default_format(format);
339✔
1552
}
339✔
1553

1554
bool workbook::has_style(const std::string &name) const
5✔
1555
{
1556
    return d_->stylesheet_.get().has_style(name);
5✔
1557
}
1558

1559
void workbook::clear_styles()
1✔
1560
{
1561
    apply_to_cells([](cell c) { c.clear_style(); });
2✔
1562
}
1✔
1563

1564
void workbook::default_slicer_style(const std::string &value)
2✔
1565
{
1566
    d_->stylesheet_.get().default_slicer_style = value;
2✔
1567
}
2✔
1568

1569
std::string workbook::default_slicer_style() const
×
1570
{
1571
    return d_->stylesheet_.get().default_slicer_style.get();
×
1572
}
1573

1574
void workbook::enable_known_fonts()
49✔
1575
{
1576
    d_->stylesheet_.get().known_fonts_enabled = true;
49✔
1577
}
49✔
1578

1579
void workbook::disable_known_fonts()
×
1580
{
1581
    d_->stylesheet_.get().known_fonts_enabled = false;
×
1582
}
×
1583

1584
bool workbook::known_fonts_enabled() const
×
1585
{
1586
    return d_->stylesheet_.get().known_fonts_enabled;
×
1587
}
1588

1589
void workbook::clear_formats()
1✔
1590
{
1591
    apply_to_cells([](cell c) { c.clear_format(); });
2✔
1592
}
1✔
1593

1594
std::size_t workbook::format_count() const
19✔
1595
{
1596
    return d_->stylesheet_.get().format_impls.size();
19✔
1597
}
1598

1599
void workbook::apply_to_cells(std::function<void(cell)> f)
2✔
1600
{
1601
    for (auto ws : *this)
6✔
1602
    {
1603
        for (auto row = ws.lowest_row(); row <= ws.highest_row(); ++row)
4✔
1604
        {
1605
            for (auto column = ws.lowest_column(); column <= ws.highest_column(); ++column)
6✔
1606
            {
1607
                if (ws.has_cell(cell_reference(column, row)))
2!
1608
                {
1609
                    f.operator()(ws.cell(cell_reference(column, row)));
2✔
1610
                }
1611
            }
1612
        }
1613
    }
1614
}
2✔
1615

1616
format workbook::format(std::size_t format_index)
883✔
1617
{
1618
    return d_->stylesheet_.get().format(format_index);
883✔
1619
}
1620

1621
const format workbook::format(std::size_t format_index) const
×
1622
{
1623
    return d_->stylesheet_.get().format(format_index);
×
1624
}
1625

1626
manifest &workbook::manifest()
20,226✔
1627
{
1628
    return d_->manifest_;
20,226✔
1629
}
1630

1631
const manifest &workbook::manifest() const
821✔
1632
{
1633
    return d_->manifest_;
821✔
1634
}
1635

1636
const rich_text &workbook::shared_strings(std::size_t index) const
210✔
1637
{
1638
    if (index < d_->shared_strings_values_.size())
210!
1639
    {
1640
        return d_->shared_strings_values_.at(index);
210✔
1641
    }
1642

1643
    static rich_text empty;
×
1644
    return empty;
×
1645
}
1646

1647
std::vector<rich_text> &workbook::shared_strings()
2✔
1648
{
1649
    return d_->shared_strings_values_;
2✔
1650
}
1651

1652
const std::vector<rich_text> &workbook::shared_strings() const
62✔
1653
{
1654
    return d_->shared_strings_values_;
62✔
1655
}
1656

1657
std::size_t workbook::add_shared_string(const rich_text &shared, bool allow_duplicates)
1,066✔
1658
{
1659
    register_workbook_part(relationship_type::shared_string_table);
1,066✔
1660

1661
    if (!allow_duplicates)
1,066✔
1662
    {
1663
        auto it = d_->shared_strings_ids_.find(shared);
314✔
1664

1665
        if (it != d_->shared_strings_ids_.end())
314✔
1666
        {
1667
            return it->second;
7✔
1668
        }
1669
    }
1670

1671
    auto sz = d_->shared_strings_ids_.size();
1,059✔
1672
    d_->shared_strings_ids_[shared] = sz;
1,059✔
1673
    d_->shared_strings_values_.push_back(shared);
1,059✔
1674

1675
    return sz;
1,059✔
1676
}
1677

1678
bool workbook::contains(const std::string &sheet_title) const
458✔
1679
{
1680
    for (auto ws : *this)
676✔
1681
    {
1682
        if (ws.title() == sheet_title) return true;
176✔
1683
    }
1684

1685
    return false;
391✔
1686
}
1687

1688
void workbook::thumbnail(const std::vector<std::uint8_t> &thumbnail,
339✔
1689
    const std::string &extension, const std::string &content_type)
1690
{
1691
    if (!d_->manifest_.has_relationship(path("/"), relationship_type::thumbnail))
1,017!
1692
    {
1693
        d_->manifest_.register_default_type(extension, content_type);
339✔
1694
        d_->manifest_.register_relationship(uri("/"), relationship_type::thumbnail,
678✔
1695
            uri("docProps/thumbnail.jpeg"), target_mode::internal);
1,017✔
1696
    }
1697

1698
    auto thumbnail_rel = d_->manifest_.relationship(path("/"), relationship_type::thumbnail);
678✔
1699
    d_->images_[thumbnail_rel.target().to_string()] = thumbnail;
339✔
1700
}
339✔
1701

1702
bool workbook::has_thumbnail() const
1✔
1703
{
1704
    auto thumbnail_rel = d_->manifest_.relationship(path("/"), relationship_type::thumbnail);
2✔
1705
    return d_->images_.count(thumbnail_rel.target().to_string()) > 0;
2✔
1706
}
1✔
1707

1708
const std::vector<std::uint8_t> &workbook::thumbnail() const
1✔
1709
{
1710
    auto thumbnail_rel = d_->manifest_.relationship(path("/"), relationship_type::thumbnail);
2✔
1711
    auto thumbnail = d_->images_.find(thumbnail_rel.target().to_string());
1✔
1712

1713
    if (thumbnail == d_->images_.end())
1!
1714
    {
NEW
1715
        throw xlnt::invalid_attribute("the workbook has no thumbnail");
×
1716
    }
1717

1718
    return thumbnail->second;
2✔
1719
}
1✔
1720

1721
const std::unordered_map<std::string, std::vector<std::uint8_t>> &workbook::binaries() const
×
1722
{
1723
    return d_->binaries_;
×
1724
}
1725

1726
style workbook::create_style(const std::string &name)
17✔
1727
{
1728
    return d_->stylesheet_.get().create_style(name);
17✔
1729
}
1730

1731
style workbook::create_builtin_style(const std::size_t builtin_id)
341✔
1732
{
1733
    return d_->stylesheet_.get().create_builtin_style(builtin_id);
341✔
1734
}
1735

1736
style workbook::style(const std::string &name)
6✔
1737
{
1738
    return d_->stylesheet_.get().style(name);
6✔
1739
}
1740

1741
const style workbook::style(const std::string &name) const
2✔
1742
{
1743
    return d_->stylesheet_.get().style(name);
2✔
1744
}
1745

1746
calendar workbook::base_date() const
58✔
1747
{
1748
    return d_->base_date_;
58✔
1749
}
1750

1751
void workbook::base_date(calendar base_date)
97✔
1752
{
1753
    d_->base_date_ = base_date;
97✔
1754
}
97✔
1755

1756
bool workbook::has_title() const
2✔
1757
{
1758
    return d_->title_.is_set();
2✔
1759
}
1760

1761
std::string workbook::title() const
2✔
1762
{
1763
    if (!d_->title_.is_set())
2✔
1764
    {
1765
        throw xlnt::invalid_attribute("the workbook has no title");
3✔
1766
    }
1767
    return d_->title_.get();
1✔
1768
}
1769

1770
void workbook::title(const std::string &title)
1✔
1771
{
1772
    d_->title_ = title;
1✔
1773
}
1✔
1774

1775
detail::workbook_impl &workbook::impl()
389✔
1776
{
1777
    return *d_;
389✔
1778
}
1779

1780
const detail::workbook_impl &workbook::impl() const
149✔
1781
{
1782
    return *d_;
149✔
1783
}
1784

1785
bool workbook::has_view() const
97✔
1786
{
1787
    return d_->view_.is_set();
97✔
1788
}
1789

1790
workbook_view workbook::view() const
93✔
1791
{
1792
    if (!d_->view_.is_set())
93!
1793
    {
NEW
1794
        throw invalid_attribute("the workbook has no view");
×
1795
    }
1796

1797
    return d_->view_.get();
93✔
1798
}
1799

1800
void workbook::view(const workbook_view &view)
425✔
1801
{
1802
    d_->view_ = view;
425✔
1803
}
425✔
1804

1805
bool workbook::has_code_name() const
49✔
1806
{
1807
    return d_->code_name_.is_set();
49✔
1808
}
1809

1810
std::string workbook::code_name() const
2✔
1811
{
1812
    if (!has_code_name())
2✔
1813
    {
1814
        throw invalid_attribute("the workbook has no code name");
3✔
1815
    }
1816

1817
    return d_->code_name_.get();
1✔
1818
}
1819

1820
void workbook::code_name(const std::string &code_name)
1✔
1821
{
1822
    d_->code_name_ = code_name;
1✔
1823
}
1✔
1824

1825
bool workbook::has_file_version() const
60✔
1826
{
1827
    return d_->file_version_.is_set();
60✔
1828
}
1829

1830
void workbook::clear_file_version()
8✔
1831
{
1832
    d_->file_version_.clear();
8✔
1833
}
8✔
1834

1835
bool workbook::has_app_name() const
84✔
1836
{
1837
    return d_->file_version_.is_set() && !d_->file_version_.get().app_name.empty();
84!
1838
}
1839

1840
const std::string &workbook::app_name() const
43✔
1841
{
1842
    if (!d_->file_version_.is_set())
43✔
1843
    {
1844
        return constants::empty_str();
1✔
1845
    }
1846
    return d_->file_version_.get().app_name;
42✔
1847
}
1848

1849
void workbook::app_name(const std::string &app_name)
1✔
1850
{
1851
    if (!d_->file_version_.is_set())
1!
1852
    {
1853
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1854
    }
1855

1856
    d_->file_version_.get().app_name = app_name;
1✔
1857
}
1✔
1858

1859
bool workbook::has_last_edited() const
49✔
1860
{
1861
    return d_->file_version_.is_set() && !d_->file_version_.get().last_edited.empty();
49✔
1862
}
1863

1864
const std::string &workbook::last_edited_str() const
45✔
1865
{
1866
    if (!d_->file_version_.is_set())
45✔
1867
    {
1868
        return constants::empty_str();
1✔
1869
    }
1870
    return d_->file_version_.get().last_edited;
44✔
1871
}
1872

1873
std::size_t workbook::last_edited() const
4✔
1874
{
1875
    if (!has_last_edited())
4✔
1876
    {
1877
        throw xlnt::invalid_attribute("the workbook has no last edited property");
3✔
1878
    }
1879

1880
    size_t last_edited = 0;
3✔
1881
    size_t num_parsed_chars = 0;
3✔
1882
    if (detail::parse(d_->file_version_.get().last_edited, last_edited, &num_parsed_chars) != std::errc() ||
5✔
1883
        num_parsed_chars != d_->file_version_.get().last_edited.length())
2✔
1884
    {
1885
        throw xlnt::invalid_attribute("the last edited property could not be parsed from string \"" + d_->file_version_.get().last_edited + "\"");
2✔
1886
    }
1887
    return last_edited;
1✔
1888
}
1889

1890
void workbook::last_edited(const std::string &last_edited)
3✔
1891
{
1892
    if (!d_->file_version_.is_set())
3✔
1893
    {
1894
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1895
    }
1896

1897
    d_->file_version_.get().last_edited = last_edited;
3✔
1898
}
3✔
1899

1900
void workbook::last_edited(std::size_t last_edited)
1✔
1901
{
1902
    if (!d_->file_version_.is_set())
1!
1903
    {
1904
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1905
    }
1906

1907
    d_->file_version_.get().last_edited = std::to_string(last_edited);
1✔
1908
}
1✔
1909

1910
bool workbook::has_lowest_edited() const
49✔
1911
{
1912
    return d_->file_version_.is_set() && !d_->file_version_.get().lowest_edited.empty();
49✔
1913
}
1914

1915
const std::string &workbook::lowest_edited_str() const
45✔
1916
{
1917
    if (!d_->file_version_.is_set())
45✔
1918
    {
1919
        return constants::empty_str();
1✔
1920
    }
1921
    return d_->file_version_.get().lowest_edited;
44✔
1922
}
1923

1924
std::size_t workbook::lowest_edited() const
4✔
1925
{
1926
    if (!has_lowest_edited())
4✔
1927
    {
1928
        throw xlnt::invalid_attribute("the workbook has no lowest edited property");
3✔
1929
    }
1930

1931
    size_t lowest_edited = 0;
3✔
1932
    size_t num_parsed_chars = 0;
3✔
1933
    if (detail::parse(d_->file_version_.get().lowest_edited, lowest_edited, &num_parsed_chars) != std::errc() ||
5✔
1934
        num_parsed_chars != d_->file_version_.get().lowest_edited.length())
2✔
1935
    {
1936
        throw xlnt::invalid_attribute("the lowest edited property could not be parsed from string \"" + d_->file_version_.get().lowest_edited + "\"");
2✔
1937
    }
1938
    return lowest_edited;
1✔
1939
}
1940

1941
void workbook::lowest_edited(const std::string &lowest_edited)
3✔
1942
{
1943
    if (!d_->file_version_.is_set())
3✔
1944
    {
1945
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1946
    }
1947

1948
    d_->file_version_.get().lowest_edited = lowest_edited;
3✔
1949
}
3✔
1950

1951
void workbook::lowest_edited(std::size_t lowest_edited)
1✔
1952
{
1953
    if (!d_->file_version_.is_set())
1!
1954
    {
1955
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1956
    }
1957

1958
    d_->file_version_.get().lowest_edited = std::to_string(lowest_edited);
1✔
1959
}
1✔
1960

1961
bool workbook::has_rup_build() const
49✔
1962
{
1963
    return d_->file_version_.is_set() && !d_->file_version_.get().rup_build.empty();
49✔
1964
}
1965

1966
const std::string &workbook::rup_build_str() const
45✔
1967
{
1968
    if (!d_->file_version_.is_set())
45✔
1969
    {
1970
        return constants::empty_str();
1✔
1971
    }
1972
    return d_->file_version_.get().rup_build;
44✔
1973
}
1974

1975
std::size_t workbook::rup_build() const
4✔
1976
{
1977
    if (!has_rup_build())
4✔
1978
    {
1979
        throw xlnt::invalid_attribute("the workbook has no rup_build property");
3✔
1980
    }
1981

1982
    size_t rup_build = 0;
3✔
1983
    size_t num_parsed_chars = 0;
3✔
1984
    if (detail::parse(d_->file_version_.get().rup_build, rup_build, &num_parsed_chars) != std::errc() ||
5✔
1985
        num_parsed_chars != d_->file_version_.get().rup_build.length())
2✔
1986
    {
1987
        throw xlnt::invalid_attribute("the rup_build property could not be parsed from string \"" + d_->file_version_.get().rup_build + "\"");
2✔
1988
    }
1989
    return rup_build;
1✔
1990
}
1991

1992
void workbook::rup_build(const std::string &rup_build)
3✔
1993
{
1994
    if (!d_->file_version_.is_set())
3✔
1995
    {
1996
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
1997
    }
1998

1999
    d_->file_version_.get().rup_build = rup_build;
3✔
2000
}
3✔
2001

2002
void workbook::rup_build(std::size_t rup_build)
1✔
2003
{
2004
    if (!d_->file_version_.is_set())
1!
2005
    {
2006
        d_->file_version_ = detail::workbook_impl::file_version_t();
1✔
2007
    }
2008

2009
    d_->file_version_.get().rup_build = std::to_string(rup_build);
1✔
2010
}
1✔
2011

2012
bool workbook::has_calculation_properties() const
47✔
2013
{
2014
    return d_->calculation_properties_.is_set();
47✔
2015
}
2016

2017
void workbook::clear_calculation_properties()
2✔
2018
{
2019
    d_->calculation_properties_.clear();
2✔
2020
}
2✔
2021

2022
class calculation_properties workbook::calculation_properties() const
86✔
2023
{
2024
    if (!d_->calculation_properties_.is_set())
86✔
2025
    {
2026
        return {};
1✔
2027
    }
2028
    return d_->calculation_properties_.get();
85✔
2029
}
2030

2031
void workbook::calculation_properties(const class calculation_properties &props)
424✔
2032
{
2033
    d_->calculation_properties_ = props;
424✔
2034
}
424✔
2035

2036
void workbook::garbage_collect_formulae()
18✔
2037
{
2038
    auto any_with_formula = false;
18✔
2039

2040
    for (auto ws : *this)
36✔
2041
    {
2042
        for (auto row : ws.rows(true))
104✔
2043
        {
2044
            for (auto cell : row)
512✔
2045
            {
2046
                if (cell.has_formula())
426✔
2047
                {
2048
                    any_with_formula = true;
136✔
2049
                }
2050
            }
2051
        }
18✔
2052
    }
2053

2054
    if (any_with_formula) return;
18✔
2055

2056
    auto wb_rel = manifest().relationship(path("/"), relationship_type::office_document);
4✔
2057

2058
    if (manifest().has_relationship(wb_rel.target().path(), relationship_type::calculation_chain))
2!
2059
    {
2060
        auto calc_chain_rel = manifest().relationship(wb_rel.target().path(), relationship_type::calculation_chain);
2✔
2061
        auto calc_chain_part = manifest().canonicalize({wb_rel, calc_chain_rel});
10!
2062
        manifest().unregister_override_type(calc_chain_part);
2✔
2063
        manifest().unregister_relationship(wb_rel.target(), calc_chain_rel.id());
2✔
2064
    }
2✔
2065
}
4!
2066

2067
void workbook::update_sheet_properties()
414✔
2068
{
2069
    if (has_extended_property(xlnt::extended_property::titles_of_parts))
414!
2070
    {
2071
        extended_property(xlnt::extended_property::titles_of_parts, sheet_titles());
414✔
2072
    }
2073

2074
    if (has_extended_property(xlnt::extended_property::heading_pairs))
414!
2075
    {
2076
        extended_property(xlnt::extended_property::heading_pairs,
414✔
2077
            std::vector<variant>{variant("Worksheets"), variant(static_cast<int>(sheet_count()))});
2,070!
2078
    }
2079
}
828!
2080

2081
namespace {
2082
// true if a sheet index is != worksheet relationship index
2083
bool needs_reorder(const std::unordered_map<std::string, std::string> &title_to_rels,
386✔
2084
    const std::vector<std::string> &titles,
2085
    std::vector<std::string> &relation_ids)
2086
{
2087
    bool all_match = true;
386✔
2088
    for (std::size_t title_index = 0; title_index < titles.size(); ++title_index)
841✔
2089
    {
2090
        const auto &rel = title_to_rels.at(titles[title_index]);
455✔
2091
        relation_ids.push_back(rel);
455✔
2092
        const auto expected_rel_id = "rId" + std::to_string(title_index + 1);
455✔
2093
        if (rel != expected_rel_id)
455✔
2094
        {
2095
            all_match = false;
48✔
2096
        }
2097
    }
455✔
2098
    return !all_match; // if all are as expected, reorder not required
386✔
2099
}
2100

2101
struct rel_id_sorter
2102
{
2103
    // true if lhs < rhs
2104
    bool operator()(const xlnt::relationship &lhs, const xlnt::relationship &rhs)
310✔
2105
    {
2106
        // format is rTd<decimal number 1..n>
2107
        if (lhs.id().size() != rhs.id().size()) // a number with more digits will be larger
310!
2108
        {
2109
            return lhs.id().size() < rhs.id().size();
×
2110
        }
2111
        return lhs.id() < rhs.id();
310✔
2112
    }
2113
};
2114
} // namespace
2115

2116
void workbook::reorder_relationships()
386✔
2117
{
2118
    const auto titles = sheet_titles();
386✔
2119
    // the relation ID corresponding to the title at the same index is copied into here
2120
    std::vector<std::string> worksheet_rel_ids;
386✔
2121
    worksheet_rel_ids.reserve(titles.size());
386✔
2122
    if (!needs_reorder(d_->sheet_title_rel_id_map_, titles, worksheet_rel_ids))
386✔
2123
    {
2124
        return;
339✔
2125
    }
2126
    // copy of existing relations
2127
    const auto wb_rel_target = manifest().relationship(path("/"), relationship_type::office_document).target();
94✔
2128
    auto rel_copy = manifest().relationships(wb_rel_target.path());
47✔
2129
    std::sort(rel_copy.begin(), rel_copy.end(), rel_id_sorter{});
47✔
2130
    // clear existing relations
2131
    for (const auto &rel : rel_copy)
260✔
2132
    {
2133
        manifest().unregister_relationship(wb_rel_target, rel.id());
213✔
2134
    }
2135
    // create new relations
2136
    std::size_t index = 0;
47✔
2137
    auto new_id = [&index]() { return "rId" + std::to_string(++index); }; // ids start from 1
213✔
2138
    // worksheets first
2139
    while (index < worksheet_rel_ids.size())
163✔
2140
    {
2141
        auto rel_it = std::find_if(rel_copy.begin(), rel_copy.end(),
116✔
2142
            [&](const relationship &rel) { return rel.id() == worksheet_rel_ids[index]; });
318✔
2143

2144
        std::string rel_id = new_id();
116✔
2145
        d_->sheet_title_rel_id_map_.at(titles[index - 1]) = rel_id; // update title -> relation mapping
116✔
2146
        manifest().register_relationship(relationship(rel_id, rel_it->type(),
232✔
2147
            rel_it->source(), rel_it->target(), rel_it->target_mode()));
116✔
2148
    }
116✔
2149
    // then all the other relations in the same order they started (just new indices)
2150
    for (const auto &old_rel : rel_copy)
260✔
2151
    {
2152
        if (old_rel.type() == relationship_type::worksheet)
213✔
2153
        {
2154
            continue;
116✔
2155
        }
2156
        manifest().register_relationship(relationship(new_id(), old_rel.type(),
194✔
2157
            old_rel.source(), old_rel.target(), old_rel.target_mode()));
97✔
2158
    }
2159
}
725✔
2160

2161
} // namespace xlnt
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc