diff --git a/include/xlnt/worksheet/range_reference.hpp b/include/xlnt/worksheet/range_reference.hpp
index 7429bc58..f5956c75 100644
--- a/include/xlnt/worksheet/range_reference.hpp
+++ b/include/xlnt/worksheet/range_reference.hpp
@@ -74,6 +74,8 @@ public:
range_reference(column_t column_index_start, row_t row_index_start,
column_t column_index_end, row_t row_index_end);
+ range_reference(const range_reference &ref);
+
///
/// Returns true if the range has a width and height of 1 cell.
///
@@ -154,6 +156,11 @@ public:
///
bool operator!=(const char *reference_string) const;
+ ///
+ /// Assigns the extents of the provided range to this range.
+ ///
+ range_reference &operator=(const range_reference &ref);
+
private:
///
/// The top left cell in the range
diff --git a/include/xlnt/worksheet/selection.hpp b/include/xlnt/worksheet/selection.hpp
index 02ce66a0..bff902e7 100644
--- a/include/xlnt/worksheet/selection.hpp
+++ b/include/xlnt/worksheet/selection.hpp
@@ -60,12 +60,36 @@ public:
active_cell_ = ref;
}
+ ///
+ /// Returns true if this selection has a defined sqref.
+ ///
+ bool has_sqref() const
+ {
+ return sqref_.is_set();
+ }
+
///
/// Returns the range encompassed by this selection.
///
range_reference sqref() const
{
- return sqref_;
+ return sqref_.get();
+ }
+
+ ///
+ /// Sets the range encompassed by this selection.
+ ///
+ void sqref(const range_reference &ref)
+ {
+ sqref_ = ref;
+ }
+
+ ///
+ /// Sets the range encompassed by this selection.
+ ///
+ void sqref(const std::string &ref)
+ {
+ sqref(range_reference(ref));
}
///
@@ -104,7 +128,7 @@ private:
///
/// The range
///
- range_reference sqref_;
+ optional sqref_;
///
/// The quadrant
diff --git a/include/xlnt/worksheet/worksheet.hpp b/include/xlnt/worksheet/worksheet.hpp
index 9c3fdd18..58227b85 100644
--- a/include/xlnt/worksheet/worksheet.hpp
+++ b/include/xlnt/worksheet/worksheet.hpp
@@ -654,7 +654,7 @@ public:
///
/// Returns the view at the given index.
///
- sheet_view view(std::size_t index = 0) const;
+ sheet_view &view(std::size_t index = 0) const;
///
/// Adds new_view to the set of available views for this sheet.
diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp
index b61c82da..4086d68f 100644
--- a/source/detail/serialization/xlsx_consumer.cpp
+++ b/source/detail/serialization/xlsx_consumer.cpp
@@ -441,9 +441,9 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id)
: sheet_view_type::page_layout);
}
- skip_attributes({ "windowProtection", "showFormulas", "showRowColHeaders", "showZeros", "rightToLeft",
- "tabSelected", "showRuler", "showOutlineSymbols", "showWhiteSpace", "view", "topLeftCell",
- "colorId", "zoomScale", "zoomScaleNormal", "zoomScaleSheetLayoutView", "zoomScalePageLayoutView" });
+ skip_attributes({ "windowProtection", "showFormulas", "showRowColHeaders", "showZeros", "rightToLeft", "tabSelected", "showRuler", "showOutlineSymbols", "showWhiteSpace",
+ "view", "topLeftCell", "colorId", "zoomScale", "zoomScaleNormal", "zoomScaleSheetLayoutView",
+ "zoomScalePageLayoutView" });
while (in_element(qn("spreadsheetml", "sheetView")))
{
@@ -488,6 +488,12 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id)
{
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);
diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp
index da1e5156..f5611f88 100644
--- a/source/detail/serialization/xlsx_producer.cpp
+++ b/source/detail/serialization/xlsx_producer.cpp
@@ -2093,14 +2093,13 @@ void xlsx_producer::write_worksheet(const relationship &rel)
if (current_selection.has_active_cell())
{
write_attribute("activeCell", current_selection.active_cell().to_string());
- write_attribute("sqref", current_selection.active_cell().to_string());
}
- /*
- if (current_selection.sqref() != "A1:A1")
- {
- write_attribute("sqref", current_selection.sqref().to_string());
- }
- */
+
+ if (current_selection.has_sqref())
+ {
+ write_attribute("sqref", current_selection.sqref().to_string());
+ }
+
if (current_selection.pane() != pane_corner::top_left)
{
write_attribute("pane", current_selection.pane());
diff --git a/source/worksheet/range_reference.cpp b/source/worksheet/range_reference.cpp
index 2c4cf0ec..7116c077 100644
--- a/source/worksheet/range_reference.cpp
+++ b/source/worksheet/range_reference.cpp
@@ -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)
: 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;
}
+range_reference &range_reference::operator=(const range_reference &ref)
+{
+ top_left_ = ref.top_left_;
+ bottom_right_ = ref.bottom_right_;
+ return *this;
+}
+
} // namespace xlnt
diff --git a/source/worksheet/worksheet.cpp b/source/worksheet/worksheet.cpp
index 861536db..f240e895 100644
--- a/source/worksheet/worksheet.cpp
+++ b/source/worksheet/worksheet.cpp
@@ -1011,7 +1011,7 @@ bool worksheet::has_view() const
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);
}
diff --git a/tests/workbook/serialization_test_suite.hpp b/tests/workbook/serialization_test_suite.hpp
index 1da4cb1a..a4ae2fd2 100644
--- a/tests/workbook/serialization_test_suite.hpp
+++ b/tests/workbook/serialization_test_suite.hpp
@@ -189,6 +189,12 @@ public:
auto sheet1 = wb.active_sheet();
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()
.bold(true)
.size(10)