Cell.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Cell.h"
  7. #include "Spreadsheet.h"
  8. #include <AK/StringBuilder.h>
  9. #include <AK/TemporaryChange.h>
  10. namespace Spreadsheet {
  11. void Cell::set_data(String new_data)
  12. {
  13. // If we are a formula, we do not save the beginning '=', if the new_data is "" we can simply change our kind
  14. if (m_kind == Formula && m_data.is_empty() && new_data.is_empty()) {
  15. m_kind = LiteralString;
  16. return;
  17. }
  18. if (m_data == new_data)
  19. return;
  20. if (new_data.starts_with('=')) {
  21. new_data = new_data.substring(1, new_data.length() - 1);
  22. m_kind = Formula;
  23. } else {
  24. m_kind = LiteralString;
  25. }
  26. m_data = move(new_data);
  27. m_dirty = true;
  28. m_evaluated_externally = false;
  29. }
  30. void Cell::set_data(JS::Value new_data)
  31. {
  32. m_dirty = true;
  33. m_evaluated_externally = true;
  34. StringBuilder builder;
  35. builder.append(new_data.to_string_without_side_effects());
  36. m_data = builder.build();
  37. m_evaluated_data = move(new_data);
  38. }
  39. void Cell::set_type(CellType const* type)
  40. {
  41. m_type = type;
  42. }
  43. void Cell::set_type(StringView name)
  44. {
  45. auto* cell_type = CellType::get_by_name(name);
  46. if (cell_type) {
  47. return set_type(cell_type);
  48. }
  49. VERIFY_NOT_REACHED();
  50. }
  51. void Cell::set_type_metadata(CellTypeMetadata&& metadata)
  52. {
  53. m_type_metadata = move(metadata);
  54. }
  55. CellType const& Cell::type() const
  56. {
  57. if (m_type)
  58. return *m_type;
  59. if (m_kind == LiteralString) {
  60. if (m_data.to_int().has_value())
  61. return *CellType::get_by_name("Numeric"sv);
  62. }
  63. return *CellType::get_by_name("Identity"sv);
  64. }
  65. JS::ThrowCompletionOr<String> Cell::typed_display() const
  66. {
  67. return type().display(const_cast<Cell&>(*this), m_type_metadata);
  68. }
  69. JS::ThrowCompletionOr<JS::Value> Cell::typed_js_data() const
  70. {
  71. return type().js_value(const_cast<Cell&>(*this), m_type_metadata);
  72. }
  73. void Cell::update_data(Badge<Sheet>)
  74. {
  75. TemporaryChange cell_change { m_sheet->current_evaluated_cell(), this };
  76. if (!m_dirty)
  77. return;
  78. if (m_dirty) {
  79. m_dirty = false;
  80. if (m_kind == Formula) {
  81. if (!m_evaluated_externally) {
  82. auto value_or_error = m_sheet->evaluate(m_data, this);
  83. if (value_or_error.is_error()) {
  84. m_evaluated_data = JS::js_undefined();
  85. m_thrown_value = *value_or_error.release_error().release_value();
  86. } else {
  87. m_evaluated_data = value_or_error.release_value();
  88. m_thrown_value = {};
  89. }
  90. }
  91. }
  92. for (auto& ref : m_referencing_cells) {
  93. if (ref) {
  94. ref->m_dirty = true;
  95. ref->update();
  96. }
  97. }
  98. }
  99. m_evaluated_formats.background_color.clear();
  100. m_evaluated_formats.foreground_color.clear();
  101. if (m_thrown_value.is_empty()) {
  102. for (auto& fmt : m_conditional_formats) {
  103. if (!fmt.condition.is_empty()) {
  104. auto value_or_error = m_sheet->evaluate(fmt.condition, this);
  105. if (value_or_error.is_error()) {
  106. m_thrown_value = *value_or_error.release_error().release_value();
  107. } else {
  108. if (value_or_error.release_value().to_boolean()) {
  109. if (fmt.background_color.has_value())
  110. m_evaluated_formats.background_color = fmt.background_color;
  111. if (fmt.foreground_color.has_value())
  112. m_evaluated_formats.foreground_color = fmt.foreground_color;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. void Cell::update()
  120. {
  121. m_sheet->update(*this);
  122. }
  123. JS::Value Cell::js_data()
  124. {
  125. if (m_dirty)
  126. update();
  127. if (m_kind == Formula)
  128. return m_evaluated_data;
  129. return JS::js_string(m_sheet->interpreter().heap(), m_data);
  130. }
  131. String Cell::source() const
  132. {
  133. StringBuilder builder;
  134. if (m_kind == Formula)
  135. builder.append('=');
  136. builder.append(m_data);
  137. return builder.to_string();
  138. }
  139. // FIXME: Find a better way to figure out dependencies
  140. void Cell::reference_from(Cell* other)
  141. {
  142. if (!other || other == this)
  143. return;
  144. if (!m_referencing_cells.find_if([other](auto const& ptr) { return ptr.ptr() == other; }).is_end())
  145. return;
  146. m_referencing_cells.append(other->make_weak_ptr());
  147. }
  148. void Cell::copy_from(Cell const& other)
  149. {
  150. m_dirty = true;
  151. m_evaluated_externally = other.m_evaluated_externally;
  152. m_data = other.m_data;
  153. m_evaluated_data = other.m_evaluated_data;
  154. m_kind = other.m_kind;
  155. m_type = other.m_type;
  156. m_type_metadata = other.m_type_metadata;
  157. m_conditional_formats = other.m_conditional_formats;
  158. m_evaluated_formats = other.m_evaluated_formats;
  159. m_thrown_value = other.m_thrown_value;
  160. }
  161. }