Improved handling of automatically generated names

so printArea etc. This also fixes a number of tests.
pull/686/head
snippet 2022-11-13 12:15:21 +11:00
parent da5a6756e7
commit ccad2fb646
26 changed files with 145 additions and 9 deletions

View File

@ -94,7 +94,7 @@ struct workbook_impl
return *this;
}
bool operator==(const workbook_impl &other)
bool operator==(const workbook_impl &other) const
{
if (defined_names_.size() != other.defined_names_.size()) return false;

View File

@ -41,6 +41,7 @@
#include <xlnt/worksheet/print_options.hpp>
#include <xlnt/worksheet/sheet_pr.hpp>
#include <detail/implementations/cell_impl.hpp>
#include <detail/serialization/defined_name.hpp>
namespace xlnt {
@ -88,6 +89,8 @@ struct worksheet_impl
extension_list_ = other.extension_list_;
sheet_properties_ = other.sheet_properties_;
print_options_ = other.print_options_;
defined_names_ = other.defined_names_;
for (auto &cell : cell_map_)
{
@ -97,8 +100,15 @@ struct worksheet_impl
workbook *parent_;
bool operator==(const worksheet_impl& rhs) const
bool operator==(const worksheet_impl &rhs) const
{
if (defined_names_.size() != rhs.defined_names_.size()) return false;
for (std::size_t i = 0; i < defined_names_.size(); i++)
{
if (defined_names_[i] != rhs.defined_names_[i]) return false;
}
return id_ == rhs.id_
&& title_ == rhs.title_
&& format_properties_ == rhs.format_properties_
@ -122,6 +132,7 @@ struct worksheet_impl
&& print_options_ == rhs.print_options_
&& sheet_properties_ == rhs.sheet_properties_
&& extension_list_ == rhs.extension_list_;
}
std::size_t id_;
@ -160,6 +171,8 @@ struct worksheet_impl
std::string drawing_rel_id_;
optional<drawing::spreadsheet_drawing> drawing_;
std::vector<defined_name> defined_names_;
};
} // namespace detail

View File

@ -51,7 +51,7 @@ struct defined_name
return *this;
}
bool operator==(const defined_name &other)
bool operator==(const defined_name &other) const
{
return name == other.name
&& comment == other.comment
@ -66,7 +66,8 @@ struct defined_name
&& shortcut_key == other.shortcut_key
&& value == other.value;
}
bool operator!=(const defined_name &other)
bool operator!=(const defined_name &other) const
{
return !(*this == other);
}

View File

@ -534,7 +534,7 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id)
expect_start_element(qn("spreadsheetml", "worksheet"), xml::content::complex); // CT_Worksheet
skip_attributes({qn("mc", "Ignorable")});
read_defined_names(ws, defined_names_);
read_defined_names(ws, ws.d_->defined_names_);
while (in_element(qn("spreadsheetml", "worksheet")))
{
@ -2240,6 +2240,7 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
}
}
std::vector<defined_name> workbook_names;
for (auto worksheet_rel : manifest().relationships(workbook_path, relationship_type::worksheet))
{
auto title = std::find_if(target_.d_->sheet_title_rel_id_map_.begin(),
@ -2248,7 +2249,7 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
return p.second == worksheet_rel.id();
})->first;
auto id = sheet_title_id_map_[title];
auto id = sheet_title_id_map_[title]; // 1-indexed
auto index = sheet_title_index_map_[title];
auto insertion_iter = target_.d_->worksheets_.begin();
@ -2260,11 +2261,33 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_
current_worksheet_ = &*target_.d_->worksheets_.emplace(insertion_iter, &target_, id, title);
// If there's any defined names that are worksheet specific, move them here.
for (std::size_t i = 0; i < target_.d_->defined_names_.size(); i++)
{
const auto &name = target_.d_->defined_names_[i];
if (name.sheet_id.is_set())
{
const auto target_id = name.sheet_id.get();
if (target_id == index)
{
// It's a match, remove it from the workbook and add it to the sheet
current_worksheet_->defined_names_.push_back(name);
}
}
else
{
// Name is global and belongs to the workbook
workbook_names.push_back(name);
}
}
if (!streaming_)
{
read_part({workbook_rel, worksheet_rel});
}
}
// Update the workbook with the new defined names
target_.d_->defined_names_ = workbook_names;
}
// Write Workbook Relationship Target Parts

View File

