named_range.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. // Copyright (c) 2010-2015 openpyxl
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE
  21. //
  22. // @license: http://www.opensource.org/licenses/mit-license.php
  23. // @author: see AUTHORS file
  24. #include <xlnt/utils/exceptions.hpp>
  25. #include <xlnt/workbook/named_range.hpp>
  26. #include <xlnt/worksheet/range_reference.hpp>
  27. #include <xlnt/worksheet/worksheet.hpp>
  28. namespace {
  29. /// <summary>
  30. /// Return a vector containing string split at each delim.
  31. /// </summary>
  32. /// <remark>
  33. /// This should maybe be in a utility header so it can be used elsewhere.
  34. /// </remarks>
  35. /*
  36. std::vector<std::string> split_string(const std::string &string, char delim)
  37. {
  38. std::vector<std::string> split;
  39. std::string::size_type previous_index = 0;
  40. auto separator_index = string.find(delim);
  41. while (separator_index != std::string::npos)
  42. {
  43. auto part = string.substr(previous_index, separator_index - previous_index);
  44. split.push_back(part);
  45. previous_index = separator_index + 1;
  46. separator_index = string.find(delim, previous_index);
  47. }
  48. split.push_back(string.substr(previous_index));
  49. return split;
  50. }
  51. */
  52. /*
  53. std::vector<std::pair<std::string, std::string>> split_named_range(const std::string &named_range_string)
  54. {
  55. std::vector<std::pair<std::string, std::string>> result;
  56. for (auto part : split_string(named_range_string, ','))
  57. {
  58. auto split = split_string(part, '!');
  59. if (split[0].front() == '\'' && split[0].back() == '\'')
  60. {
  61. split[0] = split[0].substr(1, split[0].length() - 2);
  62. }
  63. // Try to parse it. Use empty string if it's not a valid range.
  64. try
  65. {
  66. xlnt::range_reference ref(split[1]);
  67. }
  68. catch (xlnt::invalid_cell_reference)
  69. {
  70. split[1] = "";
  71. }
  72. result.push_back({ split[0], split[1] });
  73. }
  74. return result;
  75. }
  76. */
  77. } // namespace
  78. namespace xlnt {
  79. std::string named_range::name() const
  80. {
  81. return name_;
  82. }
  83. const std::vector<named_range::target> &named_range::targets() const
  84. {
  85. return targets_;
  86. }
  87. named_range::named_range()
  88. {
  89. }
  90. named_range::named_range(const named_range &other)
  91. {
  92. *this = other;
  93. }
  94. named_range::named_range(const std::string &name, const std::vector<named_range::target> &targets)
  95. : name_(name), targets_(targets)
  96. {
  97. }
  98. named_range &named_range::operator=(const named_range &other)
  99. {
  100. name_ = other.name_;
  101. targets_ = other.targets_;
  102. return *this;
  103. }
  104. bool named_range::operator==(const named_range &rhs) const
  105. {
  106. return name_ == rhs.name_
  107. && targets_ == rhs.targets_;
  108. }
  109. } // namespace xlnt