diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-09-26 15:29:11 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-28 17:41:48 +0200 |
commit | f159d161fa6c15aa16cdac6967a2b165d0f9fef0 (patch) | |
tree | ae967b4d7bcf1c742c56083c7f03b82bde53f117 /Applications/Spreadsheet/JSIntegration.cpp | |
parent | 13ce24de135805c250ca64e32b8f911004073a69 (diff) | |
download | serenity-f159d161fa6c15aa16cdac6967a2b165d0f9fef0.zip |
Spreadsheet: Let the cells know their own position in the sheet
Diffstat (limited to 'Applications/Spreadsheet/JSIntegration.cpp')
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Applications/Spreadsheet/JSIntegration.cpp b/Applications/Spreadsheet/JSIntegration.cpp index f393093d8e..26cc864cb7 100644 --- a/Applications/Spreadsheet/JSIntegration.cpp +++ b/Applications/Spreadsheet/JSIntegration.cpp @@ -82,6 +82,7 @@ void SheetGlobalObject::initialize() { GlobalObject::initialize(); define_native_function("parse_cell_name", parse_cell_name, 1); + define_native_function("current_cell_position", current_cell_position, 0); } JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name) @@ -106,6 +107,36 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name) return object; } +JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position) +{ + if (vm.argument_count() != 0) { + vm.throw_exception<JS::TypeError>(global_object, "Expected no arguments to current_cell_position()"); + return {}; + } + + auto* this_object = vm.this_value(global_object).to_object(global_object); + if (!this_object) + return JS::js_null(); + + if (StringView("SheetGlobalObject") != this_object->class_name()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "SheetGlobalObject"); + return {}; + } + + auto sheet_object = static_cast<SheetGlobalObject*>(this_object); + auto* current_cell = sheet_object->m_sheet.current_evaluated_cell(); + if (!current_cell) + return JS::js_null(); + + auto position = current_cell->position(); + + auto object = JS::Object::create_empty(global_object); + object->put("column", JS::js_string(vm, position.column)); + object->put("row", JS::Value((unsigned)position.row)); + + return object; +} + WorkbookObject::WorkbookObject(Workbook& workbook) : JS::Object(*JS::Object::create_empty(workbook.global_object())) , m_workbook(workbook) |