mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:37:34 +00:00
Spreadsheet: Show errors and make them red
This commit is contained in:
parent
cb7fe4fe7c
commit
8db5057dc4
2 changed files with 36 additions and 2 deletions
|
@ -25,6 +25,8 @@
|
|||
*/
|
||||
|
||||
#include "SpreadsheetModel.h"
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
||||
namespace Spreadsheet {
|
||||
|
||||
|
@ -32,6 +34,16 @@ SheetModel::~SheetModel()
|
|||
{
|
||||
}
|
||||
|
||||
static inline JS::Object* as_error(JS::Value value)
|
||||
{
|
||||
if (value.is_object()) {
|
||||
auto& object = value.as_object();
|
||||
return object.is_error() ? &object : nullptr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
||||
{
|
||||
if (!index.is_valid())
|
||||
|
@ -42,8 +54,16 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
|
|||
if (!value)
|
||||
return String::empty();
|
||||
|
||||
if (value->kind == Spreadsheet::Cell::Formula)
|
||||
if (value->kind == Spreadsheet::Cell::Formula) {
|
||||
if (auto object = as_error(value->evaluated_data)) {
|
||||
StringBuilder builder;
|
||||
auto error = object->get("message").to_string_without_side_effects();
|
||||
builder.append("Error: ");
|
||||
builder.append(error);
|
||||
return builder.to_string();
|
||||
}
|
||||
return value->evaluated_data.is_empty() ? "" : value->evaluated_data.to_string_without_side_effects();
|
||||
}
|
||||
|
||||
return value->data;
|
||||
}
|
||||
|
@ -51,6 +71,19 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
|
|||
if (role == GUI::ModelRole::TextAlignment)
|
||||
return {};
|
||||
|
||||
if (role == GUI::ModelRole::ForegroundColor) {
|
||||
const auto* value = m_sheet->at({ m_sheet->column(index.column()), (size_t)index.row() });
|
||||
if (!value)
|
||||
return {};
|
||||
|
||||
if (value->kind == Spreadsheet::Cell::Formula) {
|
||||
if (as_error(value->evaluated_data))
|
||||
return Color(Color::Red);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue