diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-06-25 20:05:17 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-06-26 22:21:17 +0100 |
commit | 746b8ec8de6b6ffa43fb866e92b5903f9b4002fe (patch) | |
tree | f3cf25cd66c5250c2458c1790a37d3b255edadac /Userland/Applications | |
parent | 2104e9a6e4e465f30d4e029e9a8c22b26265c22b (diff) | |
download | serenity-746b8ec8de6b6ffa43fb866e92b5903f9b4002fe.zip |
Spreadsheet: Make it possible to refer to ranges in other sheets
Now the range A0:C4 in a sheet named "foo" can be represented as:
R`sheet("foo"):A0:C4`
This makes it possible to do cross-sheet lookups and more.
Diffstat (limited to 'Userland/Applications')
-rw-r--r-- | Userland/Applications/Spreadsheet/JSIntegration.cpp | 12 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/JSIntegration.h | 1 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Tests/basic.js | 22 |
3 files changed, 35 insertions, 0 deletions
diff --git a/Userland/Applications/Spreadsheet/JSIntegration.cpp b/Userland/Applications/Spreadsheet/JSIntegration.cpp index a3a047bf27..febae065de 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.cpp +++ b/Userland/Applications/Spreadsheet/JSIntegration.cpp @@ -154,6 +154,7 @@ void SheetGlobalObject::initialize_global_object() define_native_function("column_arithmetic", column_arithmetic, 2, attr); define_native_function("column_index", column_index, 1, attr); define_native_function("get_column_bound", get_column_bound, 1, attr); + define_native_accessor("name", get_name, nullptr, attr); } void SheetGlobalObject::visit_edges(Visitor& visitor) @@ -167,6 +168,17 @@ void SheetGlobalObject::visit_edges(Visitor& visitor) } } +JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_name) +{ + auto* this_object = TRY(vm.this_value(global_object).to_object(global_object)); + + if (!is<SheetGlobalObject>(this_object)) + return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "SheetGlobalObject"); + + auto sheet_object = static_cast<SheetGlobalObject*>(this_object); + return JS::js_string(global_object.heap(), sheet_object->m_sheet.name()); +} + JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::get_real_cell_contents) { auto* this_object = TRY(vm.this_value(global_object).to_object(global_object)); diff --git a/Userland/Applications/Spreadsheet/JSIntegration.h b/Userland/Applications/Spreadsheet/JSIntegration.h index 2fb1f28ad7..15c237634d 100644 --- a/Userland/Applications/Spreadsheet/JSIntegration.h +++ b/Userland/Applications/Spreadsheet/JSIntegration.h @@ -39,6 +39,7 @@ public: JS_DECLARE_NATIVE_FUNCTION(column_index); JS_DECLARE_NATIVE_FUNCTION(column_arithmetic); JS_DECLARE_NATIVE_FUNCTION(get_column_bound); + JS_DECLARE_NATIVE_FUNCTION(get_name); private: virtual void visit_edges(Visitor&) override; diff --git a/Userland/Applications/Spreadsheet/Tests/basic.js b/Userland/Applications/Spreadsheet/Tests/basic.js index 9f04e251b8..ff9cf79147 100644 --- a/Userland/Applications/Spreadsheet/Tests/basic.js +++ b/Userland/Applications/Spreadsheet/Tests/basic.js @@ -78,6 +78,28 @@ describe("Range", () => { expect(cellsVisited).toEqual(6); }); + test("multiple sheets", () => { + const workbook = createWorkbook(); + const sheet1 = createSheet(workbook, "Sheet 1"); + const sheet2 = createSheet(workbook, "Sheet 2"); + sheet1.makeCurrent(); + + sheet1.setCell("A", 0, "0"); + sheet1.focusCell("A", 0); + + sheet2.setCell("A", 0, "0"); + sheet2.setCell("A", 10, "0"); + sheet2.setCell("B", 1, "0"); + sheet2.focusCell("A", 0); + + expect(R).toBeDefined(); + let cellsVisited = 0; + R`sheet("Sheet 2"):A0:A10`.forEach(name => { + ++cellsVisited; + }); + expect(cellsVisited).toEqual(11); + }); + test("Ranges", () => { const workbook = createWorkbook(); const sheet = createSheet(workbook, "Sheet 1"); |