implement sheet view serialization

This commit is contained in:
Thomas Fussell 2018-03-14 20:12:07 -04:00
parent 0f0d3de75f
commit 2426215801
8 changed files with 69 additions and 14 deletions

View File

@ -74,6 +74,8 @@ public:
range_reference(column_t column_index_start, row_t row_index_start, range_reference(column_t column_index_start, row_t row_index_start,
column_t column_index_end, row_t row_index_end); column_t column_index_end, row_t row_index_end);
range_reference(const range_reference &ref);
/// <summary> /// <summary>
/// Returns true if the range has a width and height of 1 cell. /// Returns true if the range has a width and height of 1 cell.
/// </summary> /// </summary>
@ -154,6 +156,11 @@ public:
/// </summary> /// </summary>
bool operator!=(const char *reference_string) const; bool operator!=(const char *reference_string) const;
/// <summary>
/// Assigns the extents of the provided range to this range.
/// </summary>
range_reference &operator=(const range_reference &ref);
private: private:
/// <summary> /// <summary>
/// The top left cell in the range /// The top left cell in the range

View File

@ -60,12 +60,36 @@ public:
active_cell_ = ref; active_cell_ = ref;
} }
/// <summary>
/// Returns true if this selection has a defined sqref.
/// </summary>
bool has_sqref() const
{
return sqref_.is_set();
}
/// <summary> /// <summary>
/// Returns the range encompassed by this selection. /// Returns the range encompassed by this selection.
/// </summary> /// </summary>
range_reference sqref() const range_reference sqref() const
{ {
return sqref_; return sqref_.get();
}
/// <summary>
/// Sets the range encompassed by this selection.
/// </summary>
void sqref(const range_reference &ref)
{
sqref_ = ref;
}
/// <summary>
/// Sets the range encompassed by this selection.
/// </summary>
void sqref(const std::string &ref)
{
sqref(range_reference(ref));
} }
/// <summary> /// <summary>
@ -104,7 +128,7 @@ private:
/// <summary> /// <summary>
/// The range /// The range
/// </summary> /// </summary>
range_reference sqref_; optional<range_reference> sqref_;
/// <summary> /// <summary>
/// The quadrant /// The quadrant

View File

@ -654,7 +654,7 @@ public:
/// <summary> /// <summary>
/// Returns the view at the given index. /// Returns the view at the given index.
/// </summary> /// </summary>
sheet_view view(std::size_t index = 0) const; sheet_view &view(std::size_t index = 0) const;
/// <summary> /// <summary>
/// Adds new_view to the set of available views for this sheet. /// Adds new_view to the set of available views for this sheet.

View File

@ -441,9 +441,9 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id)
: sheet_view_type::page_layout); : sheet_view_type::page_layout);
} }
skip_attributes({ "windowProtection", "showFormulas", "showRowColHeaders", "showZeros", "rightToLeft", skip_attributes({ "windowProtection", "showFormulas", "showRowColHeaders", "showZeros", "rightToLeft", "tabSelected", "showRuler", "showOutlineSymbols", "showWhiteSpace",
"tabSelected", "showRuler", "showOutlineSymbols", "showWhiteSpace", "view", "topLeftCell", "view", "topLeftCell", "colorId", "zoomScale", "zoomScaleNormal", "zoomScaleSheetLayoutView",
"colorId", "zoomScale", "zoomScaleNormal", "zoomScaleSheetLayoutView", "zoomScalePageLayoutView" }); "zoomScalePageLayoutView" });
while (in_element(qn("spreadsheetml", "sheetView"))) while (in_element(qn("spreadsheetml", "sheetView")))
{ {
@ -489,6 +489,12 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id)
current_selection.active_cell(parser().attribute("activeCell")); current_selection.active_cell(parser().attribute("activeCell"));
} }
if (parser().attribute_present("sqref"))
{
const auto sqref = range_reference(parser().attribute("sqref"));
current_selection.sqref(sqref);
}
current_selection.pane(pane_corner::top_left); current_selection.pane(pane_corner::top_left);
new_view.add_selection(current_selection); new_view.add_selection(current_selection);

View File

@ -2093,14 +2093,13 @@ void xlsx_producer::write_worksheet(const relationship &rel)
if (current_selection.has_active_cell()) if (current_selection.has_active_cell())
{ {
write_attribute("activeCell", current_selection.active_cell().to_string()); write_attribute("activeCell", current_selection.active_cell().to_string());
write_attribute("sqref", current_selection.active_cell().to_string());
} }
/*
if (current_selection.sqref() != "A1:A1") if (current_selection.has_sqref())
{ {
write_attribute("sqref", current_selection.sqref().to_string()); write_attribute("sqref", current_selection.sqref().to_string());
} }
*/
if (current_selection.pane() != pane_corner::top_left) if (current_selection.pane() != pane_corner::top_left)
{ {
write_attribute("pane", current_selection.pane()); write_attribute("pane", current_selection.pane());

View File

@ -46,6 +46,12 @@ range_reference::range_reference(const char *range_string)
{ {
} }
range_reference::range_reference(const range_reference &ref)
{
top_left_ = ref.top_left_;
bottom_right_ = ref.bottom_right_;
}
range_reference::range_reference(const std::string &range_string) range_reference::range_reference(const std::string &range_string)
: top_left_("A1"), bottom_right_("A1") : top_left_("A1"), bottom_right_("A1")
{ {
@ -178,4 +184,11 @@ XLNT_API bool operator!=(const char *reference_string, const range_reference &re
return ref != reference_string; return ref != reference_string;
} }
range_reference &range_reference::operator=(const range_reference &ref)
{
top_left_ = ref.top_left_;
bottom_right_ = ref.bottom_right_;
return *this;
}
} // namespace xlnt } // namespace xlnt

View File

@ -1011,7 +1011,7 @@ bool worksheet::has_view() const
return !d_->views_.empty(); return !d_->views_.empty();
} }
sheet_view worksheet::view(std::size_t index) const sheet_view &worksheet::view(std::size_t index) const
{ {
return d_->views_.at(index); return d_->views_.at(index);
} }

View File

@ -189,6 +189,12 @@ public:
auto sheet1 = wb.active_sheet(); auto sheet1 = wb.active_sheet();
sheet1.format_properties(format_properties); sheet1.format_properties(format_properties);
auto &view = sheet1.view();
auto selection = xlnt::selection();
selection.active_cell("C1");
selection.sqref("C1");
view.add_selection(selection);
auto comment_font = xlnt::font() auto comment_font = xlnt::font()
.bold(true) .bold(true)
.size(10) .size(10)