@ -433,7 +433,84 @@ void xlsx_producer::write_custom_properties(const relationship & /*rel*/)
void xlsx_producer::write_workbook(const relationship &rel)
{
std::size_t num_visible = 0;
std::vector<defined_name> defined_names;
defined_names = source_.d_->defined_names_;
for (auto ws : source_)
{
if (!ws.has_page_setup() || ws.page_setup().sheet_state() == sheet_state::visible)
{
num_visible++;
}
auto title_ref = "'" + ws.title() + "'!";
bool added_auto_filter = false;
bool added_print_area = false;
bool added_print_titles = false;
if (ws.has_auto_filter())
{
defined_name name;
name.sheet_id = ws.id();
name.name = "_xlnm._FilterDatabase";
name.hidden = true;
name.value = title_ref + range_reference::make_absolute(ws.auto_filter()).to_string();
defined_names.push_back(name);
added_auto_filter = true;
}
if (ws.has_print_area())
{
defined_name name;
name.sheet_id = ws.id();
name.name = "_xlnm.Print_Area";
name.hidden = false;
name.value = title_ref + range_reference::make_absolute(ws.print_area()).to_string();
defined_names.push_back(name);
added_print_area = true;
}
if (ws.has_print_titles())
{
defined_name name;
name.sheet_id = ws.id();
name.name = "_xlnm.Print_Titles";
name.hidden = false;
auto cols = ws.print_title_cols();
if (cols.is_set())
{
name.value = title_ref + "$" + cols.get().first.column_string()
+ ":" + "$" + cols.get().second.column_string();
}
auto rows = ws.print_title_rows();
if (rows.is_set())
{
if (!name.value.empty())
{
name.value.push_back(',');
}
name.value += title_ref + "$" + std::to_string(rows.get().first)
+ ":" + "$" + std::to_string(rows.get().second);
}
defined_names.push_back(name);
added_print_titles = true;
}
// Add any sheet defined names to the vector
for (const auto &sheet_defined_name : ws.d_->defined_names_)
{
if (sheet_defined_name.name == "_xlnm._FilterDatabase" && !added_auto_filter)
defined_names.push_back(sheet_defined_name);
else if (sheet_defined_name.name == "_xlnm.Print_Area" && !added_auto_filter)
defined_names.push_back(sheet_defined_name);
else if (sheet_defined_name.name == "_xlnm.Print_Titles" && !added_auto_filter)
defined_names.push_back(sheet_defined_name);
else
defined_names.push_back(sheet_defined_name);
}
}
if (num_visible == 0)
{
@ -600,10 +677,10 @@ void xlsx_producer::write_workbook(const relationship &rel)
write_end_element(xmlns, "sheets");
if (!source_.d_->defined_names_.empty())
if (!defined_names.empty())
{
write_start_element(xmlns, "definedNames");
for (auto name : source_.d_->defined_names_)
for (auto name : defined_names)
{
write_start_element(xmlns, "definedName");
write_attribute("name", name.name);
@ -636,7 +713,7 @@ void xlsx_producer::write_workbook(const relationship &rel)
if (name.sheet_id.is_set())
{
const auto sheet_id = name.sheet_id.get();
write_attribute("localSheetId", std::to_string(sheet_id)); // Don't think this is meant to require subtracting 1?
write_attribute("localSheetId", std::to_string(sheet_id - 1)); // Don't think this is meant to require subtracting 1?
}
if (name.hidden.is_set())

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override PartName="/xl/worksheets/sheet2.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override PartName="/xl/worksheets/sheet3.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/><Override PartName="/xl/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/><Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/><Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/></Types>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>Microsoft Macintosh Excel</Application><DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><HeadingPairs><vt:vector size="4" baseType="variant"><vt:variant><vt:lpstr>Worksheets</vt:lpstr></vt:variant><vt:variant><vt:i4>3</vt:i4></vt:variant><vt:variant><vt:lpstr>Named Ranges</vt:lpstr></vt:variant><vt:variant><vt:i4>5</vt:i4></vt:variant></vt:vector></HeadingPairs><TitlesOfParts><vt:vector size="8" baseType="lpstr"><vt:lpstr>Sheet1</vt:lpstr><vt:lpstr>Sheet2</vt:lpstr><vt:lpstr>Sheet3</vt:lpstr><vt:lpstr>Sheet2!Print_Area</vt:lpstr><vt:lpstr>Sheet3!Print_Area</vt:lpstr><vt:lpstr>Sheet1!Print_Titles</vt:lpstr><vt:lpstr>Sheet2!Print_Titles</vt:lpstr><vt:lpstr>Sheet3!Print_Titles</vt:lpstr></vt:vector></TitlesOfParts><Company></Company><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>16.0300</AppVersion></Properties>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:creator>Microsoft Office User</dc:creator><cp:lastModifiedBy>Microsoft Office User</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">2016-08-12T03:16:56Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2022-08-21T15:08:32Z</dcterms:modified></cp:coreProperties>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet3.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/><Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/></Relationships>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2 xr" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"><fonts count="1" x14ac:knownFonts="1"><font><sz val="12"/><color theme="1"/><name val="Calibri"/><family val="2"/><scheme val="minor"/></font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="gray125"/></fill></fills><borders count="1"><border><left/><right/><top/><bottom/><diagonal/></border></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/></cellXfs><cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles><dxfs count="0"/><tableStyles count="0" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleMedium7"/><extLst><ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"><x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/></ext><ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"><x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/></ext></extLst></styleSheet>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15 xr xr6 xr10 xr2" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr6="http://schemas.microsoft.com/office/spreadsheetml/2016/revision6" xmlns:xr10="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"><fileVersion appName="xl" lastEdited="7" lowestEdited="6" rupBuild="11012"/><workbookPr/><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><mc:Choice Requires="x15"><x15ac:absPath url="/Users/thomas/Development/xlnt/tests/data/" xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac"/></mc:Choice></mc:AlternateContent><xr:revisionPtr revIDLastSave="0" documentId="8_{A9B1BB36-AB65-B147-A693-E8E871AE14CA}" xr6:coauthVersionLast="36" xr6:coauthVersionMax="36" xr10:uidLastSave="{00000000-0000-0000-0000-000000000000}"/><bookViews><workbookView xWindow="0" yWindow="760" windowWidth="28800" windowHeight="17400" tabRatio="500" xr2:uid="{00000000-000D-0000-FFFF-FFFF00000000}"/></bookViews><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/><sheet name="Sheet2" sheetId="2" r:id="rId2"/><sheet name="Sheet3" sheetId="3" r:id="rId3"/></sheets><definedNames><definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">Sheet1!$A$1:$A$6</definedName><definedName name="_xlnm.Print_Area" localSheetId="1">Sheet2!$B$4</definedName><definedName name="_xlnm.Print_Area" localSheetId="2">Sheet3!$B$2:$E$4</definedName><definedName name="_xlnm.Print_Titles" localSheetId="0">Sheet1!$1:$3</definedName><definedName name="_xlnm.Print_Titles" localSheetId="1">Sheet2!$A:$D</definedName><definedName name="_xlnm.Print_Titles" localSheetId="2">Sheet3!$B:$E,Sheet3!$2:$4</definedName></definedNames><calcPr calcId="150000"/></workbook>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{00000000-0001-0000-0000-000000000000}"><sheetPr filterMode="1"/><dimension ref="A1:A6"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A4" sqref="A4"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="16" x14ac:dyDescent="0.2"/><sheetData><row r="1" spans="1:1" x14ac:dyDescent="0.2"><c r="A1"><v>1</v></c></row><row r="2" spans="1:1" hidden="1" x14ac:dyDescent="0.2"><c r="A2"><v>2</v></c></row><row r="3" spans="1:1" hidden="1" x14ac:dyDescent="0.2"><c r="A3"><v>3</v></c></row><row r="4" spans="1:1" x14ac:dyDescent="0.2"><c r="A4"><v>4</v></c></row><row r="5" spans="1:1" x14ac:dyDescent="0.2"><c r="A5"><v>5</v></c></row><row r="6" spans="1:1" x14ac:dyDescent="0.2"><c r="A6"><v>6</v></c></row></sheetData><autoFilter ref="A1:A6" xr:uid="{92FD0A6F-3B87-3B48-91F2-658499CB39CF}"><filterColumn colId="0"><customFilters><customFilter operator="greaterThan" val="3"/></customFilters></filterColumn></autoFilter><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/></worksheet>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{00000000-0001-0000-0100-000000000000}"><dimension ref="A1"/><sheetViews><sheetView workbookViewId="0"><selection activeCell="B4" sqref="B4"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultColWidth="8.83203125" defaultRowHeight="16" x14ac:dyDescent="0.2"/><sheetData/><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup orientation="portrait" horizontalDpi="0" verticalDpi="0"/></worksheet>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{00000000-0001-0000-0200-000000000000}"><dimension ref="A1"/><sheetViews><sheetView workbookViewId="0"><selection activeCell="B2" sqref="B2:E4"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultColWidth="8.83203125" defaultRowHeight="16" x14ac:dyDescent="0.2"/><sheetData/><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup orientation="portrait" horizontalDpi="0" verticalDpi="0"/></worksheet>

BIN
tests/temp.xlsx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
tests/temp2_minimal.xlsx Normal file

Binary file not shown.

BIN
tests/temp3_default.xlsx Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
tests/titles1.xlsx Normal file

Binary file not shown.