range.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <xlnt/cell/cell.hpp>
  24. #include <xlnt/styles/style.hpp>
  25. #include <xlnt/workbook/workbook.hpp>
  26. #include <xlnt/worksheet/range.hpp>
  27. #include <xlnt/worksheet/range_iterator.hpp>
  28. #include <xlnt/worksheet/range_reference.hpp>
  29. #include <xlnt/worksheet/worksheet.hpp>
  30. namespace xlnt {
  31. range::range(class worksheet ws, const range_reference &reference, major_order order, bool skip_null)
  32. : ws_(ws),
  33. ref_(reference),
  34. order_(order),
  35. skip_null_(skip_null)
  36. {
  37. }
  38. range::~range() = default;
  39. void range::clear_cells()
  40. {
  41. if (ref_.top_left().column() == ws_.lowest_column()
  42. && ref_.bottom_right().column() == ws_.highest_column())
  43. {
  44. for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
  45. {
  46. ws_.clear_row(row);
  47. }
  48. }
  49. else
  50. {
  51. for (auto row = ref_.top_left().row(); row <= ref_.bottom_right().row(); ++row)
  52. {
  53. for (auto column = ref_.top_left().column(); column <= ref_.bottom_right().column(); ++column)
  54. {
  55. ws_.clear_cell(xlnt::cell_reference(column, row));
  56. }
  57. }
  58. }
  59. }
  60. cell_vector range::operator[](std::size_t index)
  61. {
  62. return vector(index);
  63. }
  64. const cell_vector range::operator[](std::size_t index) const
  65. {
  66. return vector(index);
  67. }
  68. const worksheet &range::target_worksheet() const
  69. {
  70. return ws_;
  71. }
  72. range_reference range::reference() const
  73. {
  74. return ref_;
  75. }
  76. std::size_t range::length() const
  77. {
  78. if (order_ == major_order::row)
  79. {
  80. return ref_.bottom_right().row() - ref_.top_left().row() + 1;
  81. }
  82. return (ref_.bottom_right().column() - ref_.top_left().column()).index + 1;
  83. }
  84. bool range::operator==(const range &comparand) const
  85. {
  86. return ref_ == comparand.ref_
  87. && ws_ == comparand.ws_
  88. && order_ == comparand.order_;
  89. }
  90. cell_vector range::vector(std::size_t vector_index)
  91. {
  92. auto cursor = ref_.top_left();
  93. if (order_ == major_order::row)
  94. {
  95. cursor.row(cursor.row() + static_cast<row_t>(vector_index));
  96. }
  97. else
  98. {
  99. cursor.column_index(cursor.column_index() + static_cast<column_t::index_t>(vector_index));
  100. }
  101. return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
  102. }
  103. const cell_vector range::vector(std::size_t vector_index) const
  104. {
  105. auto cursor = ref_.top_left();
  106. if (order_ == major_order::row)
  107. {
  108. cursor.row(cursor.row() + static_cast<row_t>(vector_index));
  109. }
  110. else
  111. {
  112. cursor.column_index(cursor.column_index() + static_cast<column_t::index_t>(vector_index));
  113. }
  114. return cell_vector(ws_, cursor, ref_, order_, skip_null_, false);
  115. }
  116. bool range::contains(const cell_reference &cell_ref)
  117. {
  118. return ref_.contains(cell_ref);
  119. }
  120. range range::alignment(const xlnt::alignment &new_alignment)
  121. {
  122. apply([&new_alignment](class cell c) { c.alignment(new_alignment); });
  123. return *this;
  124. }
  125. range range::border(const xlnt::border &new_border)
  126. {
  127. apply([&new_border](class cell c) { c.border(new_border); });
  128. return *this;
  129. }
  130. range range::fill(const xlnt::fill &new_fill)
  131. {
  132. apply([&new_fill](class cell c) { c.fill(new_fill); });
  133. return *this;
  134. }
  135. range range::font(const xlnt::font &new_font)
  136. {
  137. apply([&new_font](class cell c) { c.font(new_font); });
  138. return *this;
  139. }
  140. range range::number_format(const xlnt::number_format &new_number_format)
  141. {
  142. apply([&new_number_format](class cell c) { c.number_format(new_number_format); });
  143. return *this;
  144. }
  145. range range::protection(const xlnt::protection &new_protection)
  146. {
  147. apply([&new_protection](class cell c) { c.protection(new_protection); });
  148. return *this;
  149. }
  150. range range::style(const class style &new_style)
  151. {
  152. apply([&new_style](class cell c) { c.style(new_style); });
  153. return *this;
  154. }
  155. range range::style(const std::string &style_name)
  156. {
  157. return style(ws_.workbook().style(style_name));
  158. }
  159. conditional_format range::conditional_format(const condition &when)
  160. {
  161. return ws_.conditional_format(ref_, when);
  162. }
  163. void range::apply(std::function<void(class cell)> f)
  164. {
  165. for (auto row : *this)
  166. {
  167. for (auto cell : row)
  168. {
  169. f(cell);
  170. }
  171. }
  172. }
  173. cell range::cell(const cell_reference &ref)
  174. {
  175. return (*this)[ref.row() - 1][ref.column().index - 1];
  176. }
  177. const cell range::cell(const cell_reference &ref) const
  178. {
  179. return (*this)[ref.row() - 1][ref.column().index - 1];
  180. }
  181. cell_vector range::front()
  182. {
  183. return *begin();
  184. }
  185. const cell_vector range::front() const
  186. {
  187. return *cbegin();
  188. }
  189. cell_vector range::back()
  190. {
  191. return *(--end());
  192. }
  193. const cell_vector range::back() const
  194. {
  195. return *(--cend());
  196. }
  197. range::iterator range::begin()
  198. {
  199. return iterator(ws_, ref_.top_left(), ref_, order_, skip_null_);
  200. }
  201. range::iterator range::end()
  202. {
  203. auto cursor = ref_.top_left();
  204. if (order_ == major_order::row)
  205. {
  206. cursor.row(ref_.bottom_right().row() + 1);
  207. }
  208. else
  209. {
  210. cursor.column_index(ref_.bottom_right().column_index() + 1);
  211. }
  212. return iterator(ws_, cursor, ref_, order_, skip_null_);
  213. }
  214. range::const_iterator range::cbegin() const
  215. {
  216. return const_iterator(ws_, ref_.top_left(), ref_, order_, skip_null_);
  217. }
  218. range::const_iterator range::cend() const
  219. {
  220. auto cursor = ref_.top_left();
  221. if (order_ == major_order::row)
  222. {
  223. cursor.row(ref_.bottom_right().row() + 1);
  224. }
  225. else
  226. {
  227. cursor.column_index(ref_.bottom_right().column_index() + 1);
  228. }
  229. return const_iterator(ws_, cursor, ref_, order_, skip_null_);
  230. }
  231. bool range::operator!=(const range &comparand) const
  232. {
  233. return !(*this == comparand);
  234. }
  235. range::const_iterator range::begin() const
  236. {
  237. return cbegin();
  238. }
  239. range::const_iterator range::end() const
  240. {
  241. return cend();
  242. }
  243. range::reverse_iterator range::rbegin()
  244. {
  245. return reverse_iterator(end());
  246. }
  247. range::reverse_iterator range::rend()
  248. {
  249. return reverse_iterator(begin());
  250. }
  251. range::const_reverse_iterator range::crbegin() const
  252. {
  253. return const_reverse_iterator(cend());
  254. }
  255. range::const_reverse_iterator range::rbegin() const
  256. {
  257. return crbegin();
  258. }
  259. range::const_reverse_iterator range::crend() const
  260. {
  261. return const_reverse_iterator(cbegin());
  262. }
  263. range::const_reverse_iterator range::rend() const
  264. {
  265. return crend();
  266. }
  267. } // namespace xlnt