1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:48:13 +00:00

Spreadsheet: Let the cells know their own position in the sheet

This commit is contained in:
AnotherTest 2020-09-26 15:29:11 +03:30 committed by Andreas Kling
parent 13ce24de13
commit f159d161fa
7 changed files with 227 additions and 22 deletions

View file

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

View file

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

View file

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

View 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);
}
};
}

View file

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

View file

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