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 | |
parent | 13ce24de135805c250ca64e32b8f911004073a69 (diff) | |
download | serenity-f159d161fa6c15aa16cdac6967a2b165d0f9fef0.zip |
Spreadsheet: Let the cells know their own position in the sheet
-rw-r--r-- | Applications/Spreadsheet/Cell.h | 15 | ||||
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.cpp | 31 | ||||
-rw-r--r-- | Applications/Spreadsheet/JSIntegration.h | 1 | ||||
-rw-r--r-- | Applications/Spreadsheet/Position.h | 49 | ||||
-rw-r--r-- | Applications/Spreadsheet/Spreadsheet.cpp | 4 | ||||
-rw-r--r-- | Applications/Spreadsheet/Spreadsheet.h | 17 | ||||
-rw-r--r-- | Base/res/js/Spreadsheet/runtime.js | 132 |
7 files changed, 227 insertions, 22 deletions
diff --git a/Applications/Spreadsheet/Cell.h b/Applications/Spreadsheet/Cell.h index 3942d83c94..04c10a66c0 100644 --- a/Applications/Spreadsheet/Cell.h +++ b/Applications/Spreadsheet/Cell.h @@ -30,6 +30,7 @@ #include "ConditionalFormatting.h" #include "Forward.h" #include "JSIntegration.h" +#include "Position.h" #include <AK/String.h> #include <AK/Types.h> #include <AK/WeakPtr.h> @@ -37,20 +38,22 @@ namespace Spreadsheet { struct Cell : public Weakable<Cell> { - Cell(String data, WeakPtr<Sheet> sheet) + Cell(String data, Position position, WeakPtr<Sheet> sheet) : dirty(false) , data(move(data)) , kind(LiteralString) , sheet(sheet) + , m_position(move(position)) { } - Cell(String source, JS::Value&& cell_value, WeakPtr<Sheet> sheet) + Cell(String source, JS::Value&& cell_value, Position position, WeakPtr<Sheet> sheet) : dirty(false) , data(move(source)) , evaluated_data(move(cell_value)) , kind(Formula) , sheet(sheet) + , m_position(move(position)) { } @@ -63,6 +66,13 @@ struct Cell : public Weakable<Cell> { void set_type(const CellType*); void set_type_metadata(CellTypeMetadata&&); + const Position& position() const { return m_position; } + void set_position(Position position, Badge<Sheet>) + { + dirty = true; + m_position = move(position); + } + const Format& evaluated_formats() const { return m_evaluated_formats; } const Vector<ConditionalFormat>& conditional_formats() const { return m_conditional_formats; } void set_conditional_formats(Vector<ConditionalFormat>&& fmts) @@ -99,6 +109,7 @@ struct Cell : public Weakable<Cell> { Vector<WeakPtr<Cell>> referencing_cells; const CellType* m_type { nullptr }; CellTypeMetadata m_type_metadata; + Position m_position; Vector<ConditionalFormat> m_conditional_formats; Format m_evaluated_formats; 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) diff --git a/Applications/Spreadsheet/JSIntegration.h b/Applications/Spreadsheet/JSIntegration.h index 9bddb1e25e..46886f9529 100644 --- a/Applications/Spreadsheet/JSIntegration.h +++ b/Applications/Spreadsheet/JSIntegration.h @@ -45,6 +45,7 @@ public: virtual void initialize() override; JS_DECLARE_NATIVE_FUNCTION(parse_cell_name); + JS_DECLARE_NATIVE_FUNCTION(current_cell_position); private: Sheet& m_sheet; diff --git a/Applications/Spreadsheet/Position.h b/Applications/Spreadsheet/Position.h new file mode 100644 index 0000000000..358ab9c1f6 --- /dev/null +++ b/Applications/Spreadsheet/Position.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include <AK/String.h> +#include <AK/Types.h> + +namespace Spreadsheet { + +struct Position { + String column; + size_t row { 0 }; + + bool operator==(const Position& other) const + { + return row == other.row && column == other.column; + } + + bool operator!=(const Position& other) const + { + return !(other == *this); + } +}; + +} diff --git a/Applications/Spreadsheet/Spreadsheet.cpp b/Applications/Spreadsheet/Spreadsheet.cpp index 0519434c1f..254b77ae71 100644 --- a/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Applications/Spreadsheet/Spreadsheet.cpp @@ -245,12 +245,12 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook) OwnPtr<Cell> cell; switch (kind) { case Cell::LiteralString: - cell = make<Cell>(obj.get("value").to_string(), sheet->make_weak_ptr()); + cell = make<Cell>(obj.get("value").to_string(), position, sheet->make_weak_ptr()); break; case Cell::Formula: { auto& interpreter = sheet->interpreter(); auto value = interpreter.call(parse_function, json, JS::js_string(interpreter.heap(), obj.get("value").as_string())); - cell = make<Cell>(obj.get("source").to_string(), move(value), sheet->make_weak_ptr()); + cell = make<Cell>(obj.get("source").to_string(), move(value), position, sheet->make_weak_ptr()); break; } } diff --git a/Applications/Spreadsheet/Spreadsheet.h b/Applications/Spreadsheet/Spreadsheet.h index 3207398ec8..8d8ad29acc 100644 --- a/Applications/Spreadsheet/Spreadsheet.h +++ b/Applications/Spreadsheet/Spreadsheet.h @@ -41,21 +41,6 @@ namespace Spreadsheet { -struct Position { - String column; - size_t row { 0 }; - - bool operator==(const Position& other) const - { - return row == other.row && column == other.column; - } - - bool operator!=(const Position& other) const - { - return !(other == *this); - } -}; - class Sheet : public Core::Object { C_OBJECT(Sheet); @@ -89,7 +74,7 @@ public: if (auto cell = at(position)) return *cell; - m_cells.set(position, make<Cell>(String::empty(), make_weak_ptr())); + m_cells.set(position, make<Cell>(String::empty(), position, make_weak_ptr())); return *at(position); } diff --git a/Base/res/js/Spreadsheet/runtime.js b/Base/res/js/Spreadsheet/runtime.js index df74a37d1c..67e66307e0 100644 --- a/Base/res/js/Spreadsheet/runtime.js +++ b/Base/res/js/Spreadsheet/runtime.js @@ -1,8 +1,82 @@ +// FIXME: Figure out a way to document non-function entities too. +class Position { + constructor(column, row, sheet) { + this.column = column; + this.row = row; + this.sheet = sheet ?? thisSheet; + this.name = `${column}${row}`; + } + + static from_name(name) { + let sheet = thisSheet; + let obj = sheet.parse_cell_name(name); + return new Position(obj.column, obj.row, sheet); + } + + up(how_many) { + how_many = how_many ?? 1; + const row = Math.max(0, this.row - how_many); + return new Position(this.column, row, this.sheet); + } + + down(how_many) { + how_many = how_many ?? 1; + const row = Math.max(0, this.row + how_many); + return new Position(this.column, row, this.sheet); + } + + left(how_many) { + how_many = how_many ?? 1; + const column = Math.min( + "Z".charCodeAt(0), + Math.max("A".charCodeAt(0), this.column.charCodeAt(0) - how_many) + ); + return new Position(String.fromCharCode(column), this.row, this.sheet); + } + + right(how_many) { + how_many = how_many ?? 1; + const column = Math.min( + "Z".charCodeAt(0), + Math.max("A".charCodeAt(0), this.column.charCodeAt(0) + how_many) + ); + return new Position(String.fromCharCode(column), this.row, this.sheet); + } + + with_column(value) { + return new Position(value, this.row, this.sheet); + } + + with_row(value) { + return new Position(this.column, value, this.sheet); + } + + in_sheet(the_sheet) { + return new Position(this.column, this.row, sheet(the_sheet)); + } + + value() { + return this.sheet[this.name]; + } + + valueOf() { + return value(); + } + + toString() { + return `<Cell at ${this.name}>`; + } +} + function range(start, end, columnStep, rowStep) { columnStep = integer(columnStep ?? 1); rowStep = integer(rowStep ?? 1); - start = parse_cell_name(start) ?? { column: "A", row: 0 }; - end = parse_cell_name(end) ?? start; + if (!(start instanceof Position)) { + start = parse_cell_name(start) ?? { column: "A", row: 0 }; + } + if (!(end instanceof Position)) { + end = parse_cell_name(end) ?? start; + } if (end.column.length > 1 || start.column.length > 1) throw new TypeError("Only single-letter column names are allowed (TODO)"); @@ -172,6 +246,21 @@ function stddev(cells) { return Math.sqrt(variance(cells)); } +// Lookup + +function row() { + return thisSheet.current_cell_position().row; +} + +function column() { + return thisSheet.current_cell_position().column; +} + +function here() { + const position = current_cell_position(); + return new Position(position.column, position.row, thisSheet); +} + // Cheat the system and add documentation range.__documentation = JSON.stringify({ name: "range", @@ -382,3 +471,42 @@ stddev.__documentation = JSON.stringify({ 'stddev(range("A0", "C4"))': "Calculate the standard deviation of the values in A0:C4", }, }); + +row.__documentation = JSON.stringify({ + name: "row", + argc: 0, + argnames: [], + doc: "Returns the row number of the current cell", + examples: {}, +}); + +column.__documentation = JSON.stringify({ + name: "column", + argc: 0, + argnames: [], + doc: "Returns the column name of the current cell", + examples: {}, +}); + +here.__documentation = JSON.stringify({ + name: "here", + argc: 0, + argnames: [], + doc: + "Returns an object representing the current cell's position, see `Position` below.\n\n" + + "## Position\na `Position` is an object representing a given cell position in a given sheet.\n" + + "### Methods:\n- `up(count = 1)`: goes up count cells, or returns the top position if at the top\n" + + "- `down(count = 1)`: goes down count cells\n" + + "- `left(count = 1)`: Goes left count cells, or returns the leftmost position if the edge\n" + + "- `right(count = 1)`: Goes right count cells.\n" + + "- `with_row(row)`: Returns a Position with its column being this object's, and its row being the provided the value.\n" + + "- `with_column(column)`: Similar to `with_row()`, but changes the column instead.\n" + + "- `in_sheet(the_sheet)`: Returns a Position with the same column and row as this one, but with its sheet being `the_sheet`.\n" + + "- `value()`: Returns the value at the position which it represents, in the object's sheet (current sheet by default).\n\n" + + "**NOTE**: Currently only supports single-letter column names", + examples: { + "here().up().value()": "Get the value of the cell above this one", + "here().up().with_column('A')": + "Get a Position above this one in column A, for instance, evaluates to A2 if run in B3.", + }, +}); |