1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:37:34 +00:00

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.
This commit is contained in:
Ali Mohammad Pur 2022-06-25 20:05:17 +04:30 committed by Linus Groh
parent 2104e9a6e4
commit 746b8ec8de
4 changed files with 93 additions and 20 deletions

View file

@ -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));

View file

@ -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;

View file

@ -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");