mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 19:35:06 +00:00
Spreadsheet: Let the cells know their own position in the sheet
This commit is contained in:
parent
13ce24de13
commit
f159d161fa
7 changed files with 227 additions and 22 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include "ConditionalFormatting.h"
|
#include "ConditionalFormatting.h"
|
||||||
#include "Forward.h"
|
#include "Forward.h"
|
||||||
#include "JSIntegration.h"
|
#include "JSIntegration.h"
|
||||||
|
#include "Position.h"
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/WeakPtr.h>
|
#include <AK/WeakPtr.h>
|
||||||
|
@ -37,20 +38,22 @@
|
||||||
namespace Spreadsheet {
|
namespace Spreadsheet {
|
||||||
|
|
||||||
struct Cell : public Weakable<Cell> {
|
struct Cell : public Weakable<Cell> {
|
||||||
Cell(String data, WeakPtr<Sheet> sheet)
|
Cell(String data, Position position, WeakPtr<Sheet> sheet)
|
||||||
: dirty(false)
|
: dirty(false)
|
||||||
, data(move(data))
|
, data(move(data))
|
||||||
, kind(LiteralString)
|
, kind(LiteralString)
|
||||||
, sheet(sheet)
|
, 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)
|
: dirty(false)
|
||||||
, data(move(source))
|
, data(move(source))
|
||||||
, evaluated_data(move(cell_value))
|
, evaluated_data(move(cell_value))
|
||||||
, kind(Formula)
|
, kind(Formula)
|
||||||
, sheet(sheet)
|
, sheet(sheet)
|
||||||
|
, m_position(move(position))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +66,13 @@ struct Cell : public Weakable<Cell> {
|
||||||
void set_type(const CellType*);
|
void set_type(const CellType*);
|
||||||
void set_type_metadata(CellTypeMetadata&&);
|
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 Format& evaluated_formats() const { return m_evaluated_formats; }
|
||||||
const Vector<ConditionalFormat>& conditional_formats() const { return m_conditional_formats; }
|
const Vector<ConditionalFormat>& conditional_formats() const { return m_conditional_formats; }
|
||||||
void set_conditional_formats(Vector<ConditionalFormat>&& fmts)
|
void set_conditional_formats(Vector<ConditionalFormat>&& fmts)
|
||||||
|
@ -99,6 +109,7 @@ struct Cell : public Weakable<Cell> {
|
||||||
Vector<WeakPtr<Cell>> referencing_cells;
|
Vector<WeakPtr<Cell>> referencing_cells;
|
||||||
const CellType* m_type { nullptr };
|
const CellType* m_type { nullptr };
|
||||||
CellTypeMetadata m_type_metadata;
|
CellTypeMetadata m_type_metadata;
|
||||||
|
Position m_position;
|
||||||
|
|
||||||
Vector<ConditionalFormat> m_conditional_formats;
|
Vector<ConditionalFormat> m_conditional_formats;
|
||||||
Format m_evaluated_formats;
|
Format m_evaluated_formats;
|
||||||
|
|
|
@ -82,6 +82,7 @@ void SheetGlobalObject::initialize()
|
||||||
{
|
{
|
||||||
GlobalObject::initialize();
|
GlobalObject::initialize();
|
||||||
define_native_function("parse_cell_name", parse_cell_name, 1);
|
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)
|
JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
|
||||||
|
@ -106,6 +107,36 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::parse_cell_name)
|
||||||
return object;
|
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)
|
WorkbookObject::WorkbookObject(Workbook& workbook)
|
||||||
: JS::Object(*JS::Object::create_empty(workbook.global_object()))
|
: JS::Object(*JS::Object::create_empty(workbook.global_object()))
|
||||||
, m_workbook(workbook)
|
, m_workbook(workbook)
|
||||||
|
|
|
@ -45,6 +45,7 @@ public:
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
|
|
||||||
JS_DECLARE_NATIVE_FUNCTION(parse_cell_name);
|
JS_DECLARE_NATIVE_FUNCTION(parse_cell_name);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(current_cell_position);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Sheet& m_sheet;
|
Sheet& m_sheet;
|
||||||
|
|
49
Applications/Spreadsheet/Position.h
Normal file
49
Applications/Spreadsheet/Position.h
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -245,12 +245,12 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
|
||||||
OwnPtr<Cell> cell;
|
OwnPtr<Cell> cell;
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case Cell::LiteralString:
|
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;
|
break;
|
||||||
case Cell::Formula: {
|
case Cell::Formula: {
|
||||||
auto& interpreter = sheet->interpreter();
|
auto& interpreter = sheet->interpreter();
|
||||||
auto value = interpreter.call(parse_function, json, JS::js_string(interpreter.heap(), obj.get("value").as_string()));
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,21 +41,6 @@
|
||||||
|
|
||||||
namespace Spreadsheet {
|
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 {
|
class Sheet : public Core::Object {
|
||||||
C_OBJECT(Sheet);
|
C_OBJECT(Sheet);
|
||||||
|
|
||||||
|
@ -89,7 +74,7 @@ public:
|
||||||
if (auto cell = at(position))
|
if (auto cell = at(position))
|
||||||
return *cell;
|
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);
|
return *at(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
function range(start, end, columnStep, rowStep) {
|
||||||
columnStep = integer(columnStep ?? 1);
|
columnStep = integer(columnStep ?? 1);
|
||||||
rowStep = integer(rowStep ?? 1);
|
rowStep = integer(rowStep ?? 1);
|
||||||
start = parse_cell_name(start) ?? { column: "A", row: 0 };
|
if (!(start instanceof Position)) {
|
||||||
end = parse_cell_name(end) ?? start;
|
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)
|
if (end.column.length > 1 || start.column.length > 1)
|
||||||
throw new TypeError("Only single-letter column names are allowed (TODO)");
|
throw new TypeError("Only single-letter column names are allowed (TODO)");
|
||||||
|
@ -172,6 +246,21 @@ function stddev(cells) {
|
||||||
return Math.sqrt(variance(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
|
// Cheat the system and add documentation
|
||||||
range.__documentation = JSON.stringify({
|
range.__documentation = JSON.stringify({
|
||||||
name: "range",
|
name: "range",
|
||||||
|
@ -382,3 +471,42 @@ stddev.__documentation = JSON.stringify({
|
||||||
'stddev(range("A0", "C4"))': "Calculate the standard deviation of the values in A0:C4",
|
'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.",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue