diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-11-27 13:55:14 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-30 12:07:45 +0100 |
commit | 474453244b591ff8c90547831e798d66c5d2be1e (patch) | |
tree | 6a29739f698b40c082cd5f04513abddc0a97c9cf /Applications/Spreadsheet/JSIntegration.cpp | |
parent | f6ae4edbd230dab2dcc4e108f731f00f54627497 (diff) | |
download | serenity-474453244b591ff8c90547831e798d66c5d2be1e.zip |
Spreadsheet: Implement infinit-scroll for columns
This naturally also implements multi-char columns, and also integrates
it into the js runtime (such columns can be named in ranges too).
Diffstat (limited to 'Applications/Spreadsheet/JSIntegration.cpp')
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Applications/Spreadsheet/JSIntegration.cpp b/Applications/Spreadsheet/JSIntegration.cpp index 26cc864cb7..7c94cda388 100644 --- a/Applications/Spreadsheet/JSIntegration.cpp +++ b/Applications/Spreadsheet/JSIntegration.cpp @@ -83,6 +83,8 @@ 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); + define_native_function("column_arithmetic", column_arithmetic, 2); + define_native_function("column_index", column_index, 1); } JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name) @@ -137,6 +139,82 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::current_cell_position) return object; } +JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_index) +{ + if (vm.argument_count() != 2) { + vm.throw_exception<JS::TypeError>(global_object, "Expected exactly one argument to column_index()"); + return {}; + } + + auto column_name = vm.argument(0); + if (!column_name.is_string()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "String"); + return {}; + } + + auto& column_name_str = column_name.as_string().string(); + + 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& sheet = sheet_object->m_sheet; + auto column_index = sheet.column_index(column_name_str); + if (!column_index.has_value()) { + vm.throw_exception(global_object, JS::TypeError::create(global_object, String::formatted("'{}' is not a valid column", column_name_str))); + return {}; + } + + return JS::Value((i32)column_index.value()); +} + +JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic) +{ + if (vm.argument_count() != 2) { + vm.throw_exception<JS::TypeError>(global_object, "Expected exactly two arguments to column_arithmetic()"); + return {}; + } + + auto column_name = vm.argument(0); + if (!column_name.is_string()) { + vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "String"); + return {}; + } + + auto& column_name_str = column_name.as_string().string(); + + auto offset = vm.argument(1).to_number(global_object); + if (!offset.is_number()) + return {}; + + auto offset_number = offset.as_i32(); + + 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& sheet = sheet_object->m_sheet; + auto new_column = sheet.column_arithmetic(column_name_str, offset_number); + if (!new_column.has_value()) { + vm.throw_exception(global_object, JS::TypeError::create(global_object, String::formatted("'{}' is not a valid column", column_name_str))); + return {}; + } + + return JS::js_string(vm, new_column.release_value()); +} + WorkbookObject::WorkbookObject(Workbook& workbook) : JS::Object(*JS::Object::create_empty(workbook.global_object())) , m_workbook(workbook) |